0

I'm currently trying to load a dll (C++) with a dynamic path into my VBA Code.

I've two different dll's, one for 32bit and one for 64bit. Both dll's are working fine, I can also use both if i declare the path to the dll (

Private Declare Function crypt Lib "C:\path\to\crypt32.dll" (ByVal inputs As String, ByVal intinput As Long) As String

).

I've compiled the 32 bit dll with a .def, so i can call it without an Alias correctly.

The problem is, that it only works on Excel 64bit with the 64bit dll, not on Excel 32bit with the 32bit dll. I've also tried it with the mangling name, before i created the .def.

Here is my VBA Code to load it with the dynamic path:

#If Win64 Then
    Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare PtrSafe Function crypt Lib "crypt64.dll" (ByVal inputs As String, ByVal intinput As Long) As String
#Else
    Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare Function crypt Lib "crypt32.dll" (ByVal inputs As String, ByVal intinput As Long) As String
#End If

Public Function dllcheck() As Boolean

    Dim dllPath As String, dllPath2 As String, hModule As Long, hModule2 As Long

#If Win64 Then
    dllPath = ActiveSheet.Cells(1, 13).value & "\crypt64.dll"
#Else
    dllPath = ActiveSheet.Cells(1, 13).value & "\crypt32.dll"
#End If

    hModule = LoadLibrary(dllPath)
    
    If hModule = 0 Then
        MsgBox "Cannot find dll"
        dllcheck = False
        Exit Function
    End If

    dllcheck = True
End Function

Sub TestFunction()
    
    If Not dllcheck() Then Exit Sub

    Dim inputChar As String, shiftValue As Long, result As String, outputChar As String

    inputChar = "a"
    shiftValue = 1

    result = crypt(inputChar, shiftValue)
       
    MsgBox "Input: " & inputChar & vbNewLine & _
           "Shift: " & shiftValue & vbNewLine & _
           "Output: " & result, vbInformation, "Output"
End Sub

Thanks

Edit:

Im using it on a Network, so i think that should be the reason since other stuff like ChDir doesnt work aswell. I think the only way is to implement CallWindowProc. I‘m able to get the Loadlibrary and GetProcAdress. Am i able connect to the dll in this way with CallWindowProc?

7
  • Instead of using #If Win64 try using #If VBA7.
    – BobS
    Commented Nov 20 at 2:45
  • Hello Bob, thanks for your comment. That doesnt solve the problem, since VBA7 can also be in 32bit or 64bit. Choosing the Code by #if Win64 is correct in this way. The two dll‘s are made for 32bit and 64bit. If i use VBA7, it would lead a user with Excel 2016 32bit for example into the 64bit dll.
    – zquizle
    Commented Nov 20 at 6:47
  • What are your Excel versions? Why PtrSafe on one side but not the other? Why the need for a .def file (I showed in your other question that it works fine without it)? What does work and what doesn't (in one place you claim both are working, in another only one is)?
    – CristiFati
    Commented Nov 21 at 14:31
  • So both dll‘s are working correctly (32bit & 64bit). The problem is, that i want to use them with a dynamic library path and not hard coded in the declaration. Thats the reason why i want to use the LoadLibrary API. This works fine for me on Excel 64bit, but not on Excel 32bit. A way, that would always work is using CallWindowProc API. But i dont get it working with the parameters. I used a .def file, so i dont need to use an Alias which works perfectly on the 32bit dll, but only if i Declare the whole path.
    – zquizle
    Commented Nov 21 at 14:49
  • Excel Versions are 2016 32bit and 2022 64bit, 2010 32bit. I got a couple to test on. 32bit are not working in that way with loading a variable path like the Code above, only the 64bit version works like that.
    – zquizle
    Commented Nov 21 at 15:04

1 Answer 1

1

I solved the problem on my own. On windows, there is already a Crypt32.dll. So vba directed to it…

3
  • Great that you came back and answered your own question, but what was the solution? DId you just rename your dll to something else or did you work around the redirect? Commented Nov 22 at 6:55
  • I just changed the name of my dll, since windows has a crypt32.dll, it linked to the windows dll instead of mine. The Code above is working.
    – zquizle
    Commented Nov 22 at 8:36
  • I wanted to say something about Win's crypt32.dll (even from the other question), but if you specify the full path, there's no problem loading 2 .dlls with the same name (but different paths).
    – CristiFati
    Commented Nov 22 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.