【发布时间】: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,但无济于事。