How to Check if the Operating System is 32 Bit or 64 Bit using VB.NET, VB 2010, and Newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.00 out of 5)
 

I’ve seen many questions posted about how to check if the operating system the user is running is 64 Bit supported or just 32-Bit capable. 32 Bit is also referred to as x86 and 64-Bit is referred to as x64.

Update: October 19th 2010 – I decided to go ahead and add the simple code for checking OS Addressing to a new method if you use Visual Basic .NET 2010 and VB 2013.

Update 02/28/2011: I want to mention that I have another article for checking if the OS is 32bit/64bit, but also if your application is 32-bit/64-bit at this link here.


For Visual Basic.NET 2010 Only…

 Environment.Is64BitOperatingSystem

That property was added with the .NET Framework 4.0. Simply get the value from that property like below…

MsgBox(Environment.Is64BitOperatingSystem)

The property is a Boolean so it will return True if the Operating System is 64-Bit, otherwise it will return false.
If your not using VB 2010, 2013, and higher then continue reading the article…


Visual Basic.NET Older than version 2010

I’ve seen various options people have come up with like checking WOW64 emulation , ect… I’m not going to get into all of them. From what I understand those methods may not be 100%. So far in MY EXPERIENCE my version has been 100% when used with Vista and Windows 7. That is NOT to say that its perfect or has no flaws. Its just I have not had any problems, yet…

Anyways, its a very simple solution. Use the Windows built-in WMI (Windows Management Instrumentation) objects and features. Using WMI is quite easy with .NET. I unfortunately won’t be providing VB 5 or Visual Basic 6.0 source code. But the basics are still the same. I think VB classic can get support by referencing the WMI Scripting Library or using some API’s.

The apparent downside is that Microsoft says this wmi method is not supported on Windows XP. You can use environment variables and getnativesysteminfo api to query the os addressing. This wmi method so far appears to work just fine using Vista and Windows 7. 64 Bit operating systems became VERY popular with Vista, and especially Windows 7 due to ram being so cheap and if you wanted to address more than the 32 Bit limit of 4GB you needed to have an OS that can do 64 Bit addressing and a cpu capable of using AMD’s 64 Bit extensions. Intel CPU’s actually use AMD64 technology for their 64 Bit processors (Less Itanium)).

First of all in DotNET you want to add a Reference to two classes under: System.Managment. This will provide features to access wmi classes and features with minimal coding on your part. Of interest is the Management Class and the Management Object.

Below is the complete WMI based code that is used to get the Windows info…

    '
    'This function uses the WMI method to see if the operating system return 32-bit or 64-bit.
    Public Function getWMIInfo(ByVal wmiObjectInfo As String, ByVal wmiRelativePath As String) As String

        Try
            '
            'Give it something to report in case something wrong happens.
            getWMIInfo = "Nothing!" '

            Dim wmiClass As New System.Management.ManagementClass

            Dim wmiObject As New System.Management.ManagementObject

            wmiClass.Path.RelativePath = wmiRelativePath
            '
            'This will go through each item in the requested info. I only care about
            'the 1st for the most part but remeber that each instance could have different values.
            For Each wmiObject In wmiClass.GetInstances

                getWMIInfo = (wmiObject(wmiObjectInfo))
                '
                'I only want the first instance.

                Return getWMIInfo

            Next

        Catch exc As Exception

            Return exc.Message

        End Try

    End Function

Now all thats left to do is provide the object and path your interested in. The code below will throw a Messagebox with the message “32-bit” if your OS is x86 or “64-bit” if your OS is x64.

        '
        'Supply the WMI Object and Path to get whether the os is 32-bit or 64-bit.
        MsgBox(getWMIInfo("OSArchitecture", "Win32_OperatingSystem"))


That’s all there is to it! Like I said, so far its worked fine for me. Thats not to say there won’t be problems though. I will upload a new example I made at my vbcodesource.com website that shows how to get the Operating System and Applications addressing whethers its 32 Bit or 64 Bit that works with Windows XP, Vista, and Windows 7. Definitely, Let me know of any problems you run into using the source code in this article! Anyways have fun!

Jason

Revised: 2015

