/
VersionDetect.cs
149 lines (126 loc) · 4.4 KB
/
VersionDetect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace OSVersionExample
{
public static class VersionDetect
{
public static string ErrorMessage { get; private set; }
public static bool Is64BitOperatingSystem
{
get
{
// Clearly if this is a 64-bit process we must be on a 64-bit OS.
if (IntPtr.Size == 8)
return true;
// Ok, so we are a 32-bit process, but is the OS 64-bit?
// If we are running under Wow64 than the OS is 64-bit.
bool isWow64;
return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
}
}
#region winapi
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
extern static IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
#endregion
#region call_wmi_and_registry
private static string CallWMI(string WMIProperty)
{
string ret = null;
ManagementObjectSearcher mos = new
ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM CIM_OperatingSystem");
try
{
foreach (ManagementObject MObj in mos.Get())
{
ret = MObj.GetPropertyValue(WMIProperty).ToString();
}
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
return ret;
}
private static string CallRegistry(string RegValueName)
{
string ret = null;
try
{
RegistryKey key = Registry.LocalMachine.
OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\");
ret = key.GetValue(RegValueName).ToString();
key.Close();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
ret = null;
}
return ret;
}
#endregion
#region get_wmi_data
public static string GetOSVersionWMI()
{
ErrorMessage = string.Empty;
return CallWMI("Version");
}
public static string GetOSNameWMI()
{
ErrorMessage = string.Empty;
return CallWMI("Caption");
}
public static string GetOSSPWMI()
{
ErrorMessage = string.Empty;
return CallWMI("CSDVersion");
}
public static string GetOSArchWMI()
{
ErrorMessage = string.Empty;
return CallWMI("OSArchitecture");
}
#endregion
#region get_reg_data
public static string GetOSVersionReg()
{
ErrorMessage = string.Empty;
string Version = CallRegistry("CurrentVersion");
string Build = CallRegistry("CurrentBuildNumber");
if ((Version == null) || (Build == null))
{
return null;
}
return Version + "." + Build;
}
public static string GetOSNameReg()
{
ErrorMessage = string.Empty;
return CallRegistry("ProductName");
}
public static string GetOSSPReg()
{
ErrorMessage = string.Empty;
return CallRegistry("CSDVersion");
}
#endregion
static bool ModuleContainsFunction(string moduleName, string methodName)
{
IntPtr hModule = GetModuleHandle(moduleName);
if (hModule != IntPtr.Zero)
return GetProcAddress(hModule, methodName) != IntPtr.Zero;
return false;
}
}
}