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

2019-08-24 13:34发布

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?

标签: c# winapi dll
3条回答
该账号已被封号
2楼-- · 2019-08-24 14:15

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.

查看更多
贪生不怕死
3楼-- · 2019-08-24 14:17

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.

查看更多
我命由我不由天
4楼-- · 2019-08-24 14:21

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)]
查看更多
登录 后发表回答