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.

3
  • 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. Commented Aug 5, 2016 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. Commented Aug 5, 2016 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. Commented Aug 8, 2016 at 1:01

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

7 Comments

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?
No you don't need the DLL as a reference. Either the DLL or its dependencies cannot be found.
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.
That is the wrong solution. Don't modify system folder.
Wrong solution. Don't modify the system folder. Install the MSVC runtime, or place dependencies in the executable directory.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.