无法从C#.NET加载Win32本机DLL文件[英] Unable to load Win32 Native DLL file from C#.NET

本文是小编为大家收集整理的关于无法从C#.NET加载Win32本机DLL文件的处理方法,想解了无法从C#.NET加载Win32本机DLL文件的问题怎么解决?无法从C#.NET加载Win32本机DLL文件问题的解决办法?无法从C#.NET加载Win32本机DLL文件问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我有一个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(...)
}

我该怎么办?

推荐答案

部署具有本机依赖性的.NET应用程序时,一个常见的问题是,本机DLL可能缺少依赖关系本身,例如C运行时的正确版本.

使用一种工具这样的工具 dependencency Walker 分析您的本地dll并确定它是否缺少对您也已复制的机器.

其他推荐答案

尽量不要硬编码DllImport属性参数中指定文件名称的任何路径.然后,您应该让USRE文件除了可执行文件外.

类似的东西:

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

其他推荐答案

将DLL移至根.如果有效,请查看您的属性以确定原因.您没有发布任何代码,所以我不能给您任何具体原因.

本文地址:https://www.itbaoku.cn/post/645700.html

问题描述

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?

推荐答案

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.

其他推荐答案

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)]

其他推荐答案

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.

查看更多