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

This article has been archived. It is offered "as is" and will no longer be updated.
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
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 _UNICODEint wmain(void)#elseint 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.
For more general information about Visual C++ .NET, visit the following Microsoft Usenet newsgroup and Microsoft Web site:


Article ID: 307393 - Last Review: 01/16/2015 23:26:50 - Revision: 5.0

Microsoft Visual C++ 2005 Express Edition, Microsoft Visual C++ .NET 2003 Standard Edition, Microsoft Visual C++ .NET 2002 Standard Edition

  • kbnosurvey kbarchive kbhowtomaster kbnewsgrouplink KB307393