c# - kernel32.dll 中的 LoadLibrary 函数在 Asp Web 应用程序中返回零

标签 c# asp.net

我将在 asp.net web 应用程序中使用 kernel32 dll。这是代码: //DllGetClassObject函数指针签名 private delegate 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 时返回 0 由于多种原因,例如,DLL 不在指定路径上或平台不支持 DLL,或者在构建 native 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 !
}

关于c# - kernel32.dll 中的 LoadLibrary 函数在 Asp Web 应用程序中返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382938/

相关文章:

c# - ASP.NET MVC2 项目的 DDD 架构

c# - 网络表单 MVP : What is presenters responsibility

c# - Outlook 2010 VSTO 加载项 : UI freezes randomly,,同时将文件夹异步添加到 PST

c# - DataGridView 只读 "bug"

c# - 使用 caSTLe-windsor 的 MVC4 中 HttpClient 的生活方式

asp.net - 如何使用 IIS 授予 IIS APPPOOL\DefaultAppPool 对 Asp.net 中存储过程的访问权限?

asp.net - Mono 是 ASP.NET 的可行替代品吗?

c# - 格式化 - 添加逗号,2 位小数到单元格值 vb.net excel interop

c# - 我可以在 IIS 之外托管 ASP.NET 网站吗?

c# - 打印按钮在 ReportViewer 中不可见