I am writing a C#/WPF application that will rely on a C++ DLL to manage a device.
Currently I am testing to setup a Callback interface between them based on the code from this thread: Howto implement callback interface from unmanaged DLL to .net app? (You can see my version below)
The C#/WPF project is being compiled as Debug x64. The C++ DLL is compiled as Release x64. I've also added the DLL into the project directory and into the solution as a reference. The C# project compiles and runs until it reaches the function "SetCallback". Visual studio will then proceed to tell me "System.DllNotFoundException". This is all unfamiliar territory for me so help is much appreciated.
C# Code:
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++ Code:
namespace 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");
}
}