How to determine the operating system service pack level in Visual Basic .NET or in Visual Basic 2005 (304722)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q304722
For a Microsoft Visual C# .NET version of this article, see 304721.
For a Microsoft Visual C++ .NET version of this article, see 307393.

SUMMARY

This step-by-step article shows you how to build the GetServicePack method.

MORE INFORMATION

The OSVersion property, which is provided for obtaining operating system (OS) information, does not contain a member that provides service pack information. To determine what service pack is installed, you must call the GetVersionEx API function directly. Typically, it is better to avoid this practice; the .NET Framework provides access to the underlying API sets in a much more consistent (and easier to use) manner than by calling the individual API functions.

When you must call an API function directly, you can do this through the Interop layer of the .NET Framework. The sample code in this article gives a method, GetServicePack, that returns the service pack level.

NOTE: The OSVERSIONINFO structure contains a fixed-length string, szCSDVersion. Because fixed-length strings are no longer supported, you must provide the marshalling information for this member. Do this by using the attribute (denoted by <>) preceding the member name.
  1. Open a new Visual Basic .NET or Visual Basic 2005 console application.
  2. Open the code window for Module1.vb, and delete all of the code.
  3. Paste the following code into Module1.vb.
    Module module1
        Private Structure OSVERSIONINFO
            Dim dwOSVersionInfoSize As Integer
            Dim dwMajorVersion As Integer
            Dim dwMinorVersion As Integer
            Dim dwBuildNumber As Integer
            Dim dwPlatformId As Integer
            <VBFixedString(128), _
              System.Runtime.InteropServices.MarshalAs _
                   (System.Runtime.InteropServices.UnmanagedType.ByValTStr, _
                SizeConst:=128)> Dim szCSDVersion As String
    
        End Structure
    
        Private Declare Function GetVersionExA Lib "kernel32" (ByRef lpVersionInformation As OSVERSIONINFO) As Short
    
        Public Function getServicePack() As String
            Dim osinfo As OSVERSIONINFO
            Dim retvalue As Short
            osinfo.dwOSVersionInfoSize = 148
            retvalue = GetVersionExA(osinfo)
            If Len(osinfo.szCSDVersion) = 0 Then
                Return ("No Service Pack Installed")
            Else
                Return (CStr(osinfo.szCSDVersion))
            End If
        End Function
    
        Public Sub main()
            Console.WriteLine(getServicePack())
        End Sub
    End Module
    
  4. Press CTRL+F5 to build and then run the project. The service pack information appears in the console window.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

304289 How To Determine Windows Version by Using Visual Basic .NET


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbhowto KB304722 kbAudDeveloper