1

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.

Draken
  • 3,134
  • 13
  • 34
  • 54
Teja
  • 33
  • 1
  • 4
  • No, it is not a duplicate because there they are able to run successfully on the machine but not able to load it on a different Windows 2003 machine. In my case, I'm getting this exception in my computer when I run it using VS2015. – Teja Aug 05 '16 at 21:22
  • I would use the `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 changing `ExactSpelling` to false. Also, I don't think CallingConvention.Cdecl is right for a C++ DLL, I think you want StdCall. – Cody Aug 05 '16 at 22:10
  • 1
    Thanks @Cody. I used Dependency walker and found that it is not able to get some related dependencies. I added them to C:\Windows\System32 folder and added this in the 'Path' environment variable. Now I'm able to go past this error. Thanks so much. – Teja Aug 08 '16 at 01:01

1 Answers1

1

The unmanaged DLL needs to be locateable by your managed process. Typically that means placing the DLL in the same directory as the executable file. But you gave used an absolute path which I presume you transcribed correctly.

You may also encounter this error if the DLL's dependencies cannot be located. That seems the likely explanation here. Most likely the MSVC runtime cannot be located when your DLL is loaded.

Using an absolute path isn't a great idea. That will break down when you distribute to another machine. Use just the DLL file name and place it in the same directory as the executable.

Your DllImport attribute seems fussy. No point specifying CharSet when there is no text. I doubt your function calls SetLastError. And do you really need ExactSpelling?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I updated the DllImport line to - [DllImport("ctxmix.dll",CallingConvention =CallingConvention.Cdecl)] And also placed the cpp dll in the same location as exe. But I'm still getting the same error. I'm running this using VS2015. So, I assume it should take all the dependencies related to it right? Also, I don't see my CPP Dll in the Project References section. I wonder if that might be a problem? – Teja Aug 05 '16 at 21:09
  • No you don't need the DLL as a reference. Either the DLL or its dependencies cannot be found. – David Heffernan Aug 06 '16 at 04:23
  • Thanks @David. I used Dependency walker and found that it is not able to get some related dependencies. I added them to C:\Windows\System32 folder and added this in the 'Path' environment variable. Now I'm able to go past this error. Thanks so much. – Teja Aug 08 '16 at 01:00
  • That is the wrong solution. Don't modify system folder. – David Heffernan Aug 08 '16 at 06:03
  • I'm doing a server side development on my local machine. But, if I try running my exe on the server virtual machine, it is working fine.Ideally, the server will have those additional dlls in the System folder. Since mine is a local machine, I don't have those in my machine and I get the exception. So, I copied those additional .dll files to the System folder. – Teja Aug 08 '16 at 14:24
  • 1
    Wrong solution. Don't modify the system folder. Install the MSVC runtime, or place dependencies in the executable directory. – David Heffernan Aug 08 '16 at 14:37
  • Yeah I removed those dependencies from the System folder and installed necessary software for the additional dependencies in my local machine and it is working now. Thanks! – Teja Aug 08 '16 at 16:34