How to determine the operating system service pack level in Visual C++ (307393)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q307393
For a Microsoft Visual C# .NET version of this article, see 304721.
For a Microsoft Visual Basic .NET version of this article, see 304722.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Runtime.InteropServices

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 .NET C++ managed application.

    Note In the New Project dialog box, click Visual C++ Projects under Project Types, and then click Managed C++ Application for Visual Studio .NET 2002, Console Application (.NET) for Visual Studio .NET 2003, or CLR Console Application for Visual Studio 2005 under Templates.
  2. Open the code window for Project Name.cpp, and then delete all of the code.
  3. Paste the following sample code:
    #include "stdafx.h"
     
    #using <mscorlib.dll>
     
    using namespace System;
    using namespace System::Runtime::InteropServices;
    String *GetServicePack(void);
    
    [StructLayout(StructLayoutKind::Sequential)]
    __value struct OSVERSIONINFO
    {
    public:
     unsigned long dwOSVersionInfoSize; 
     unsigned long dwMajorVersion; 
     unsigned long dwMinorVersion; 
     unsigned long dwBuildNumber; 
     unsigned long dwPlatformId;
     [MarshalAs(UnmanagedType::ByValTStr, SizeConst=128)]
     String* szCSDVersion;
    };
     
    [DllImport("kernel32.dll", EntryPoint="GetVersionEx",  SetLastError=true, 
         CallingConvention=CallingConvention::StdCall)]
    extern long GetVersionEx ( [In, Out] OSVERSIONINFO* povi );
     
    // This is the entry point for this application.
    #ifdef _UNICODE
    int wmain(void)
    #else
    int main(void)
    #endif
    {
    	Console::WriteLine(GetServicePack());
        
        return 0;
    }
    
    
    String *GetServicePack(void)
    {
     OSVERSIONINFO ovi;
     ovi.dwOSVersionInfoSize = Marshal::SizeOf ( __typeof(OSVERSIONINFO) );
     
     if ( GetVersionEx ( &ovi ) == 0 )
     {
    	  return (S"Call to GetVersionEx() failed.");
      
     }
     if (ovi.szCSDVersion==(S""))					 
    	  return (S"No Service Pack Installed");
     else
          return (ovi.szCSDVersion);
     
      
    }
    					
  4. Press CTRL+F5 to build and then run the project. The service pack information appears in the console window.

REFERENCES

For more general information about Visual C++ .NET, visit the following Microsoft Usenet newsgroup and Microsoft Web site:

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kbHOWTOmaster kbNewsgroupLink KB307393 kbAudDeveloper