I am working on a project written in C # for .NET 4.0 (through Visual Studio 2010). There is a third-party tool that requires the use of a C / C ++ DLL, and there are examples for 32-bit applications and 64-bit applications in C #.
The problem is that the 32-bit demo is statically linked to the 32-bit DLL, and the 64-bit demo is statically referenced to the 64-bit DLL. Being a .NET application, it can be launched either as a 32-bit or 64-bit process on client PCs.
The .NET 4.0 framework provides the Environment.Is64BitProcess property, which returns true if the application is running as a 64-bit process.
What I would like to do is dynamically load the correct DLL after checking the Is64BitProcess property. However, when I explore dynamic library loading, I always come up with the following:
[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);
These methods appear to be specifically designed for the 32-bit operating system. Are there 64-bit equivalents?
Can a problem statically link both 32-bit and 64-bit libraries if the corresponding methods are called based on the Is64BitProcess check?
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(); }
Thanks!