I have a C# application and a C++ DLL, both x86. The Application is the startup project, the DLL project is inside the same solution and is referenced as a project. The C++ DLL outputs its PDB file with the same name as the DLL inside the Debug folder of the Application.
I have a function called SomeFunction
that I'm trying to execute in C#. When the code reaches that line, it stops there. The UI of the C# application continues to be responsive, but the breakpoint never leaves that line (and that's in Form_Load).
If I try to set the DLL as startup project and tell it to execute the C# application, then C# will crash at that line with: System.EntryPointNotFoundException: Unable to find an entry point named 'SomeFunction' in DLL 'SomeDLL.dll'.
This is the declaration of the function that I'm trying to call:
[DllImport("SomeDLL.dll", EntryPoint = "SomeFunction", CallingConvention = CallingConvention.Cdecl)]
public static extern int SomeFunction(IntPtr hwnd);
This is the function's declaration from the C++ header:
#define MYDLL_API __declspec(dllexport)
MYDLL_API extern int SomeFunction(HWND hWnd);
This is how I call it from C#:
var someAnswer = SomeFunction(_hwnd);
UPDATE: since the discussion below could take some time to read, here's the answer in the nutshell: I was missing an extern "C". Also, in order to debug the DLL (which was also a problem), the project needs to support native debugging both from C++ and C#, here's an excellent list that I went through and at the end it was all good!
SomeFunction
to ensure unmanaged .dll is loadedsymbols
status. You can try to load symbols
in context menu of your c++ dll in that window.(very often happens that .net loads different (not expected) version of unmanaged .dll)
Now I'm pretty sure that the problem is that your exported function name is mangled. Try to define function like this:
extern "C" MYDLL_API int PASCAL SomeFunction(HWND hWnd);
In C#:
[DllImport("SomeDLL.dll")]
public static extern int SomeFunction(IntPtr hwnd);