|
-
May 6th, 2010, 02:57 PM
#1
Determine if your code is running on a 64 bit OS
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 here Hope it helps someone out.
Chris
-
May 31st, 2010, 11:29 AM
#2
Re: Determine if your code is running on a 64 bit OS
-
Sep 1st, 2010, 02:24 PM
#3
New Member
Re: Determine if your code is running on a 64 bit OS
Why not just check to see if the Program Files (x86) directory exists? That's what I've been doing and it works for me:
Code:
If Directory.Exists("C:\Program Files (x86)") Then
Shell("msiexec.exe /i " & Chr(34) & "\\server\Install Packages\Install Menu\CorporateMenu_Install64.msi" & Chr(34) & " /quiet", AppWinStyle.NormalFocus, True)
Else
Shell("msiexec.exe /i " & Chr(34) & "\\server\Install Packages\Install Menu\CorporateMenu_Install32.msi" & Chr(34) & " /quiet", AppWinStyle.NormalFocus, True)
End If
-
Sep 1st, 2010, 02:25 PM
#4
New Member
Re: Determine if your code is running on a 64 bit OS
Why not just check to see if the Program Files (x86) directory exists? That's what I've been doing and it works for me:
VBCode Code:
If Directory.Exists("C:\Program Files (x86)") Then
Shell("msiexec.exe /i " & Chr(34) & "\\server\Install Packages\Install Menu\CorporateMenu_Install64.msi" & Chr(34) & " /quiet", AppWinStyle.NormalFocus, True)
Else
Shell("msiexec.exe /i " & Chr(34) & "\\server\Install Packages\Install Menu\CorporateMenu_Install32.msi" & Chr(34) & " /quiet", AppWinStyle.NormalFocus, True)
End If
-
Sep 1st, 2010, 03:04 PM
#5
Re: Determine if your code is running on a 64 bit OS
Two problems with your code example there.
1. What if the user didn't install Windows on the C drive? I know that is the default but it is possible to install it to another drive letter.
2. The user could have renamed Program Files (x86) on a 64 bit OS or could have created a folder named Program Files (x86) even though they are on a 32 bit OS.
I know all of these things are unlikely, but personally I would rather write robust software that will work 100% of the time rather than 95% of the time, even if it means a bit more work.
Last edited by chris128; Aug 15th, 2013 at 11:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|