VS 2008 [RESOLVED] Determining Architecture of OS (x86 or x64)-VBForums
Results 1 to 24 of 24

Thread: [RESOLVED] Determining Architecture of OS (x86 or x64)

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,825

    Resolved [RESOLVED] Determining Architecture of OS (x86 or x64)

    When trying to determine the Architecture of an OS, I ran across IntPtr.

    From reading the documentation, I saw this:
    The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.
    Since the size of IntPtr is intended to be the size of the architecture, would it be feasible to test the size and determine the OS by that? Or is there a better way?

    I was thinking there might also be a way via WMI, but I don't want to use that for compatibility reason.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,825

    Re: Determining Architecture of OS (x86 or x64)

    Reading the documentation a bit more, I found this:
    The size of a pointer or handle on this platform, measured in bytes. The value of this property is 4 on a 32-bit platform, and 8 on a 64-bit platform
    So it seems it will work, but I wonder how reliable this would be.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: Determining Architecture of OS (x86 or x64)

    Using a console application, I did this:
    vb.net Code:
    1. Module Module1
    2.     Sub Main()
    3.         Select Case IntPtr.Size
    4.             Case "4"
    5.                 Console.WriteLine("32-bit Platform Detected!")
    6.             Case "8"
    7.                 Console.WriteLine("64-bit Platform Detected!")
    8.             Case Else
    9.                 Console.WriteLine("No clue what architecture this is!")
    10.         End Select
    11.         System.Threading.Thread.Sleep(3000)
    12.     End Sub
    13. End Module
    I have a 64-bit OS and it wrote that it found a 64-bit. My friend has a 32-bit OS and it said it was a 32-Bit. I think we can say IntPtr is reliable as of this state (well at least on my two tests.)

  4. #4

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,825

    Re: Determining Architecture of OS (x86 or x64)

    Quote Originally Posted by formlesstree4 View Post
    Using a console application, I did this:
    I have a 64-bit OS and it wrote that it found a 64-bit. My friend has a 32-bit OS and it said it was a 32-Bit. I think we can say IntPtr is reliable as of this state (well at least on my two tests.)
    Thanks Formless. I had tested it on my machine, but wasn't sure how reliable it would be.

    It worked just fine on my machine and it seems it works just fine on your and your friends'. So, I would say it's reliable enough.

    Thanks again.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: Determining Architecture of OS (x86 or x64)

    No problem.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    If your program is compiled to x86 then that will not work though because on a 64-bit OS it will run the program as if it were 32-bit (for compatibility obviously) and that includes 'pretending' that IntPtr's are only 4 bytes long.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  7. #7
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,579

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    There were some lengthy threads about this and the size of the intptr structure can indeed only give you an idea if the process is running in a 32 bit environment (emulated or not). This is usefull for api signatures and whatnot.

    See if the following could be useful to you
    Code:
    MessageBox.Show(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"))
    If not, try jmcilhinney's suggestion here.
    VB 2005, Win Xp Pro sp2

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Weird that when I run that I get "AMD64" as the result, when I'm on an Intel processor.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  9. #9
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,579

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    It's the number that counts & you've been scammed
    VB 2005, Win Xp Pro sp2

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Just wanted to update this thread with something that is new in .NET 4.0 as I noticed a link to this thread was in a couple of people's signatures. In .NET 4.0 you can use the following to determine if you are running on a 64 bit OS
    Code:
    Environment.Is64BitOperatingSystem
    and there is also
    Code:
    Environment.Is64BitProcess
    which determines if the current process (doesnt look like there is any way to run it against external processes) is 64 bit.

    The way it actually works internally is kind of interesting, here's the definition of the GET for the Is64BitOperatingSystem property
    vb.net Code:
    1. Public Shared Function get_Is64BitOperatingSystem() As Boolean
    2.     Dim flag As Boolean
    3.     Return ((Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process") AndAlso Win32Native.IsWow64Process(Win32Native.GetCurrentProcess, flag)) AndAlso flag)
    4. End Function
    so it looks like it just checks to see if the IsWow64Process method exists in the kernel32.dll file

    The Is64BitProcess property is a bit more confusing though as it just returns False and that is it :S

    EDIT: Ahh this article explains why - http://alexmg.com/post/2009/12/21/Is...in-NET-40.aspx (basically because I was looking at the 32 bit version of the framework DLLs)
    Last edited by chris128; May 5th, 2010 at 05:22 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  11. #11
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Can't we just do that ourselves perhaps?

    EDIT: I mean check for the Method in kernel32.dll.

  12. #12
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Quote Originally Posted by chris128 View Post
    Weird that when I run that I get "AMD64" as the result, when I'm on an Intel processor.
    64 bit OSes will return AMD64 regardless of whether they're AMD or Intel and 32 bit wil return x86.

    Good to know about the .NET 4.0 changes.

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Quote Originally Posted by formlesstree4 View Post
    Can't we just do that ourselves perhaps?

    EDIT: I mean check for the Method in kernel32.dll.
    I assume you mean if you are still working with pre .NET 4.0 code yeah?

    As the article I linked to in my post explains, there are 2 versions of the core Framework DLL (mscorlib), one for 32 bit and one for 64 bit and obviously the relevant one gets loaded depending on which system you are running on. So in the 32 bit version which I was looking at, it does that check for the method, however in the 64 bit version of mscorlib it just returns True no matter what, because the 64 bit version can only be loaded on a 64 bit OS.

    So its not quite as black and white as I initially thought when I posted that, but yeah if you can figure out how to check for the existence of a method in a C++ DLL then I guess you could use that as your test.
    Here are the definitions of the methods that the property is using to check for the method existing but no idea if that will work just copying and pasting it... Perhaps this code for checking for a method existing in a DLL could be useful for something else as well.

    vb.net Code:
    1. Friend Shared Function DoesWin32MethodExist(ByVal moduleName As String, ByVal methodName As String) As Boolean
    2.     Dim moduleHandle As IntPtr = Win32Native.GetModuleHandle(moduleName)
    3.     If (moduleHandle = IntPtr.Zero) Then
    4.         Return False
    5.     End If
    6.     Return (Win32Native.GetProcAddress(moduleHandle, methodName) <> IntPtr.Zero)
    7. End Function
    8.  
    9. <ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    10. Private Shared Function GetModuleHandle(ByVal moduleName As String) As IntPtr
    11. End Function
    12.  
    13.  
    14. <DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    15. Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal methodName As String) As IntPtr
    16. End Function
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Tried it and it doesnt seem to work no errors or anything, just returns False when it should return True...
    EDIT: Ah no it does work but only if the process is running on a 64 bit OS but as a 32 bit process... sure there will be a way to adapt it to give an accurate answer no matter what though - give me a few minutes

    Here's the code I'm using:
    vb Code:
    1. '
    2.    Friend Shared Function DoesWin32MethodExist(ByVal moduleName As String, ByVal methodName As String) As Boolean
    3.         Dim moduleHandle As IntPtr = GetModuleHandle(moduleName)
    4.         If (moduleHandle = IntPtr.Zero) Then
    5.             Return False
    6.         End If
    7.         Return (GetProcAddress(moduleHandle, methodName) <> IntPtr.Zero)
    8.     End Function
    9.  
    10.     <ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    11.     Private Shared Function GetModuleHandle(ByVal moduleName As String) As IntPtr
    12.     End Function
    13.  
    14.  
    15.     <DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    16.     Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal methodName As String) As IntPtr
    17.     End Function
    18.  
    19.     <DllImport("kernel32.dll", SetLastError:=True)> _
    20.     Friend Shared Function IsWow64Process(<[In]()> ByVal hSourceProcessHandle As IntPtr, <Out(), MarshalAs(UnmanagedType.Bool)> ByRef isWow64 As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
    21.     End Function
    22.  
    23.     <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    24.     Friend Shared Function GetCurrentProcess() As IntPtr
    25.     End Function
    26.  
    27.  
    28.     Public ReadOnly Property Is64BitOperatingSystem() As Boolean
    29.         Get
    30.             Dim flag As Boolean
    31.             Return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") AndAlso IsWow64Process(GetCurrentProcess, flag)) AndAlso flag)
    32.         End Get
    33.     End Property
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  15. #15
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    what does stepping through reveal?

  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Not a lot as the majority of it is API calls. I've just found out though that you cant use this to determine if an OS is 64 bit because the IsWow64Process method exists in 32 bit versions of the Windows kernel as well so it looks like the .NET framework basically already knows what architecture it is running as but just uses this method to check to make sure it is not a 32 bit process running on a 64 bit system - so it checks to make sure the method exists, then calls it on the current process. I think the reason it checks for the existence of the method first is because the method only exists in kernel32.dll in XP SP2 onwards.

    However I think we can still make use of it - you just use the property that I posted in my previous post first and if it returns True then you know you are on a 64 bit OS but you are running as a 32 bit process (which is where IntPtr cant help you). If it returns false then you check the size of IntPtr, because if it returned false then you are either on a 64 bit OS and you are running as a 64 bit process (therefore IntPtr = 8) or you are a 32 bit process running on a 32 bit OS (so IntPtr = 4).

    Having said that, it is much simpler and easier to just check the
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  17. #17
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    for gods sake I just realised that JMC already posted a much simpler way of doing this that I completely missed haha http://www.vbforums.com/showpost.php...17&postcount=2
    EDIT: Though it is not perfect as it does not check for the existence of the IsWow64Process before calling it so it would crash on a pre SP2 XP machine..
    Last edited by chris128; May 6th, 2010 at 02:43 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  18. #18
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Having said that, it is much simpler and easier to just check the
    The what? That made me laugh for a few minutes, didn't finish your train of thought?

  19. #19
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    haha I have no idea what I was going to say there...

    EDIT: Here's a codebank thread I just made that should provide a fool proof way of checking for a 64 bit OS http://www.vbforums.com/showthread.php?t=613889
    Last edited by chris128; May 6th, 2010 at 02:59 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  20. #20

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,825

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    That's awesome, Chris. I've never run into an issue with IntPtr yet, but I'm bound to.

    I'm curious though, if JMC's example worked, why would I want to use the one you've posted? If they both do the same thing, wouldn't I choose the example with fewer lines of code?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  21. #21

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,825

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Err... disregard last post, I guess. I just saw where you said his code crashes on pre XP SP2 machines.

    Though at this point, running XP in general is just silly
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  22. #22
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Though at this point, running XP in general is just silly
    Despite most PC's running XP at this point? I dunno man....it is still wise to develop for XP and above until XP's support is phased out completely, and even a few years afterwards.

  23. #23

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,825

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    I was being facetious. I know it's hard to convey inflection and sarcasm through our posts, but I was hoping the wink would suffice.

    Also, I was kind of hoping Minitech would see this post. I'm always giving him a hard time about not adopting new technology
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  24. #24
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,216

    Re: [RESOLVED] Determining Architecture of OS (x86 or x64)

    Quote Originally Posted by weirddemon View Post
    I was being facetious. I know it's hard to convey inflection and sarcasm through our posts, but I was hoping the wink would suffice.

    Also, I was kind of hoping Minitech would see this post. I'm always giving him a hard time about not adopting new technology
    I did not seem to put two and two together to make four, but instead twenty two. Basically, I overlooked it completely.

    As for Minitech, he'll come around eventually. He's bound to!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.