15 thoughts on “How to Check if the Operating System is 32 Bit or 64 Bit using VB.NET, VB 2010, and Newer

  1. Dave W

    One thing to watch out for is that older operating systems do not have the OSArchitecture property. From the msdn site:-

    “Windows Server 2003, Windows 2000, Windows NT 4.0, Windows XP, and Windows Me/98/95: This property is not available.”

    I don’t know if this means it might be available when these happen to be 64 bit (e.g. Windows Server 2003 can be 64 bit but I haven’t been able to test on one that is) or if it is not available ever on these operating systems. If it is not available at all, I don’t know how you can determine whether one of these operating systems is 64-bit.

    Reply
  2. Jason Post author

    Hi Dave, your right about older os like XP. I need to update the article.

    If the os is WinXP for example, a good way to see if it is 64 or 32 Bit is by checking a common Environment Variable. Below is some code that works;

    Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”)

    If the OS is 32 Bit then that code will return x86. If it is 64 Bit then is would return AMD64.

    At least that has been my experience. If the wmi way returns a blank string then you can have it execute some secondary code to check the environment variable. Or if the OS is detected as XP then just go straight to the environment variable based code.

    Anyways, thanks for reminding me about XP and checking if 64 Bit. I’m going to update when I get the chance.

    Jason

    Reply
  3. Carsten Giesen

    Hi Jason,

    sorry you are not right.
    I use
    Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”)
    in a x86 compiled Application on a x64 Windows 7 premium and get
    x86
    as result!

    Other ideas?
    cu
    Carsten

    Reply
  4. Jason Post author

    I tried the environment variable method and the wmi method and they both worked perfectly fine in Vista 64 and Windows 7 64bit. Both ways returned the expected values. The envorionment variable method returned AMD64 and the WMI way returned 64-bit.

    I’m not sure why your getting different results.

    Also, if your using VB 2010 Microsoft added a property that will return whether the os is 64bit or 32 bit.

    Reply
  5. Ridge Anderson

    Hi. Will this work on Visual Basic 2010?

    Is it possible for you to upload the VB file for this whole thing so that i can check it?
    There seems to be something wrong with my VB 2010, keeps on giving weird errors.

    Reply
  6. Jason Post author

    Hi, you should go ahead and use the built-in property if your using VB 2010 instead of the wmi codes in this article. Simply use the code below…

    MsgBox(Environment.Is64BitOperatingSystem)

    So if the OS is 64 Bit then Environment.Is64BitOperatingSystem will return True. Otherwise False.

    That should do it for you. I am going to go ahead and update the article with this info.

    Jason

    Reply
  7. Russ

    When using the environment variables, you must specify that you want the variables for the machine and not the current process to get the correct OS architecture.

    Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”, EnvironmentVariableTarget.Machine)

    Reply
  8. Jason Post author

    You are correct. I should have mentioned that. I actually have a VB 2005 example ready that shows 6 different ways to determine the os level and process level for checking for 32 bit and 64 bit addressing architecture. I’ve just been to lazy to put it up. I will see about putting a link on this article to download it.

    I actually have a fully updated version of this post but I haven’t made it public yet.

    Thanks for clarifying the environment variable method. Its definitely something I should have mentioned before. 🙂

    Jason

    Reply
  9. Rizky Aqew

    Dim wmiClass As New System.Management.ManagementClass
    Dim wmiObject As New System.Management.ManagementObject

    two error. not define ..

    thanks

    Reply
  10. Jason D

    Thanks for the help! Works great. Do you by chance have a link to the Operating System and Applications addressing whethers its 32 Bit or 64 Bit that works with Windows XP, Vista, and Windows 7 code you mentioned in your article?

    Reply
  11. Pete

    OK… this might be a little simple of me, but I just check to see if “Program Files (x86)” exists. If it does, then it’s a 64 bit OS. Really easy, been using it since XP64.

    Reply
  12. Jason Post author

    Hi Pete, if you read my comment just above yours that I made alittle over a year ago, it has a link to another article I made overviewing an example program I made which uses the method you just mentioned and many more. 🙂

    There is a big limitation though! Checking for the (x86) will Only 100% work to detect whether your program is running in 32-bit or 64-bit mode. Its NOT 100% to check if the OS is 64-bit or not.

    To see what I mean, is set your program to run in x86 (32-bit) mode instead of All CPU’s or x64. Then use your program to check if the Program Files(x86) path exists. What you will find out is that it will say the path does NOT exist even though it truly does exist. So the Program Files(x86) method will not be 100% correct in detecting if the operating system is 32-bit or 64-bit unless your program is running in 64-bit mode on a 64-bit OS.

    Anyways, thats my experience with using the method you mentioned. 🙂

    Jason

    Reply

Leave a Reply