This article describes an All-In-One Code Framework sample that is available for download. The code sample demonstrates the following platform detection tasks:
- Detect the name of the current operating system. For example, detect that you are running Microsoft Windows 7 Enterprise.
- Detect the version of the current operating system. For example, detect that you are running Microsoft Windows NT 6.1.7600.0.
- Determine whether the current operating system is a 64-bit operating system.
- Determine whether the current process is a 64-bit process.
- Determine whether an arbitrary process running on the system is 64-bit.
Difficulty level
Download information
To download this code sample, click the following link:
Technical overview
Detect the name of the current operating system
The name of the operating system can be detected from the Caption property of the Win32_Operating System WMI class.
Note For more information about the Win32_Operating System WMI class, please visit the following Microsoft website:
General information about Win32_OperatingSystem class
You can find the C++, C#, and VB.NET code that queries the value of Win32_OperatingSystem.Captionproperty in the GetOSName function of the code sample.Alternatively, you can build the string of the operating system name by using the GetVersionEx, GetSystemMetrics, GetProductInfo, and GetNativeSystemInfo functions.
Note For more information about how to build the string of the operating system name by using the GetVersionEx, GetSystemMetrics, GetProductInfo, and GetNativeSystemInfo functions, visit the following Microsoft website:
Note This solution may not apply to versions of Windows later than Windows 7.
Detect the version of the current operating system
- For C++ code, the OSVERSIONINFOEX structure that is outputted by the GetVersionEx function contains the following information:
- The dwMajorVersion major version number
- The dwMinorVersion minor version number
- The dwBuildNumber build number
- The wServicePackMajor major version number of the latest Service Pack
- The ServicePackMinor minor version number of the latest Service Pack
- For C# and VB.NET code, the System.Environment.OSVersion property returns an OperatingSystem object that contains the current platform identifier and version numbers.
Note For more information about the System.Environment.OSVersion property, please visit the following Microsoft website:General information about Environment.OSVersion Property
Note For more information about the OperatingSystem class, please visit the following Microsoft website: You can use these numbers to quick determine the current operating system and whether a certain Service Pack is installed.
Determine whether the operating system is a 64-bit operating system
- For C++ code, if a running process is a 64-bit process, the operating system must be a 64-bit operating system. To programmatically detect whether a 32-bit program is running on 64-bit operating system, you can use the IsWow64Process function.
- For C# and VB.NET code, the Environment.Is64BitOperatingSystem property in the .NET Framework 4 determines whether the current operating system is a 64-bit operating system.
Note For more information about the Environment.Is64BitOperatingSystem property, visit the following Microsoft website:General information about Environment.Is64BitOperatingSystem property
Determine whether a process is a 64-bit process
If you want to determine whether a running process is a 64-bit process, you can run the following code to determine the Boolean type return value by using the VC++ preprocessor symbols:
BOOL Is64BitProcess(void) { #if defined(_WIN64) return TRUE; // 64-bit program #else return FALSE; #endif }
You can also use the Environment.Is64BitProcess property in the .NET Framework 4 if you are running C# or VB.NET code.
Note For more information about the Environment.Is64BitProcess property, visit the following Microsoft website:
General information about Environment.Is64BitProcess property
If you want to detect whether an arbitrary application is a 64-bit process, you need to detect whether the OS bit is 64-bit. To do this, use the IsWow64Process() method along with the target process handle.
Technology category
- Windows Base
Languages
This code sample contains the following programming languages.
Language | Project Name |
Visual C++ .NET | CppPlatformDetector |
Visual C# | CSPlatformDetector |
Visual Basic .NET | VBPlatformDetector |
Prerequisites
- This sample application is created by using Visual Studio 2010 and the .NET Framework 4.