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

Article translations Article translations
Article ID: 307393 - View products that this article applies to.
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
Expand all | Collapse all

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:

Visual C++ .NET (2002) Support Center

Properties

Article ID: 307393 - Last Review: November 9, 2012 - Revision: 4.0
Applies to
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbnewsgrouplink KB307393

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com