VB DLL problems

Status
Not open for further replies.

technophile

Ars Legatus Legionis
23,768
I have a (black box) DLL -- non-COM -- which I am using in a VB project. The function I'm using is declared at the top of the file it's used in similarly:
code:<HR>
Code:
Private Declare Function MyFunction Lib "thedll.dll" _
  (ByVal lpszPath As String, ByVal lpszUsername As String, _
  ByVal lpszPassword As String, ByVal lpszServer As String) As Long

In the function which calls the DLL, the code goes like this:
code:<HR>
Code:
    Dim curLocation As String
    curLocation = CurDir
    ChDir strPathToTheDLL
    DoEvents
    
    lngReturnCode = MyFunction(lstrPath, lstrUsername, lstrPassword, lstrServer)
    Sleep 1500
    If lngReturnCode = 0 Then
        lngReturnCode = MyFunction(lstrPath, lstrUsername, lstrPassword, lstrServer)
    End If<P>    DoEvents
    ChDir curLocation
The lstr* variables are all local fixed-length string variables. The path to thedll.dll is correct. thedll.dll is not in the system path or the process path, but even if I add it the problem persists. MyFunction returns 0 on failure and 1 on success.

Now, here's the weird thing:
If I run the function in Debug mode, even without any breakpoints, the first call succeeds. In compiled form the behavior is stranger: the first call to MyFunction fails. If I immediately call it again the second one fails as well. If I leave the Sleep 1500 in there the second call succeeds.

I'd really rather not have the Sleep statement in there, as this function may be called multiple times, and adding at least a 1.5 second delay to each call is bad form. Can anyone tell me why the first call fails, and how to fix it?
 
code:<HR>
Code:
Private Declare Function MyFunction Lib "thedll.dll" _
  (ByVal lpszPath As String, ByVal lpszUsername As String, _
  ByVal lpszPassword As String, ByVal lpszServer As String) As Long

Shouldn't you be using LONG for each value then converting null terminated strings to VB string (or ByRef)? It looks like the DLL is expecting an address of a string and not a string on the stack.
 

russ-iha

Ars Praefectus
4,407
Subscriptor
No way Joe... VB takes care of marshalling the VB safe-strings into standard strings in whatever format expected by the DLL (null-terminated ANSI on Win9x, Unicode on NT/2K). It can also take care of the reverse action automatically.


I should add that in cases where the DLL is stuffing values into the variables themselves, you would just drop the ByVal keyword. VB will marshal the variable into a temporary space (regardless of type, though Variant would be Bad(tm) and Any has its own caveats) regardless of how it is being passed into the DLL, but on the return trip any of the vars that don't have ByVal on them will get pulled from the temp space and re-wrapped in VB's safe types.


If you specify As Any, then you can literally pass anything to the DLL, which is useful since VB 6 and lower don't support overloading.

If you specify Variant, good luck.
 

technophile

Ars Legatus Legionis
23,768
Originally posted by russ-iha:
The DLL your program is using needs to be

a) in the same directory as the app

or

b) in the SYSTEM32 folder

Those are your choices ;) btw: When running under the IDE, sometimes your app's running path will actually be the path to VB6.exe and not your app. Be aware of that....
That is counter to the documentation I've read. See here for a discussion. As far as I know it should just need to be in the current search path.

Of course, it could be that the stuff I've seen is wrong. ;)

Regardless, moving the DLL is impossible.

Anyway, after further work it appeared that the DLL was actually being accessed (sysinternal's FS monitor detected FS access of the DLL in question) and it was actually returning an error code as far as I can tell. It's possible that the DLL was being loaded, but failing to properly initialize or something (though I can't conceive of why). At this point I've worked around the problem since the DLL was just a wrapper around an EXE; I'm calling the EXE directly instead.
 
Status
Not open for further replies.