admin管理员组文章数量:1336331
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?
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?
Share Improve this question edited Nov 20, 2024 at 14:16 zquizle asked Nov 19, 2024 at 21:09 zquizlezquizle 233 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1I solved the problem on my own. On windows, there is already a Crypt32.dll. So vba directed to it…
本文标签: vbaLoading DLL with dynamic path in Excel 32bitStack Overflow
版权声明:本文标题:vba - Loading DLL with dynamic path in Excel 32bit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398605a2467427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
#If Win64
try using#If VBA7
. – BobS Commented Nov 20, 2024 at 2:45