Ich habe eine delphi dll zu verwenden, das einige Verfahren Rückruf zur Verfügung stellt. Wenn ich sie mit C# verwende, funktioniert alles. Wenn ich C++ benutze, funktionieren die Callbacks nicht. In den delphi dll der Rückruf wie geschrieben steht: Alles funktioniertmit Rückruf von delphi dll mit C++
procedure addConnectionCallBack(connectCallback: TConnectCallback); StdCall;
begin
initMyConnection();
if assigned(MyConnection) then
begin
MyConnection.addConnectionCallBack(connectCallback);
end;
end;
wenn C#:
// make delegate
public delegate void ConnectionCallBack();
// define dll
[DllImport(_dll_name, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern void addConnectionCallBack(ConnectionCallBack ccb);
// function with signature of ConnectionCallBack
private void showConnected() {
Console.WriteLine("connected");
}
// address callback to dll
public void start() {
addConnectionCallBack(showConnected);
}
In C++ ich es nicht lösen kann:
typedef void (__stdcall *ConnectionCallBack)();
typedef void (__stdcall *addConnectionCallBack)(ConnectionCallBack);
addConnectionCallBack _addConnectionCallBack;
// !!! this should be called from delphi dll, but isn't !!!
void __stdcall showConnected() {
std::cout << "connected" << std::endl;
}
//auto showConnected = []()->void {std::cout << "connected" << std::endl; };
int main()
{
LPCWSTR _dll_name = L"MyDelphi.dll";
HINSTANCE _hModule = NULL;
_hModule = LoadLibrary(_dll_name);
assert(_hModule != NULL);
_addConnectionCallBack = (addConnectionCallBack) GetProcAddress(_hModule, "addConnectionCallBack");
ConnectionCallBack conn = showConnected;
_addConnectionCallBack(conn);
// do some other dll calls which work and force the callback.
FreeLibrary(_hModule);
return 0;
}
Andere Anrufe der DLL, die eine Zeichenfolge zurückgeben, funktionieren. Versucht, Funktionszeiger auf verschiedene Arten oder std::function
/std::bind
ohne Glück versucht.
Bitte kann jemand meinen C++ Code überprüfen und mir einen Hinweis geben! Ich habe keine Ideen mehr.
* Wenn ich C++ benutze, funktionieren die Callbacks nicht. * - Bitte erläutern Sie genauer, was Sie mit "nicht arbeiten" meinen. Das Programm stürzt ab? Eine andere Funktion wird aufgerufen? Keine Funktion wird aufgerufen? – PaulMcKenzie
In der C++ Lösung wird die Funktion showConnected() nie aufgerufen. In der C# wird es. – solarisx
Die Frage lautet nun: Was macht der Delphi-Code? Was ist der Wert dieses Rückrufs? Der C++ - Code wird wahrscheinlich nicht aufgerufen, weil Delphi ihn nicht aufruft. 'if Assigned (connectedCallback) then' - und was ist, wenn dieses Ergebnis' false' ist? – PaulMcKenzie