0

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");
    }
}
Community
  • 1
  • 1
Informat
  • 808
  • 2
  • 11
  • 25

2 Answers2

0

This means that the C# app is not able to find WEyeLib.dll. Try copying it next to the built .exe and see if that fixes it.

gregstoll
  • 1,318
  • 9
  • 14
  • actually when i build the project, it will automatically spawn a WEyeLib.dll in the same directory as the built .exe file – Informat Jan 03 '16 at 22:07
0

I had the same problem and using the Dependy Walker tool is responsible for finding the missing dll. In my case my dll uses code c ++ Microsoft c ++ Runtime uses library. For this reason I had to add two libraries needed: - Msvcp * .dll - Msvcr * .dll

With this I avoid even install the Microsoft C ++ Runtime Library on the client PC.