Tag Archives: wmi

Check if Windows or your Program is in 64-bit or 32-bit Mode using VB.NET and Higher

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

A while back I made a simple post on using WMI (Windows Management Instrumentaion) to check if the operating system your program was running on was 32-bit or 64-bit. The main problem with that method is its not supported on Windows XP and doesn’t tell you what mode your application is running in. I made a small example with different methods that should be near fool proof. I have 4 methods of checking if your application is 32 bit (x86) or 64 bit (x64 or AMD64 which is the code name). I also have 5 methods of checking what mode the operating system is running in. If you still can’t pin down whether your application/operating system is 32 or 64 bit then I don’t know what to tell you. BTW here is a link to the earlier post I made on how to easily check if the operating system is 32-bit or 64-bit using WMI, which applications running on Vista or Windows 7 can use just fine.

Image of example application

There is to much code in the example I made to post here. You should definitely download it and check it out. It was made with VB 2005 but the codes will work in VB.NET 2002,.NET 2003, Visual Basic 2008, and Visual Basic.NET 2010 as well. So I will just highlight a couple ways to determine your application and os mode.


The code below grabs a string from a registry key and examines the text. This  is pretty much a fool proof way of whether your cpu is running on a x86 or x64 operating system. All you need to do is check a specific key in the registry like below…

        Dim cpuID As String = _
            My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", _
                "Identifier", "n/a")
        '
        'Get all chars from the beginning of the string until the first space is detected.
        cpuID = cpuID.Substring(0, cpuID.IndexOf(" "))

        MsgBox(cpuID)

If the key has x86 then its 32bit. If the key has AMD 64 or Intel64 then Windows is 64 bit. You can also check the registry key’s text to see if it contains AMD64 or Intel64. Of course if it contains x86 then the os is 32-bit and its 64-bit if it contains AMD64 or Intel64.

        'You can also check the registry string in this way.
        If LCase(cpuID).Contains("amd64") OrElse LCase(cpuID).Contains("intel64") Then

            MsgBox("It Is 64 Bit!")

        End If

I also want to show a simple way of checking if your application is running in 32 bit or 64 bit mode. You can simply check the size of IntPtr. If the pointer size is 4 bytes then its 32 bit. If its 8 bytes then its 64 Bit. This code simply checks if the size is higher than 4 or not. If its higher than 4 then its 64 bit. Otherwise its 32 bit. Remember, this method is the mode at the application level and not the platform level.

        'Basically if the Integer Pointer size is 4 then its 32 Bit and 8 is 64 Bit
        If IntPtr.Size > 4 Then

            MsgBox("64 Bit!")

        Else

            MsgBox("32 Bit!")

        End If

This post was mainly to give you an overview of a couple methods I used in the example program I made. The best thing would be for you to download the example and check out all of the methods I’ve come up with (or found out about).

I might add more to this little article in the future. That’s all for now. Have fun!

Jason

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

Find out how many CPU’s/Processors/Cores the computer has installed using VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

Its actually very easy with Visual Basic.NET VB 2008, VB.NET 2010, 2013, and Higher to detect the cpu or core count for the computer. Below is the very simple way to do it which will return the total number of logical processors installed in the computer…

        '
        'This works for Visual Basic 2005 and Higher.
        '
        'The code we want is under the System.Environment Namespace and is the ProcessorCount
        'property.
        MsgBox(System.Environment.ProcessorCount.ToString)

What if you want to get the CPU/Processor/Core count with Visual Basic.NET 2002 or 2003?

Well, .NET version 1.1 and older does not have the ProcessorCount property like version 2.0 of the .NET Framework. But that doesn’t mean it can’t be done. Its actually not to awfully hard to do. Below is a way to do it using the Windows system Registry…

        '
        'The registry path to get the info we are wanting.
        Dim str As String = "HARDWARE\DESCRIPTION\System\CentralProcessor"
        '
        'Returns how many SubKeys are under “CentralProcessor”. The subKey count under this key
        '=’s the‘total cpu’s your system has installed. Of course, 90% or so will only have 1x
        'subkey denoting a ‘single cpu system. My system happens to return 2x subkeys since I
        'have a Dual Core system.
        Dim cpuCount As Integer = My.Computer.Registry.LocalMachine.OpenSubKey(str, False).SubKeyCount

        MsgBox(cpuCount.ToString)

The Messagebox will then throw a Dialog with the total number of cpu’s/core’s on the current computer. I do not know though if this works on all Windows operating systems.

You can also use the Windows Management Instrumentation (WMI) and get the total instances of the class.

Below is a simple way to use WMI to return the computers CPU/Core Count…

       '
        'Create a Instrument Class and Object.
        Dim wmiClass As New System.Management.ManagementClass
        Dim wmiObject As New System.Management.ManagementObject
        '
        'Set the PAth to “Win32_Processor” which is where we want to get info.
        wmiClass.Path.RelativePath = "Win32_Processor"
        '
        'Last of all, just check how many instances is available and that will be your cpu/core count.
        MsgBox(wmiClass.GetInstances.Count)

Executing the code above will tell you how many cpus are on the computer since there is a class instance for each CPU/processor/core on the current computer.

Take care 🙂

                       Jason

Revised: 2015