【问题标题】:App fails to find unmanaged dll when deployed应用程序在部署时无法找到非托管 dll
【发布时间】:2016-11-25 05:07:50
【问题描述】:

我正在运行一个在任何 CPU 模式下运行的 Web API Asp.Net 4.6 应用程序(尽管我也尝试过指定 x64),它调用了一个非托管 x64 C dll。这在 Visual Studio 中运行时(使用默认 IIS Express 设置)运行良好,但当我部署到另一台服务器并在 IIS 或 IIS Express 中运行时出现 Windows 错误 126(找不到指定的模块。)即使我确信DLL 的路径是正确的。还有什么我可以尝试的吗?

我的原生方法包装器:

public static class NativeMethods
{
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);

    [DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
    public static extern IntPtr GetModuleHandle(string lpModuleName);

    [DllImport("kernel32.dll")]
    public static extern uint GetLastError();
}

我的加载 DLL 方法:

private static void LoadDll()
    {
        UnloadDLL(); //Unload the module first

        if (string.IsNullOrEmpty(DllDirectory))
            throw new ApplicationException("DPI DLL directory not specified.");

        if (!File.Exists(DllPath))
            throw new ApplicationException("Could not find DPI DLL.");

        pDll = NativeMethods.LoadLibrary(DllPath);
        //Fails Here
        if (pDll == IntPtr.Zero)
            throw new ApplicationException(string.Format("Failed to load library <{0}> (ErrorCode: {1})", DllPath, Marshal.GetLastWin32Error())); 
    }

如果我能说得更清楚一些,请告诉我,谢谢!

【问题讨论】:

  • 使用Dependency Walker 获取模块的依赖列表(路径为DllPath 的模块)。还要检查您是否需要在服务器上安装 Visual C++ Redistributable。
  • @ArtavazdBalayan 谢谢!!我过去使用过 Dependency Walker,但没想到在这里使用它。事实证明,DLL 是在调试模式下编译的,并且正在寻找显然不在生产服务器上的 DLL 的调试版本。如果您想重新发布作为答案,我很乐意接受。再次感谢。

标签: c# asp.net iis dll unmanaged


【解决方案1】:

使用Dependency Walker 获取模块的依赖项列表(路径为 DllPath 的模块)。还要检查您是否需要在服务器上安装 Visual C++ Redistributable。

By ikenread:需要检查非托管DLL编译模式。如果它在调试模式下编译,它将依赖于 Visual C++ Redistributable dll 的调试版本,并且它们在生产服务器上将不存在(很可能)。

【讨论】:

    猜你喜欢
    • 2015-10-02
    • 2019-08-15
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2015-02-15
    相关资源
    最近更新 更多