我正在使用C#for .NET 4.0(通过Visual Studio 2010)编写的项目上工作。有第三方工具需要使用C/C++ DLL,并且在C#中有32位应用程序和64位应用程序的示例。使用Environment.Is64BitProcess从c#应用程序动态调用32位或64位DLL
问题是,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();
}
谢谢!!