OK so there are a few common ways to check at runtime if your code is running on a 64 bit OS but each method I have seen is not 100% reliable. For example, checking the size of IntPtr (4 = 32 bit, 8 = 64 bit) is not a reliable way to see if the OS is 64 bit because your process could be running in 32 bit emulation mode on a 64 bit OS (aka Wow64) and therefore IntPtr would be 4 even though the OS is 64 bit. Another method is to check the PROCESSOR_ARCHITECTURE environmental variable but as a user can change environmental variables if they are so inclined then I dont count this as a fool proof way of checking either.
So, with .NET 4.0 Microsoft introduced the Is64BitOperatingSystem property to provide an easy way to check - which is great for those of us that can use .NET 4.0 in a production environment but in reality at the moment that is not very many people. So I have looked at the way this property works internally and copied it, making a few changes, for people to use in any version of .NET from 2.0 upwards.
So here is the definition of the property that you would check in your code:
vb.net Code:
''' <summary> ''' Determines whether or not the operating system is 64 bit ''' </summary> Public ReadOnly Property Is64BitOperatingSystem() As Boolean Get If IntPtr.Size = 8 Then Return True Else Dim Is64 As Boolean = False If DoesWin32MethodExist("kernel32.dll", "IsWow64Process") Then IsWow64Process(Process.GetCurrentProcess.Handle, Is64) End If Return Is64 End If End Get End Property
and here are the Windows API definitions that you will need to add to make that code work:
vb.net Code:
'API Definitions ''' <summary> ''' Determines if a process is running on a 64 bit Operating System but in 32 bit emulation mode (WOW64) ''' </summary> ''' <param name="hProcess">A handle to the process to check</param> ''' <param name="Wow64Process">Output parameter. A boolean that will be set to True if the process is running in WOW64 mode</param> <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="IsWow64Process")> _ Public Shared Function IsWow64Process(<System.Runtime.InteropServices.InAttribute()> ByVal hProcess As System.IntPtr, <System.Runtime.InteropServices.OutAttribute()> ByRef Wow64Process As Boolean) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean End Function ''' <summary> ''' Determines if a method exists in the specified DLL ''' </summary> ''' <param name="moduleName">The DLL to look for the method in</param> ''' <param name="methodName">The method to look for</param> Public Shared Function DoesWin32MethodExist(ByVal moduleName As String, ByVal methodName As String) As Boolean Dim moduleHandle As IntPtr = GetModuleHandle(moduleName) If (moduleHandle = IntPtr.Zero) Then Return False End If Return (GetProcAddress(moduleHandle, methodName) <> IntPtr.Zero) End Function ''' <summary> ''' Gets a handle to a specified DLL ''' </summary> ''' <param name="moduleName">The module to return a handle for</param> <Runtime.ConstrainedExecution.ReliabilityContract(Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, Runtime.ConstrainedExecution.Cer.MayFail), DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _ Public Shared Function GetModuleHandle(ByVal moduleName As String) As IntPtr End Function ''' <summary> ''' Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL) ''' </summary> ''' <param name="hModule">A handle to the DLL to look for the method in</param> ''' <param name="methodName">The method to look for</param> <DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _ Public Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal methodName As String) As IntPtr End Function
In case you are wondering, the reason for the code checking to see if the IsWow64Process method exists in kernel32.dll before calling it is because prior to XP SP2 this method did not exist.
Tested on Windows 7 (64 bit) and Windows XP (32 bit) and worked perfectly on both.
Of course any corrections or suggestions feel free to post them hereHope it helps someone out.
Chris




Hope it helps someone out.
Reply With Quote