Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.DllNotFoundException: Unable to load DLL. No errors in Dev machine but error in target machines

Tags:

c#

windows

dll

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?

like image 933
Maher Manoubi Avatar asked Jan 29 '13 16:01

Maher Manoubi


1 Answers

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.

like image 130
kol Avatar answered Sep 24 '22 20:09

kol