我正在使用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();
}
謝謝!!