Is there a way to programatically determine if the OS Architecture is 32-bit or 64 -bit?
Printable View
Is there a way to programatically determine if the OS Architecture is 32-bit or 64 -bit?
I don't think there's actually a managed property or method for that anywhere. This should do the trick, although I haven't actually tested it on a 64-bit Windows install.Note that this will tell you if the current process is a 32-bit application running on 64-bit Windows. If it's a native 64-bit application then you'd already know that and it couldn't run on 32-bit platforms.vb.net Code:
<Runtime.InteropServices.DllImport("kernel32")> _ Private Shared Function IsWow64Process(ByVal hProcess As IntPtr, _ ByRef wow64Prcess As Boolean) As Boolean End Function Private Function IsWow64() As Boolean Dim wow64 As Boolean IsWow64Process(Process.GetCurrentProcess().Handle, wow64) Return wow64 End Function
should work too but I only have 32bit XP and can't test it. Wouldn't the IntPtr size be informative too? It shows 32 here, which is to be expected in my case.Code:MessageBox.Show(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"))
I was under the impression that that code would provide information about the processor itself rather than the OS, but I just ran it on 32-bit Windows running on a 64-bit processor and got "x86" as the result. I have also read suggestions for using the fact that the size of an IntPtr will be 4 on 32-bit systems and 8 on 64-bit systems, which you would exploit like so:Quote:
Originally Posted by Half
vb.net Code:
Runtime.InteropServices.Marshal.SizeOf(GetType(IntPtr))
Thanks to all I got it working.
Sorry to drag this old thread up but for anyone that finds it via a search I thought it would be worth mentioning that the IsWow64Process API that JMC mentioned does not exist on Windows XP unless SP2 is installed (and obviously also doesnt exist on Windows 2000) so that code will crash on a pre SP2 XP machine. After looking at the code that Microsoft use in the .NET 4.0 property Is64BitProcess, I've found that you can use another API (well a combination of a few) to check for the existence of a method in an unmanaged DLL. As there seem to be a few threads on this subject, all without a straight forward answer, I think I'll stick something in the codebank that is an absolutely fool proof way of finding out if your code is running on a 64 bit OS, unless someone else already has :)
EDIT: See this thread: http://www.vbforums.com/showthread.php?t=613889