Microsoft KB Archive/307393

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

Article ID: 307393

Article Last Modified on 1/4/2006

-

APPLIES TO


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

-



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.  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. Open the code window for .cpp, and then delete all of the code.  Paste the following sample code: using namespace System; using namespace System::Runtime::InteropServices; String *GetServicePack(void);
 * 1) include &quot;stdafx.h&quot;
 * 1) using 

[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(&quot;kernel32.dll&quot;, EntryPoint=&quot;GetVersionEx&quot;, SetLastError=true,      CallingConvention=CallingConvention::StdCall)] extern long GetVersionEx ( [In, Out] OSVERSIONINFO* povi ); // This is the entry point for this application. int wmain(void) int main(void) {   Console::WriteLine(GetServicePack); return 0; }
 * 1) ifdef _UNICODE
 * 1) else
 * 1) endif

String *GetServicePack(void) { OSVERSIONINFO ovi; ovi.dwOSVersionInfoSize = Marshal::SizeOf ( __typeof(OSVERSIONINFO) ); if ( GetVersionEx ( &ovi ) == 0 ) {     return (S&quot;Call to GetVersionEx failed.&quot;); } if (ovi.szCSDVersion==(S&quot;&quot;)) return (S&quot;No Service Pack Installed&quot;); else return (ovi.szCSDVersion); }                    Press CTRL+F5 to build and then run the project. The service pack information appears in the console window.

