【发布时间】:2017-09-11 23:27:51
【问题描述】:
我正在开发一个用 C# 为 .NET 4.0 编写的项目(通过 Visual Studio 2010)。有一个需要使用 C/C++ DLL 的第 3 方工具,并且有 C# 中 32 位应用程序和 64 位应用程序的示例。
问题在于 32 位演示静态链接到 32 位 DLL,而 64 位演示静态链接到 64 位 DLL。作为一个 .NET 应用程序,它可以在客户端 PC 上作为 32 位或 64 位进程运行。
.NET 4.0 框架提供 Environment.Is64BitProcess 属性,如果应用程序作为 64 位进程运行,则返回 true。
我想做的是在检查 Is64BitProcess 属性后动态加载正确的 DLL。但是,当我研究动态加载库时,我总是会想到以下内容:
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
这些方法似乎专门针对 32 位操作系统。是否有 64 位等效项?
只要基于 Is64BitProcess 检查调用适当的方法,静态链接 32 位和 64 位库是否会导致问题?
public class key32
{
[DllImport("KEYDLL32.DLL", CharSet = CharSet.Auto)]
private static extern uint KFUNC(int arg1, int arg2, int arg3, int arg4);
public static bool IsValid()
{
... calls KFUNC() ...
}
}
public class key64
{
[DllImport("KEYDLL64.DLL", CharSet = CharSet.Auto)]
private static extern uint KFUNC(int arg1, int arg2, int arg3, int arg4);
public static bool IsValid()
{
... calls KFUNC() ...
}
}
...
if (Environment.Is64BitProcess)
{
Key64.IsValid();
}
else
{
Key32.IsValid();
}
谢谢!!
【问题讨论】: