繁体   English   中英

部署后,应用无法找到非托管dll

[英]App fails to find unmanaged dll when deployed

我正在以任何CPU模式运行Web API Asp.Net 4.6应用程序(尽管我也尝试过指定x64),并且它会调用非托管x64 C dll。 当我在Visual Studio中运行时(使用默认的IIS Express设置),当我将Windows部署到另一台服务器并在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。

通过ikenread :需要检查非托管DLL的编译模式。 如果以Debug模式进行编译,则它将依赖Visual C ++ Redistributable dll的Debug版本,并且生产服务器上(最可能)将不存在它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM