无法从 C#.NET 加载 Win32 本机 DLL 文件
我有一个 C# winapp。我从 C# 应用程序调用本机 .dll 文件(我自己用 C++ 创建),并且运行良好。
但是,当我将应用程序(.exe 和 .dll 文件)复制到另一台计算机时,出现错误:
无法加载 DLL“c:\dllname.dll”:找不到指定的模块。 (HRESULT 异常:0x8007007E)
这是 C# 代码:
class IsoMessageHelper
{
public const string ISO8583_DLL = "c:\\Hc8583.dll";
[DllImport(ISO8583_DLL, CallingConvention = CallingConvention.Cdecl)]
public static extern bool InitializationRq(...)
}
我应该做什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
部署具有本机依赖项的 .Net 应用程序时的一个常见问题是本机 dll 可能缺少目标计算机上的依赖项本身,例如 C 运行时的正确版本。
使用诸如 Dependency Walker 之类的工具来分析您的本机 dll 并确定它是否缺少对您的计算机的依赖项也复制了。
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.
尽量不要在指定文件名的
DllImport
属性参数中硬编码任何路径。然后你应该使 usr 文件与可执行文件一起正确。像这样的事情:
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:
将 DLL 移至根目录。如果有效,请查看您的属性以确定原因。您还没有发布任何代码,所以我无法给您任何具体原因。
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.