使用来自 delphi dll 的回调与 C++
using callback from delphi dll with c++
我必须使用 delphi dll,它提供了一些回调程序。如果我将它们与 C# 一起使用,一切正常。如果我使用 C++,回调将不起作用。
在 delphi dll 中,回调是这样写的:
procedure addConnectionCallBack(connectCallback: TConnectCallback); StdCall;
begin
initMyConnection();
if assigned(MyConnection) then
begin
MyConnection.addConnectionCallBack(connectCallback);
end;
end;
使用 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);
}
在C++中我无法解决:
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;
}
对 dll 的其他调用 return 一个字符串正在工作。尝试以各种方式使用函数指针或 std::function
/std::bind
但没有成功。
请有人检查我的 C++ 代码并给我提示!我已经没有想法了。
我通过使用 Visual Studion Win32 项目模板或者更确切地说是将 C++ 主函数修改为:
解决了这个问题
int CALLBACK WinMain( <em>In</em> HINSTANCE hInstance, <em>In</em> HINSTANCE hPrevInstance, <em>In</em> LPSTR lpCmdLine, <em>In</em> int nCmdShow) {}
这使 dll 的进程保持活动状态。
感谢大家的帮助。
我必须使用 delphi dll,它提供了一些回调程序。如果我将它们与 C# 一起使用,一切正常。如果我使用 C++,回调将不起作用。 在 delphi dll 中,回调是这样写的:
procedure addConnectionCallBack(connectCallback: TConnectCallback); StdCall;
begin
initMyConnection();
if assigned(MyConnection) then
begin
MyConnection.addConnectionCallBack(connectCallback);
end;
end;
使用 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);
}
在C++中我无法解决:
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;
}
对 dll 的其他调用 return 一个字符串正在工作。尝试以各种方式使用函数指针或 std::function
/std::bind
但没有成功。
请有人检查我的 C++ 代码并给我提示!我已经没有想法了。
我通过使用 Visual Studion Win32 项目模板或者更确切地说是将 C++ 主函数修改为:
解决了这个问题int CALLBACK WinMain( <em>In</em> HINSTANCE hInstance, <em>In</em> HINSTANCE hPrevInstance, <em>In</em> LPSTR lpCmdLine, <em>In</em> int nCmdShow) {}
这使 dll 的进程保持活动状态。
感谢大家的帮助。