10

I built a C# application with WPF on Visual Studio 2012 that uses C++ DLL and targets .NET4.5. I have two projects running, one for the C# project and the other for C++ DLL project. I released both projects into a folder that has a .exe for C# and a .dll for C++ in the same folder.

I run them on my machine where they were developed and everything works fine. I run the .exe in other machines and it throws this exception:

System.DllNotFoundException: Unable to load DLL

It's not recognizing the DLL that is in the same folder.

I tried many things and nothing seems to work. I followed the solution in this post but nothing worked.

The Dev and Target machine are identical. In Dev, Visual Studio 2012 is installed, but that's the only difference.

Code:

C#:

[DllImport(@"Wireless.dll", EntryPoint = "?cert_exists@certificate@CertFuncs@@SAHHPBD@Z", CallingConvention = CallingConvention.Cdecl)]
    static extern int cert_exists(int store, [MarshalAs(UnmanagedType.LPTStr)]string cert_str);

C++:

static int __declspec(dllexport) cert_exists(int type, LPCSTR cert_str);

Update:

If I install Visual Studio 2012 on the target machine, everything works fine. If I remove it, the application crashes again. Any ideas on what VS is adding that can make the application work?

Community
  • 1
  • 1
Maher Manoubi
  • 585
  • 1
  • 6
  • 18

2 Answers2

11

Just a guess: install Microsoft Visual C++ 2012 Redistributable Package on the user's machine. The native DLL you use may have other dependencies which have to be installed (try Dependency Walker). If the native DLL requires for example registry settings, config files etc. these should also be present. It should be distributed to user machines the same way you installed it on the dev machine.

kol
  • 27,881
  • 12
  • 83
  • 120
  • I just did but still nothing, it's still not finding the DLL. I'm suspecting there is a protection on the dll that makes it only work only on dev machine, not really sure – Maher Manoubi Jan 29 '13 at 17:09
  • Hmm... I edited my answer, please check it out. Anyway, is the native DLL publicly available? – kol Jan 29 '13 at 17:18
  • I will try Dependency Walker and see what happens – Maher Manoubi Jan 29 '13 at 17:31
  • 1
    Walker was telling me that the app dll is targeting different CPU but that's not true because they have the same CPU – Maher Manoubi Jan 29 '13 at 18:20
  • I just installed Visual Studio in target machine and it worked so Visual Studio must be installing something that is required to run the project. I need to get it to work in target machines with no dependency with Visual Studio – Maher Manoubi Jan 29 '13 at 18:21
  • "the app dll is targeting different CPU" - Are you sure you don't accidentally have another version of the native DLL on the dev machine (1) in the system directory, (2) in the Windows directory, (3) in the current directory of the application, or (4) in one of the directories listed in the PATH environment variable? If not sure, then run a search please. – kol Jan 29 '13 at 18:53
  • Are you sure you installed the proper VC++2012 redistributable on the machine that did not have VS installed? Proper meaning - matches the CPU of your app AND matches the version of your VS install (whether or not you have VS2012 Update 1 or not). – pstrjds Jan 29 '13 at 18:56
  • What does Dependency Walker tell you on the *user* machine? Do you have all dependencies installed? – kol Jan 29 '13 at 19:11
  • Dependency Walker gives me a CPU mismatch error with DLL, now I'm trying to buid my dll with x64, I cannot find the option for all CPUs. When I switch to x64, I get System.BadImageFormatException – Maher Manoubi Jan 29 '13 at 19:21
  • 5
    YEEES. It's the Visual C++ Redistributable x86 version and not the x64 that fixes everything!! Basically, without that the os doesn't read x86 dlls. The only other way is to distribute the dll in x64 but that was giving me even bigger issues so I will have customers install the VC++ x86 . Thank you so much for the help :) I really appreciate. – Maher Manoubi Jan 29 '13 at 19:36
0

This is due to your project build configuration being "Debug"(Right click on the solution '' -> properties -> 'configuration properties -> You should see all the projects configuration set to 'Debug'). It requires visual studio installed wherever you are running the exe(which is built in "Debug" mode).

Inorder to avoid this, change the configuration to "Release" and rebuild your project.

rajkumaradass
  • 114
  • 1
  • 8