gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:48:08 26 4
gpt4 key购买 nike

我将在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com