zquizle
zquizle

Reputation: 23

Loading DLL with dynamic path in Excel 32bit

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?

Upvotes: 0

Views: 91

Answers (1)

zquizle
zquizle

Reputation: 23

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

Upvotes: 1

Related Questions