【问题标题】:the function LoadLibrary from kernel32.dll returns zero in Asp web applicationkernel32.dll 中的函数 LoadLibrary 在 Asp Web 应用程序中返回零
【发布时间】:2014-03-13 15:11:02
【问题描述】:

我将在 asp.net Web 应用程序中使用 kernel32 dll。这是代码: //DllGetClassObject函数指针签名 私有委托 int DllGetClassObject(ref Guid ClassId, ref Guid InterfaceId, [Out, MarshalAs(UnmanagedType.Interface)] out object ppunk);

//Some win32 methods to load\unload dlls and get a function pointer
private class Win32NativeMethods
{
  [DllImport("kernel32.dll", CharSet=CharSet.Ansi)]
  public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

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

  [DllImport("kernel32.dll")]
  public static extern IntPtr LoadLibrary(string lpFileName);
}
public string GetTheDllHandle (dllName)
{
    IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName); // the dllHandle=IntPtr.Zero
    return dllHandle.ToString();
}

当我调用我的函数GetTheDllHandle时,dllHandle返回零的问题

有没有人做过类似的东西?或者有人有什么建议吗?

【问题讨论】:

  • 可能是 dll 在您认为存在的地方不存在,可能是权利问题,也可能是完全不同的东西。调用 GetLastError 找出答案。
  • 我有同样的问题,调用new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message 给我的消息是“找不到指定的模块”。随后的调用实际上将返回“操作成功完成”,因为Marshal.GetLastWin32Error() 随后返回0 而不是126。调用System.IO.File.Exists(filePathToMyDll) 返回true 所以绝对不是文件路径不正确。我还以管理员身份运行 Visual Studio,但无济于事。

标签: c# asp.net


【解决方案1】:

在加载 DLL 时返回 0 有几个原因,例如,指定路径上不存在 DLL,或者平台不支持 DLL,或者在构建本机 DLL 之前未加载依赖的 dll。因此您可以通过将 SetLastError 属性设置为 true 来跟踪这些错误

DllImport("kernel32.dll", EntryPoint = "LoadLibrary", SetLastError = true)]
public static extern IntPtr LoadLibrary(string lpFileName);
public string GetTheDllHandle (dllName)
{
   IntPtr dllHandle = Win32NativeMethods.LoadLibrary(dllName); // the dllHandle=IntPtr.Zero

   if (dllHandle == IntPtr.Zero)
      return Marshal.GetLastWin32Error().ToString(); // Error Code while loading DLL
   else
      return dllHandle.ToString();  // Loading done !
}

【讨论】:

  • Marshal.GetLastWin32Error() return 0
  • SetLastError 对我没有任何改变 - 这里仍然是 0
猜你喜欢
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2015-01-30
  • 1970-01-01
相关资源
最近更新 更多