-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsBit.cpp
385 lines (350 loc) · 13.1 KB
/
OsBit.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* \file OsBit.cpp
* \brief
*
* Module Name: CppCheckOSBitness.cpp
* Project: CppCheckOSBitness
* Copyright (c) Microsoft Corporation.
*
* The code sample demonstrates how to determine whether the operating system
* of the current machine or any remote machine is a 64-bit operating system.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/
//-------------------------------------------------------------------------------------------------
#pragma region Includes
#include <stdio.h>
#include <windows.h>
#pragma endregion
#pragma region Is64BitOperatingSystem (IsWow64Process)
//-------------------------------------------------------------------------------------------------
//
// FUNCTION: DoesWin32MethodExist(PCWSTR, PCSTR)
//
// PURPOSE: The function determins whether a method exists in the export
// table of a certain module.
//
// PARAMETERS:
// * pszModuleName - the name of the module.
// * pszMethodName - the name of the method.
//
// RETURN VALUE: The function returns TRUE if the method specified by
// methodName exists in the export table of the module specified by
// moduleName.
//
BOOL DoesWin32MethodExist(PCWSTR pszModuleName, PCSTR pszMethodName)
{
HMODULE hModule = GetModuleHandle(pszModuleName);
if (hModule == NULL)
{
return FALSE;
}
return (GetProcAddress(hModule, pszMethodName) != NULL);
}
//-------------------------------------------------------------------------------------------------
//
// FUNCTION: Is64BitOperatingSystem()
//
// PURPOSE: The function determines whether the current operating system is
// a 64-bit operating system.
//
// RETURN VALUE: The function returns TRUE if the operating system is
// 64-bit; otherwise, it returns FALSE.
//
BOOL Is64BitOperatingSystem()
{
#if defined(_WIN64)
return TRUE; // 64-bit programs run only on Win64
#elif defined(_WIN32)
// 32-bit programs run on both 32-bit and 64-bit Windows
BOOL f64bitOS = FALSE;
return ((DoesWin32MethodExist(L"kernel32.dll", "IsWow64Process") &&
IsWow64Process(GetCurrentProcess(), &f64bitOS)) && f64bitOS);
#else
return FALSE; // 64-bit Windows does not support Win16
#endif
}
//-------------------------------------------------------------------------------------------------
#pragma endregion
#pragma region Is64BitOperatingSystem (WMI)
#include <wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
#include <strsafe.h>
#include <comdef.h>
#include <wincred.h>
//-------------------------------------------------------------------------------------------------
//
// FUNCTION: Is64BitOperatingSystem(LPCWSTR, LPCWSTR, LPCWSTR)
//
// PURPOSE: The function determines whether the operating system of the
// current machine of any remote machine is a 64-bit operating system
// through Windows Management Instrumentation (WMI).
//
// PARAMETERS:
// * pszMachineName - the full computer name or IP address of the target
// machine. "." or NULL means the local machine.
// * pszUserName - the user name you need for a connection. A null value
// indicates the current security context. If the user name is from a
// domain other than the current domain, the string should contain the
// domain name and user name, separated by a backslash: string 'username'
// = "DomainName\\UserName".
// * pszPassword - the password for the specified user.
//
// RETURN VALUE: The function returns true if the operating system is
// 64-bit; otherwise, it returns false.
//
// EXCEPTION: If this function fails, it throws a C++ exception which
// contains the HRESULT of the failure. For example,
// WBEM_E_LOCAL_CREDENTIALS (0x80041064) is thrown when user credentials
// (pszUserName, pszPassword) are specified for local connections.
// COR_E_UNAUTHORIZEDACCESS (0x80070005) is thrown because of incorrect
// user name or password.
// RPC_S_SERVER_UNAVAILABLE (0x800706BA) is usually caused by the firewall
// on the target machine that blocks the WMI connection or some network
// problem.
//
// EXAMPLE CALL:
// try
// {
// f64bitOS = Is64BitOperatingSystem(L".", NULL, NULL);
// wprintf(L"The current operating system %s 64-bit.\n",
// f64bitOS ? L"is" : L"is not");
// }
// catch (HRESULT hr)
// {
// wprintf(L"Is64BitOperatingSystem failed with HRESULT 0x%08lx\n", hr);
// }
//
BOOL Is64BitOperatingSystem(LPCWSTR pszMachineName, LPCWSTR pszUserName,
LPCWSTR pszPassword)
{
BOOL f64bitOS = FALSE;
HRESULT hr = S_OK;
IWbemLocator *pLoc = NULL;
IWbemServices *pSvc = NULL;
IEnumWbemClassObject *pEnumerator = NULL;
// Initialize COM parameters with a call to CoInitializeEx.
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
throw hr;
}
// Initialize COM process security by calling CoInitializeSecurity.
hr = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IDENTIFY, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hr))
{
goto Cleanup;
}
// Obtain the initial locator to WMI by calling CoCreateInstance.
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr))
{
goto Cleanup;
}
// Connect to WMI through the IWbemLocator::ConnectServer method.
// User credentials cannot be used for local connections.
if (pszMachineName == NULL)
{
pszMachineName = L".";
}
wchar_t szPath[200];
hr = StringCchPrintf(szPath, ARRAYSIZE(szPath), L"\\\\%s\\root\\cimv2",
pszMachineName);
if (FAILED(hr))
{
goto Cleanup;
}
hr = pLoc->ConnectServer(
_bstr_t(szPath), // Path of the WMI namespace
_bstr_t(pszUserName), // User name
_bstr_t(pszPassword), // User password
NULL, // Locale
NULL, // Security flags
NULL, // Authority
NULL, // Context object
&pSvc // IWbemServices proxy
);
if (FAILED(hr))
{
goto Cleanup;
}
// If you plan to connect to the remote with a different user name and
// password than the one you are currently using, build a COAUTHIDENTITY
// struct that can be used for setting security on proxy.
COAUTHIDENTITY *userAcct = NULL;
COAUTHIDENTITY authIdent;
wchar_t szDomain[CREDUI_MAX_USERNAME_LENGTH + 1];
wchar_t szUser[CREDUI_MAX_USERNAME_LENGTH + 1];
if (pszUserName != NULL)
{
ZeroMemory(&authIdent, sizeof(authIdent));
LPCWSTR slash = wcschr(pszUserName, L'\\');
if (slash == NULL)
{
// No domain info is available in the user name.
// Leave authIdent's Domain and DomainLength fields blank.
authIdent.User = (USHORT *)pszUserName;
authIdent.UserLength = wcslen(pszUserName);
}
else
{
StringCchCopy(szUser, CREDUI_MAX_USERNAME_LENGTH + 1, slash + 1);
authIdent.User = (USHORT *)szUser;
authIdent.UserLength = wcslen(szUser);
StringCchCopyN(szDomain, CREDUI_MAX_USERNAME_LENGTH + 1,
pszUserName, slash - pszUserName);
authIdent.Domain = (USHORT *)szDomain;
authIdent.DomainLength = slash - pszUserName;
}
authIdent.Password = (USHORT *)pszPassword;
authIdent.PasswordLength = wcslen(pszPassword);
authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
userAcct = &authIdent;
}
// Set security levels on the WMI connection.
hr = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
COLE_DEFAULT_PRINCIPAL, // Server principal name
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
userAcct, // Client identity
EOAC_NONE // Proxy capabilities
);
if (FAILED(hr))
{
goto Cleanup;
}
// Use the IWbemServices pointer to make requests of WMI. Query
// Win32_Processor.AddressWidth which dicates the current operating mode
// of the processor (on a 32-bit OS, it would be 32; on a 64-bit OS, it
// would be 64).
// Note: Win32_Processor.DataWidth indicates the capability of the
// processor. On a 64-bit processor, it is 64.
// Note: Win32_OperatingSystem.OSArchitecture tells the bitness of OS
// too. On a 32-bit OS, it would be "32-bit". However, it is only
// available on Windows Vista and newer OS.
hr = pSvc->ExecQuery(bstr_t(L"WQL"),
bstr_t(L"SELECT AddressWidth FROM Win32_Processor"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL, &pEnumerator);
if (FAILED(hr))
{
goto Cleanup;
}
// Secure the enumerator proxy.
hr = CoSetProxyBlanket(
pEnumerator, // Indicates the proxy to set
RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
COLE_DEFAULT_PRINCIPAL, // Server principal name
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
userAcct, // Client identity
EOAC_NONE // Proxy capabilities
);
if (FAILED(hr))
{
goto Cleanup;
}
// Get the data from the above query.
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
// Get one object.
HRESULT hrTmp = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the AddressWidth property.
hrTmp = pclsObj->Get(L"AddressWidth", 0, &vtProp, 0, 0);
if (SUCCEEDED(hrTmp))
{
if (vtProp.intVal == 64)
{
f64bitOS = TRUE;
}
VariantClear(&vtProp);
}
pclsObj->Release();
pclsObj = NULL;
}
Cleanup:
// Centralized cleanup for all allocated resources.
if (pLoc)
{
pLoc->Release();
pLoc = NULL;
}
if (pSvc)
{
pSvc->Release();
pSvc = NULL;
}
if (pEnumerator)
{
pEnumerator->Release();
pEnumerator = NULL;
}
CoUninitialize();
// Throw the error if something failed in the function.
if (FAILED(hr))
{
throw hr;
}
return f64bitOS;
}
//-------------------------------------------------------------------------------------------------
#pragma endregion
//-------------------------------------------------------------------------------------------------
int wmain(int argc, wchar_t* argv[])
{
// Solution 1. Is64BitOperatingSystem (IsWow64Process)
// Determine whether the current operating system is a 64 bit operating
// system.
BOOL f64bitOS = Is64BitOperatingSystem();
wprintf(L"1. The current operating system %s 64-bit.\n", f64bitOS ? L"is" : L"is not");
// Solution 2. Is64BitOperatingSystem (WMI)
// Determine whether the current operating system is a 64 bit operating
// system through WMI. Note: The first solution of using IsWow64Process
// is the preferred way to detect OS bitness of the current system
// because it is much easier and faster. The WMI solution is useful when
// you want to find this information on a remote system.
try
{
// If you want to get the OS bitness information of a remote system,
// configure the system for remote connections of WMI
// (http://msdn.microsoft.com/en-us/library/aa389290.aspx), and
// replace the parameters (L".", NULL, NULL) with the remote computer
// name and credentials for the connection.
f64bitOS = Is64BitOperatingSystem(L".", NULL, NULL);
wprintf(L"2. The current operating system %s 64-bit.\n", f64bitOS ? L"is" : L"is not");
}
catch (HRESULT hr)
{
wprintf(L"Is64BitOperatingSystem failed with HRESULT 0x%08lx\n", hr);
}
return 0;
}
//-------------------------------------------------------------------------------------------------