PDA

View Full Version : [SOLVED:] DLL expects Char*File_Location Having issues figuring out how to correctly pass it.



Zuccj
06-26-2017, 07:06 AM
I'm trying to use a function from a dll that will open a file. The issue I'm having is I can't quite figure out how to correctly pass the file location information to the dll, as it is expecting the Char type. I've tried passing the data as a byte and string array, but neither one has worked so far.

The dll list the function as:


unsigned long NQ_LoadParams (unsigned long ulNQHandle, char*pcFilename)

I'm calling it like this:


Private Declare Function NQ_LoadParams Lib "C:\....dll" (ByVal ulNQHandle As Long, _
ByRef pcFilename() As String) As NQ_ErrorEnum


Sub LoadFile_Click()
Dim fopen As FileDialog
Dim fcontent As String
Dim textfile As Integer
Dim holder As String
Dim addr() As String

'Sets up File Location to File Location
Set fopen = Application.FileDialog(msoFileDialogOpen)
fopen.Title = "Select Config File"
fopen.InitialFileName = "C:\..."
fopen.Show

'Takes the file Selected and Stores its location
holder = fopen.SelectedItems(1)
ReDim addr(Len(holder) - 1)
For i = 1 To Len(holder)
addr(i - 1) = Mid(holder, i, 1)
Next

'Opens the files location
textfile = FreeFile
Open holder For Input As #textfile

'Stores file's content after it has been opened
fcontent = Input(LOF(textfile), textfile)

'Closes File
Close #textfile

ulNQError = NQ_LoadParams(ulNQHandle, addr)

End Sub

When I tried the byte array I used the StrConv() function.
I know addr has the correct information. I'm just not sure what format it needs to be, or what needs to be done in able to pass it to the dll correctly.
Also, I've tried looking around real quick for an answer on this, but I was wondering are VBA strings naturally null terminated? I was thinking could be a possible cause for error, but I would imagine that it null terminates them.

Aflatoon
06-26-2017, 07:46 AM
Have you tried declaring it like this:


Private Declare Function NQ_LoadParams Lib "C:\....dll" (ByVal ulNQHandle As Long, _
ByVal pcFilename As String) As NQ_ErrorEnum

and then just pass holder as the parameter?

Zuccj
06-26-2017, 08:10 AM
So I just tried doing that. It stopped throwing an error code after that, but it didn't look like it actually loaded the file. Plus it is now crashing about 10secs after running the load function.

Ok running it like you suggested ended up working. I had a variable mistyped farther down, which made it appear like it wasn't loading. After finding that and fixing the crash problem everything is working as it should be. Thanks.