Results 1 to 9 of 9

Thread: Finding OS Information

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Location
    USA
    Posts
    21

    Finding OS Information

    A guy at another forum I'm a member at posted code in response to a question about this, however his code didn't work properly and he only hinted to the types/api calls/etc that were used so I decided to rewrite it and include them here. After I finished just getting the basic OS name I wanted more information so I consulted MSDN. Anyway, this is more extensive than his code and I figure it'll be of use to somebody. I seperated the comments/code/usage to make it cleaner since it's pretty long.

    Comments:
    VB Code:
    1. 'This is an example of how to retreive your Operating System information.
    2. 'Special thanks to Noodlez for his code example at [url]http://forum.valhallalegends.com/phpbbs/index.php?board=17;action=display;threadid=615[/url]
    3. 'All of this information can be retreived through MSDN and by using the API Viewer
    4. 'This can be easily extended to include all suites (such as BackOffice) See: OSVERSIONEX
    5. 'To extend this even more and check if it's running on a 64-bit processor see [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/iswow64process.asp[/url]
    6.  
    7.  
    8. 'Main MSDN Resources Used:
    9. 'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getversionex.asp
    10. 'http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
    11. 'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/osversioninfo_str.asp
    12.  
    13.  
    14. 'Types format supplied by MSDN
    15.  
    16. 'typedef struct _OSVERSIONINFO {
    17. 'DWORD dwOSVersionInfoSize;
    18. 'DWORD dwMajorVersion;
    19. 'DWORD dwMinorVersion;
    20. 'DWORD dwBuildNumber;
    21. 'DWORD dwPlatformId;
    22. 'TCHAR szCSDVersion[128];
    23. '} OSVERSIONINFO;
    24. '
    25. 'typedef struct _OSVERSIONINFOEX {
    26. 'DWORD dwOSVersionInfoSize;
    27. 'DWORD dwMajorVersion;
    28. 'DWORD dwMinorVersion;
    29. 'DWORD dwBuildNumber;
    30. 'DWORD dwPlatformId;
    31. 'TCHAR szCSDVersion[128];
    32. 'WORD wServicePackMajor;
    33. 'WORD wServicePackMinor;
    34. 'WORD wSuiteMask;
    35. 'BYTE wProductType;
    36. 'BYTE wReserved;
    37. '} OSVERSIONINFOEX,
    38. '*POSVERSIONINFOEX,
    39. '*LPOSVERSIONINFOEX;
    40.  
    41. 'Supplied by MSDN (Suite Information)
    42. 'VER_SUITE_BACKOFFICE Microsoft BackOffice components are installed.
    43. 'VER_SUITE_BLADE Windows Server 2003, Web Edition is installed.
    44. 'VER_SUITE_DATACENTER Windows 2000 Datacenter Server or Windows Server 2003, Datacenter Edition is installed.
    45. 'VER_SUITE_ENTERPRISE Windows NT 4.0 Enterprise Edition, Windows 2000 Advanced Server, or Windows Server 2003, Enterprise Edition is installed. Refer to the Remarks section for more information about this bit flag.
    46. 'VER_SUITE_PERSONAL Windows XP Home Edition is installed.
    47. 'VER_SUITE_SMALLBUSINESS Microsoft Small Business Server was once installed on the system, but may have been upgraded to another version of Windows. Refer to the Remarks section for more information about this bit flag.
    48. 'VER_SUITE_SMALLBUSINESS_RESTRICTED Microsoft Small Business Server is installed with the restrictive client license in force. Refer to the Remarks section for more information about this bit flag.
    49. 'VER_SUITE_TERMINAL Terminal Services is installed.
    50. 'VER_SUITE_SINGLEUSERTS Terminal Services is installed, but only one interactive session is supported.

    Declares:
    VB Code:
    1. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    2. Private Declare Function GetVersionAdv Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFOEX) As Long
    3.  
    4.  
    5. Private Const WIN95 As String = "1.4.0"
    6. Private Const WIN98 As String = "1.4.10"
    7. Private Const WINME As String = "1.4.98"
    8. Private Const WINNT As String = "2.3.51" 'Windows NT 3.51
    9. Private Const WINNT4 As String = "2.4.0" 'Windows NT 4
    10. Private Const WIN2K As String = "2.5.0"
    11. Private Const WINXP As String = "2.5.1"
    12. Private Const WINNTS As String = "2.5.2" 'Windows Server 2k3
    13.  
    14.  
    15. Private Type OSVERSIONINFO
    16.     dwOSVersionInfoSize As Long 'Size = 148
    17.     dwMajorVersion As Long
    18.     dwMinorVersion As Long
    19.     dwBuildNumber As Long
    20.     dwPlatformID As Long
    21.     szCSDVersion As String * 128
    22. End Type
    23.  
    24. Private Type OSVERSIONINFOEX 'Only works if OS >= NT4 SP6
    25.     dwOSVersionInfoSize As Long 'Size = 156
    26.     dwMajorVersion As Long
    27.     dwMinorVersion As Long
    28.     dwBuildNumber As Long
    29.     dwPlatformID As Long
    30.     szCSDVersaion As String * 128
    31.     wServicePackMajor As Integer
    32.     wServicePackMinor As Integer
    33.     wSuiteMask As Integer
    34.     wProductType As Byte
    35.     wReserved As Byte
    36. End Type
    37.  
    38. Private Enum WinProdType
    39.     VER_NT_WORKSTATION = 1 'System = Win XP *, WIN NT 4, WIN 2K Pro
    40.     VER_NT_DOMAIN_CONTROLLER = 2 'System = domain controller
    41.     VER_NT_SERVER = 3 'System = server
    42. End Enum
    43.  
    44. Private Enum WinSuiteMask
    45.     VER_SUITE_SMALLBUSINESS = &H1
    46.     VER_SUITE_ENTERPRISE = &H2
    47.     VER_SUITE_BACKOFFICE = &H4
    48.     VER_SUITE_COMMUNICATIONS = &H8
    49.     VER_SUITE_TERMINAL = &H10
    50.     VER_SUITE_SMALLBUSINESS_RESTRICTED = &H20
    51.     VER_SUITE_EMBEDDEDNT = &H40
    52.     VER_SUITE_DATACENTER = &H80
    53.     VER_SUITE_SINGLEUSERTS = &H100
    54.     VER_SUITE_PERSONAL = &H200
    55.     VER_SUITE_BLADE = &H400
    56. End Enum
    Last edited by Trust; Jun 14th, 2004 at 01:32 PM.
    mmk

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Location
    USA
    Posts
    21
    Code:
    VB Code:
    1. Private Function DisplayOS() As String
    2.     Dim osvi As OSVERSIONINFO, xosvi As OSVERSIONINFOEX
    3.     Dim SStatus As Boolean 'boolean takes less resources than Variant
    4.     Dim xInfo As Boolean 'check if OS has extra info (ie: Home Edition)
    5.    
    6.     'Variables to hold operating system information
    7.     Dim platformID As String, BuildNumber As String, MinorVer As String, _
    8.     MajorVer As String, WindowsVer As String, WindowsDistro As String, _
    9.     WindowsType As String, ServicePack As String
    10.      
    11.     osvi.dwOSVersionInfoSize = 148 'Len(osvi)
    12.     xInfo = False
    13.     SStatus = GetVersionEx(osvi) 'GetVersionEx API
    14.  
    15.     If SStatus = True Then 'If GetVersionEx doesn't return false
    16.         platformID = osvi.dwPlatformID
    17.         majorversion = osvi.dwMajorVersion
    18.         minorversion = osvi.dwMinorVersion
    19.         BuildNumber = osvi.dwBuildNumber
    20.         ServicePack = osvi.szCSDVersion
    21.         WindowsVer = platformID & "." & majorversion & "." & minorversion
    22.         Select Case WindowsVer
    23.             Case Is < WIN95
    24.                 WindowsDistro = "Windows 3.x"
    25.             Case WIN95
    26.                 If (InStr(1, UCase(osvi.szCSDVersion), "B")) Or (InStr(1, UCase(osvi.szCSDVersion), "C")) Then
    27.                     WindowsDistro = "Windows 95 OSR2"
    28.                 Else
    29.                     WindowsDistro = "Windows 95"
    30.                 End If
    31.             Case WIN98
    32.                 If (InStr(1, UCase(osvi.szCSDVersion), "A")) Then
    33.                     WindowsDistro = "Windows 98 SE"
    34.                 Else
    35.                     WindowsDistro = "Windows 98"
    36.                 End If
    37.             Case WINME
    38.                 WindowsDistro = "Windows ME"
    39.             Case WINNT
    40.                 WindowsDistro = "Windows NT 3.51"
    41.             Case WINNT4
    42.                 WindowsDistro = "Windows NT 4.00"
    43.                     If UCase(Trim(osvi.szCSDVersion)) >= "SERVICE PACK 6" Then
    44.                         xInfo = True 'Gather More
    45.                         GoTo xInfo   'Information
    46.                     End If
    47.                    
    48.             Case WIN2K
    49.                 WindowsDistro = "Windows 2000"
    50.                     xInfo = True
    51.                     GoTo xInfo
    52.             Case WINXP
    53.                 WindowsDistro = "Windows XP"
    54.                     xInfo = True
    55.                     GoTo xInfo
    56.             Case WINNTS
    57.                 WindowsDistro = "Windows Server 2003"
    58.                     xInfo = True
    59.                     GoTo xInfo
    60.             Case Else
    61.                 WindowsDistro = "Unknown Windows Version"
    62.         End Select
    63.     End If
    64.    
    65. xInfo:
    66. If xInfo = True Then
    67.     xosvi.dwOSVersionInfoSize = 156 'Len(xosvi)
    68.     SStatus = GetVersionAdv(xosvi)
    69.    
    70.     If SStatus = True Then
    71.         Select Case xosvi.dwMajorVersion
    72.             Case &H4 'NT 4
    73.                 Select Case xosvi.wProductType
    74.                     Case WinProdType.VER_NT_DOMAIN_CONTROLLER
    75.                         WindowsType = "Domain Controller"
    76.                        
    77.                     Case WinProdType.VER_NT_SERVER
    78.                         If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE > 0) Then
    79.                             WindowsType = "Advanced Server"
    80.                         Else
    81.                             WindowsType = "Server"
    82.                         End If
    83.                        
    84.                     Case WinProdType.VER_NT_WORKSTATION
    85.                         WindowsType = "Workstation"
    86.                 End Select
    87.             Case &H5 'WIN2K/XP/.NET
    88.                 Select Case xosvi.dwMinorVersion
    89.                     Case &H0 'WIN2K
    90.                         Select Case xosvi.wProductType
    91.                             Case WinProdType.VER_NT_DOMAIN_CONTROLLER
    92.                                 WindowsType = "Domain Controller"
    93.                            
    94.                             Case WinProdType.VER_NT_SERVER
    95.                                 If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE > 0) Then
    96.                                     WindowsType = "Advanced Server"
    97.                                 Else
    98.                                     WindowsType = "Server"
    99.                                 End If
    100.                            
    101.                             Case WinProdType.VER_NT_WORKSTATION
    102.                                 WindowsType = "Professional"
    103.                         End Select
    104.                    
    105.                     Case &H1 'XP
    106.                         Select Case xosvi.wProductType
    107.                             Case WinProdType.VER_NT_DOMAIN_CONTROLLER
    108.                                 WindowsType = "Domain Controller"
    109.                                
    110.                             Case WinProdType.VER_NT_SERVER
    111.                                 WindowsType = "Server"
    112.                                
    113.                             Case WinProdType.VER_NT_WORKSTATION
    114.                                 If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_PERSONAL > 0) Then
    115.                                     WindowsType = "Home Edition"
    116.                                 Else
    117.                                     WindowsType = "Professional"
    118.                                 End If
    119.                         End Select
    120.                                                        
    121.                     Case &H2 '.NET
    122.                         Select Case xosvi.wProductType
    123.                             Case WinProdType.VER_NT_DOMAIN_CONTROLLER
    124.                                 WindowsType = "Domain Controller"
    125.                            
    126.                             Case WinProdType.VER_NT_SERVER
    127.                                 If xosvi.wSuiteMask Then
    128.                                     If WinSuiteMask.VER_SUITE_BLADE Then
    129.                                         WindowsType = "Web Server"
    130.                                     ElseIf WinSuiteMask.VER_SUITE_DATACENTER Then
    131.                                         WindowsType = "Datacenter Server"
    132.                                     ElseIf WinSuiteMask.VER_SUITE_ENTERPRISE Then
    133.                                         WindowsType = "Enterprise Server"
    134.                                     Else
    135.                                         WindowsType = "Server" 'Personal?
    136.                                     End If
    137.                                 End If
    138.  
    139.                             Case WinProdType.VER_NT_WORKSTATION
    140.                                 WindowsType = vbNullString
    141.                         End Select
    142.                        
    143.                     Case Else
    144.                         WindowsType = vbNullString
    145.                 End Select
    146.             Case Else
    147.                 WindowsType = vbNullString
    148.         End Select
    149.     End If
    150.     End If
    151.     DisplayOS = WindowsDistro & Space(1) & WindowsType & Space(1) & ServicePack
    152. End Function

    Code Example:
    VB Code:
    1. Private Sub Form_Load()
    2.      MsgBox DisplayOS
    3. End Sub

    You can also return the values for these variables which is probably of more use:
    VB Code:
    1. Dim platformID As String, BuildNumber As String, MinorVer As String,  MajorVer As String, WindowsVer As String, WindowsDistro As
    2. String,  WindowsType As String, ServicePack As String

    The code is pretty self explainitory as long as you have a basic knowledge of VB/API calls. And it looked clean in the IDE, hopefully it will stay that way when formatted on the forum.

    My wrists hurt now from all that typing. Hopefully it wasn't for no reason. :P
    Last edited by Trust; Jun 14th, 2004 at 01:33 PM.
    mmk

  3. #3
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196
    You have a minor Error in your code.
    VB Code:
    1. Case WinProdType.VER_NT_SERVER
    2.                         If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE > 0) Then
    Should Read
    VB Code:
    1. If ((xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_PERSONAL) > 0) Then

    There could be other syntax errors, but I only tested on XP Pro and XP Home, so your Bit Operations might be wrong in a few cases above.
    Brandon S Davids

  4. #4
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Finding OS Information

    Awesome, I've been trying to figure out how to get the service pack info for a while now.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  5. #5
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Finding OS Information

    Need to turn Option Explicit off as some variables incorrectly declared. Also says my XP Professional is "Home edition" (have not yet checked why).
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  6. #6
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Finding OS Information

    Quote Originally Posted by BrianHawley
    Need to turn Option Explicit off as some variables incorrectly declared. Also says my XP Professional is "Home edition" (have not yet checked why).
    Same thing happens to me. But since all we use is Professional at work I changed it to always say professional.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  7. #7
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Finding OS Information

    I found this and it seems to work perfectly (so far!)

    http://www.tek-tips.com/viewthread.cfm?qid=736949
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  8. #8
    Junior Member
    Join Date
    May 2005
    Posts
    19

    Re: Finding OS Information

    thats a long and big code why not just do this under form_load put
    VB Code:
    1. For x = 1 To 200
    2.   Info = Info & Environ(x) & vbCrLf
    3. Next
    4. MsgBox Info
    and you get all the info

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Finding OS Information

    The enviromental variables aren't very reliable since the user can change them


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

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



Click Here to Expand Forum to Full Width