Tag Archives: intel64

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

Target CPU and why you may get: “An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)”

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

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

I remember getting quite annoyed a long time ago when I would get that error message above. Although it happened a few years ago I decided to write a brief post about it now anyways. You need to remember that this error message could possibly occur in different scenario’s and not just the way I got it.

I was trying to setup an example using VB 2005 that used a static.DLL file I made with C++. Whenever I would call a function from the DLL I would get the error message below…

‘BadImageFormatException was unhandled’

“An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)”

Picture of the Bad Format Error Message


It took some time and the advice of someone to find out why the problem kept popping up.

I use as my main computer an Intel Core2 based Processor/CPU which has 64 Bit support (x64 or AMD64, Intel64 is a clone of AMD64, which is the code name). Anyways, I use Windows Vista 64 Bit. That’s obviously not a problem in itself. I made the .DLL I mentioned above long before I upgraded to Vista 64. (BTW this error message will popup as well when running Windows7 64 Bit, and newer as well) The C++ .NET .DLL I made was based on Visual Studio.NET 2003 which will only compile 32 bit assemblies and was written when I still used Windows XP Professional 32 Bit (x86). Of course that’s not a problem in itself either. (Microsoft says .NET older than 2005 should Not be used for programming on any variant of Vista). The problem was when the two came together. I use Visual Studio 2005 Standard Edition which can target both 32 Bit and 64 Bit CPU’s and by default the profile is – Target CPU: AnyCPU. Since my CPU was running on a 64 bit Operating System, Visual Studio.NET would try to target using 64 Bit profiling instead of 32-Bit which the .DLL was written in. And that turned out to be the reason I kept getting that incorrect format error message. The program was targeting 64 Bit mode but the .DLL library I was using was 32 bit.


Picture of the Target CPU Compile Screen

To fix the problem all I had to do was change the: Target CPU: to ‘x86’ which is 32 Bit mode under the:

‘Project’ Menu: then under your project’s ‘Properties’: then under the ‘Compile’ Tab, and then ‘Advanced Compiler Settings’ which will have a small ComboBox near the bottom to select the ‘Target CPU’ to use, which I set to x86 in my case. But ‘x64′ is also available. In version 2010 of Visual Basic you can target Intel’s ‘Itanium’ processor as well..

I then re-ran the program and the calls to the .dll worked exactly like its supposed too. So if you ever get that Bad Format error message and your programming in 64 Bit mode you may try changing the Target CPU to x86 instead of the default AnyCPU (Or vice-versa).

Note: Visual Basic 2005/2008/2010 Express Edition IDE can also target a cpu architecture or platform but you change the settings in a different way using the configuration manager.

Jason

Revisited: 2015

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