I’m having some difficulties while trying to consume an unmanaged-code dll
from my application (written in C# framework 4.0).
I’m using the dll
import as follows
[DllImport(@"C:\MGW_SDK.dll", EntryPoint = "fInicializaSDK")]
public static extern int fInicializaSDK();
The weird thing is that when called from my development environment (Windows XP) it works just fine, but when on the production server (Windows7) it generates the following exception:
System.DllNotFoundException
: Unable to load DLL' C:\MGW_SDK.dll'
: The specified module could not be found. (Exception from HRESULT: 0x8007007E) atComtpaq.AdminPack.SDKWrapper.fInicializaSDK()
atComtpaq.AdminPack.AdminPaqRepository.InitializeSDK()
inC:\Work\AgroIn\AdminPaqRepository.cs
:line 30
I already ran Dependency Walker, which shows the following dependency as conflicted: C:\windows\system32\SHLWAPI.DLL
I already tried copying that dll
to my deployment directory
The MGW_SDK.dll
is on c:\
(as the path says)
I already installed C Runtime Library
I also noticed some interesting stuff: on the exception, the development path is showing C:\Work\AgroIn\AdminPaqRepository.cs:line 30
What else could be the problem here?
Edit
I built a small console application with just one of the extern
/dllimport
methods that I use from MGW_SDK.dll
and ran it with no problem (no DllNotFoundException
whatsoever), so this seems to be related to the application that I'm building (a WCF Service). This service is hosted on the WAS, but I'm still unable to find the issue.
The root of C is one of the places UAC will not let you write. Sometimes unmanifested apps that ask to read from a protected area end up reading from the virtualized equivalent - and your DLL will not be there. Try moving the DLL to a different folder (not the root of C, not anything under Program Files) and see if that solves it. If it does, you can leave it there or (better) put a manifest on your app to prevent virtualization.
Have you tried using Assembly Binding Log Viewer?
Don't know if this will work for sure but have you tried registering MGW_SDK.DLL using regsrv32 or other installation package on Win7 box?
Also try to find out if the dependencies of MGW_SDK.DLL are also available on Win7 machine (either registered on system or in your application folder will work)
Well, this was a very tricky problem. It was not related to dll dependencies and not related to the dll location either. It was not about how I was calling the
DllImport
. It was a permissions problem.Basically this issue is solved by taking out and then adding back the permissions for execute on the folder where the dlls are. See this article (Wayback machine).