Results 1 to 5 of 5

Thread: Determine if your code is running on a 64 bit OS

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. ''' <summary>
    2. ''' Determines whether or not the operating system is 64 bit
    3. ''' </summary>
    4.     Public ReadOnly Property Is64BitOperatingSystem() As Boolean
    5.         Get
    6.             If IntPtr.Size = 8 Then
    7.                 Return True
    8.             Else
    9.                 Dim Is64 As Boolean = False
    10.                 If DoesWin32MethodExist("kernel32.dll", "IsWow64Process") Then
    11.                     IsWow64Process(Process.GetCurrentProcess.Handle, Is64)
    12.                 End If
    13.                 Return Is64
    14.             End If
    15.         End Get
    16.     End Property

    and here are the Windows API definitions that you will need to add to make that code work:
    vb.net Code:
    1. 'API Definitions
    2.  
    3.     ''' <summary>
    4.     ''' Determines if a process is running on a 64 bit Operating System but in 32 bit emulation mode (WOW64)
    5.     ''' </summary>
    6.     ''' <param name="hProcess">A handle to the process to check</param>
    7.     ''' <param name="Wow64Process">Output parameter. A boolean that will be set to True if the process is running in WOW64 mode</param>
    8.     <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="IsWow64Process")> _
    9.     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
    10.     End Function
    11.  
    12.     ''' <summary>
    13.     ''' Determines if a method exists in the specified DLL
    14.     ''' </summary>
    15.     ''' <param name="moduleName">The DLL to look for the method in</param>
    16.     ''' <param name="methodName">The method to look for</param>
    17.     Public Shared Function DoesWin32MethodExist(ByVal moduleName As String, ByVal methodName As String) As Boolean
    18.         Dim moduleHandle As IntPtr = GetModuleHandle(moduleName)
    19.         If (moduleHandle = IntPtr.Zero) Then
    20.             Return False
    21.         End If
    22.         Return (GetProcAddress(moduleHandle, methodName) <> IntPtr.Zero)
    23.     End Function
    24.  
    25.     ''' <summary>
    26.     ''' Gets a handle to a specified DLL
    27.     ''' </summary>
    28.     ''' <param name="moduleName">The module to return a handle for</param>
    29.     <Runtime.ConstrainedExecution.ReliabilityContract(Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, Runtime.ConstrainedExecution.Cer.MayFail), DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    30.     Public Shared Function GetModuleHandle(ByVal moduleName As String) As IntPtr
    31.     End Function
    32.  
    33.     ''' <summary>
    34.     ''' Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL)
    35.     ''' </summary>
    36.     ''' <param name="hModule">A handle to the DLL to look for the method in</param>
    37.     ''' <param name="methodName">The method to look for</param>
    38.     <DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    39.     Public Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal methodName As String) As IntPtr
    40.     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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Determine if your code is running on a 64 bit OS

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    New Member
    Join Date
    Sep 2010
    Posts
    2

    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

  4. #4
    New Member
    Join Date
    Sep 2010
    Posts
    2

    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:
    1. If Directory.Exists("C:\Program Files (x86)") Then
    2.                 Shell("msiexec.exe /i " & Chr(34) & "\\server\Install Packages\Install Menu\CorporateMenu_Install64.msi" & Chr(34) & " /quiet", AppWinStyle.NormalFocus, True)
    3.             Else
    4.                 Shell("msiexec.exe /i " & Chr(34) & "\\server\Install Packages\Install Menu\CorporateMenu_Install32.msi" & Chr(34) & " /quiet", AppWinStyle.NormalFocus, True)
    5.             End If

  5. #5

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width