【发布时间】:2016-01-03 21:45:53
【问题描述】:
我正在编写一个依赖 C++ DLL 来管理设备的 C#/WPF 应用程序。
目前我正在测试根据该线程中的代码在它们之间设置回调接口:Howto implement callback interface from unmanaged DLL to .net app?(您可以在下面看到我的版本)
C#/WPF 项目正在编译为 Debug x64。 C++ DLL 编译为 Release x64。我还将 DLL 添加到项目目录和解决方案中作为参考。 C# 项目编译并运行,直到它到达函数“SetCallback”。然后,Visual Studio 将继续告诉我“System.DllNotFoundException”。这对我来说都是陌生的领域,因此非常感谢您的帮助。
C#代码:
namespace SomeNameSpace{
public partial class Class1
{
private delegate int Callback(string text);
private Callback m_Instance;
public Class1()
{
m_Instance = new Callback(Handler);
}
private int Handler(string txt)
{
Console.WriteLine(txt);
return 0;
}
public void set()
{
SetCallback(m_Instance);
}
public void test()
{
TestCallback();
}
[DllImport("WEyeLib.dll", EntryPoint = "SetCallback", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern void SetCallback(Callback fn);
[DllImport("WEyeLib.dll", EntryPoint = "TestCallback", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern void TestCallback();
}
C++ 代码:
命名空间 WEyeLib {
typedef int(__stdcall * Callback)(const char* chr);
Callback Handler = 0;
extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback _handler) {
Handler = _handler;
}
extern "C" __declspec(dllexport)
void __stdcall TestCallback() {
int retval = Handler("Hello World");
}
}
【问题讨论】: