C++ dll funktion an VBA in 32bit richtig übergeben
30.09.2024 18:16:22
zquizle
VBA:
Option Explicit
#If Win64 Then
Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare PtrSafe Function crypt64 Lib "crypt64.dll" (ByVal inputs As String) As String
#Else
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function crypt32 Lib "crypt32.dll" (ByVal inputs As String) As String
#End If
Sub dllcheck(inputs As String) 'prüft ob dll vorhanden ist
Dim dllPath As String, hModule As Long
#If Win64 Then
dllPath = ActiveSheet.Cells(1, 13).Value & "\Code\crypt64.dll"
#Else
dllPath = ActiveSheet.Cells(1, 13).Value & "\Code\crypt32.dll"
#End If
hModule = LoadLibrary(dllPath) 'falls der Ordner Code mit der crypt.dll nicht im selben Ordner/Pfad sind dann fehler
If hModule = 0 Then
MsgBox "Fehler! Stelle sicher, dass die .dll Dateien im Ordner Code gespeichert sind und dieser Ordner im selben Pfad mit der Excel Datei gespeichert ist!"
Exit Sub
End If
End Sub
Private Sub Workbook_Open()
Sheets("87613384684438375387").Unprotect Password:="chat"
Sheets("87613384684438375387").Cells(1, 13).Value = ThisWorkbook.path
Sheets("87613384684438375387").Protect Password:="chat"
dllcheck ""
'usw.......
End Sub
Die DLL Datei wurde zu 100% als x86 richtig kompiliert, hier habe ich bereits in dumpbin reingeschaut. Es ist sicher eine 32bit dll. Die Anwendung von "__stdcall" in 32bit habe ich berücksichtigt.
Anbei mal der Code der DLL Datei, geschrieben in C++:
#include "pch.h"
#include
#include
extern "C" __declspec(dllexport) BSTR __stdcall crypt32(BSTR input) {
wchar_t output[2];
wchar_t inputChar = input[0];
if (inputChar >= L'A' && inputChar = L'Z') {
inputChar = (inputChar - L'A' + 1) % 26 + L'A';
}
else if (inputChar >= L'a' && inputChar = L'z') {
inputChar = (inputChar - L'a' + 1) % 26 + L'a';
}
switch (inputChar) {
case L'A': output[0] = L'B'; break;
\\usw....
default: output[0] = inputChar; break;
}
output[1] = L'\0';
return SysAllocString(output);
}
extern "C" __declspec(dllexport) void FreeBSTR(BSTR bstr) {
SysFreeString(bstr);
}
Ich bin neu hier, daher nehmt es mir nicht übel falls ich etwas vergessen habe. Vielleicht hat einer von euch eine Idee, vielen Dank schonmal!
Gruß
Quizle
Anzeige