I'm trying to import a C++ Project Dll into a C# project. I found a lot of people talk about using DllImport. I tried using that and here is what I have-
CPP Code:
int __declspec(dllexport) beginCode(double reportId); C# Code:
[DllImport("C:\\Users\\<my_user_id>\\Desktop\\ctxmix\\Release\\ctxmix.dll",CallingConvention =CallingConvention.Cdecl ,CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern int beginCode(double reportId); int result = beginCode(reportId); But when I run, I'm getting an exception - Exception thrown:
System.DllNotFoundException
Do I have to add any references for the CPP Dll in the project or do anything else apart from the code which I have on the top?
Edit: I'm trying to run my .exe using VS2015 and I get this exception on my local machine. Also, I don't see my CPP Dll in the Project->References section where as I see other references there.
dumpbin /exports [your dll here]command (Included in the visual studio command prompt) to see what the exported function name actually is. Odds are, the C++ compiler is name-mangling beginCode to something else, so it can't find the function. You might be able to fix this by changingExactSpellingto false. Also, I don't think CallingConvention.Cdecl is right for a C++ DLL, I think you want StdCall.