Unable to load Win32 Native DLL file from C#.NET

2019-08-24 14:01发布

问题:

I have a C# winapp. I call a native .dll file (created in C++ by myself) from the C# app, and it works fine.

But when I copy my application (.exe and .dll files) to another machine, I get an error that says:

Unable to load DLL "c:\dllname.dll": The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Here is the C# code:

class IsoMessageHelper
{
    public const string ISO8583_DLL = "c:\\Hc8583.dll";
    [DllImport(ISO8583_DLL, CallingConvention = CallingConvention.Cdecl)]
    public static extern bool InitializationRq(...)
}

What should I do?

回答1:

A common issue when deploying .Net applications that have native dependencies, is that the native dlls may be missing dependencies themselves on the target machines e.g. the correct version of the C runtime.

Use a tool such a Dependency Walker to analyze your native dll and determine if it has a missing dependency on the machine you have copied it too.



回答2:

Try not to hard code any paths in the DllImport attribute parameter that specifies the name of the file. Then you should make usre the file is right besides the executable.

Something like this:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]


回答3:

Move the DLL to the root. If that works, then look at your attribute to determine why. You haven't posted any code, so I can't give you any specific reason.



标签: c# winapi dll