Microsoft KB Archive/304722

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

Article ID: 304722

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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.  Open a new Visual Basic .NET or Visual Basic 2005 console application. Open the code window for Module1.vb, and delete all of the code.  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  Dim szCSDVersion As String

End Structure

Private Declare Function GetVersionExA Lib &quot;kernel32&quot; (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 (&quot;No Service Pack Installed&quot;) Else Return (CStr(osinfo.szCSDVersion)) End If   End Function

Public Sub main Console.WriteLine(getServicePack) End Sub End Module  Press CTRL+F5 to build and then run the project. The service pack information appears in the console window.

