1 | /* $Id: RTSystemQueryDmiString-win.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSystemQueryDmiString, windows ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #define _WIN32_DCOM
|
---|
32 | #include <Windows.h>
|
---|
33 | #include <WbemCli.h>
|
---|
34 |
|
---|
35 | #include <iprt/system.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Initialize COM.
|
---|
45 | *
|
---|
46 | * @returns COM status code.
|
---|
47 | */
|
---|
48 | static HRESULT rtSystemDmiWinInitialize(void)
|
---|
49 | {
|
---|
50 | HRESULT hrc = CoInitializeEx(0, COINIT_MULTITHREADED);
|
---|
51 | if (SUCCEEDED(hrc))
|
---|
52 | {
|
---|
53 | hrc = CoInitializeSecurity(NULL,
|
---|
54 | -1, /* COM authentication. */
|
---|
55 | NULL, /* Which authentication services. */
|
---|
56 | NULL, /* Reserved. */
|
---|
57 | RPC_C_AUTHN_LEVEL_DEFAULT, /* Default authentication. */
|
---|
58 | RPC_C_IMP_LEVEL_IMPERSONATE, /* Default impersonation. */
|
---|
59 | NULL, /* Authentication info. */
|
---|
60 | EOAC_NONE, /* Additional capabilities. */
|
---|
61 | NULL); /* Reserved. */
|
---|
62 | }
|
---|
63 | return hrc;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Undo what rtSystemDmiWinInitialize did.
|
---|
69 | */
|
---|
70 | static void rtSystemDmiWinTerminate(void)
|
---|
71 | {
|
---|
72 | CoUninitialize();
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Convert a UTF-8 string to a BSTR.
|
---|
78 | *
|
---|
79 | * @returns BSTR pointer.
|
---|
80 | * @param psz The UTF-8 string.
|
---|
81 | */
|
---|
82 | static BSTR rtSystemWinBstrFromUtf8(const char *psz)
|
---|
83 | {
|
---|
84 | PRTUTF16 pwsz = NULL;
|
---|
85 | int rc = RTStrToUtf16(psz, &pwsz);
|
---|
86 | if (RT_FAILURE(rc))
|
---|
87 | return NULL;
|
---|
88 | BSTR pBStr = SysAllocString((const OLECHAR *)pwsz);
|
---|
89 | RTUtf16Free(pwsz);
|
---|
90 | return pBStr;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Connect to the DMI server.
|
---|
96 | *
|
---|
97 | * @returns COM status code.
|
---|
98 | * @param pLocator The locator.
|
---|
99 | * @param pszServer The server name.
|
---|
100 | * @param ppServices Where to return the services interface.
|
---|
101 | */
|
---|
102 | static HRESULT rtSystemDmiWinConnectToServer(IWbemLocator *pLocator, const char *pszServer, IWbemServices **ppServices)
|
---|
103 | {
|
---|
104 | AssertPtr(pLocator);
|
---|
105 | AssertPtrNull(pszServer);
|
---|
106 | AssertPtr(ppServices);
|
---|
107 |
|
---|
108 | BSTR pBStrServer = rtSystemWinBstrFromUtf8(pszServer);
|
---|
109 | if (!pBStrServer)
|
---|
110 | return E_OUTOFMEMORY;
|
---|
111 |
|
---|
112 | HRESULT hrc = pLocator->ConnectServer(pBStrServer,
|
---|
113 | NULL,
|
---|
114 | NULL,
|
---|
115 | 0,
|
---|
116 | NULL,
|
---|
117 | 0,
|
---|
118 | 0,
|
---|
119 | ppServices);
|
---|
120 | if (SUCCEEDED(hrc))
|
---|
121 | {
|
---|
122 | hrc = CoSetProxyBlanket(*ppServices,
|
---|
123 | RPC_C_AUTHN_WINNT,
|
---|
124 | RPC_C_AUTHZ_NONE,
|
---|
125 | NULL,
|
---|
126 | RPC_C_AUTHN_LEVEL_CALL,
|
---|
127 | RPC_C_IMP_LEVEL_IMPERSONATE,
|
---|
128 | NULL,
|
---|
129 | EOAC_NONE);
|
---|
130 | if (FAILED(hrc))
|
---|
131 | (*ppServices)->Release();
|
---|
132 | }
|
---|
133 | SysFreeString(pBStrServer);
|
---|
134 | return hrc;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
|
---|
139 | {
|
---|
140 | AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
|
---|
141 | AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
|
---|
142 | *pszBuf = '\0';
|
---|
143 | AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Figure the property name before we start.
|
---|
147 | */
|
---|
148 | const char *pszPropName;
|
---|
149 | switch (enmString)
|
---|
150 | {
|
---|
151 | case RTSYSDMISTR_PRODUCT_NAME: pszPropName = "Name"; break;
|
---|
152 | case RTSYSDMISTR_PRODUCT_VERSION: pszPropName = "Version"; break;
|
---|
153 | case RTSYSDMISTR_PRODUCT_UUID: pszPropName = "UUID"; break;
|
---|
154 | case RTSYSDMISTR_PRODUCT_SERIAL: pszPropName = "IdentifyingNumber"; break;
|
---|
155 | default:
|
---|
156 | return VERR_NOT_SUPPORTED;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Before we do anything with COM, we have to initalize it.
|
---|
161 | */
|
---|
162 | HRESULT hrc = rtSystemDmiWinInitialize();
|
---|
163 | if (FAILED(hrc))
|
---|
164 | return VERR_NOT_SUPPORTED;
|
---|
165 |
|
---|
166 | int rc = VERR_NOT_SUPPORTED;
|
---|
167 | BSTR pBstrPropName = rtSystemWinBstrFromUtf8(pszPropName);
|
---|
168 | if (pBstrPropName)
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * Instantiate the IWbemLocator, whatever that is and connect to the
|
---|
172 | * DMI serve.
|
---|
173 | */
|
---|
174 | IWbemLocator *pLoc;
|
---|
175 | hrc = CoCreateInstance(CLSID_WbemLocator,
|
---|
176 | 0,
|
---|
177 | CLSCTX_INPROC_SERVER,
|
---|
178 | IID_IWbemLocator,
|
---|
179 | (LPVOID *)&pLoc);
|
---|
180 | if (SUCCEEDED(hrc))
|
---|
181 | {
|
---|
182 | IWbemServices *pServices;
|
---|
183 | hrc = rtSystemDmiWinConnectToServer(pLoc, "ROOT\\CIMV2", &pServices);
|
---|
184 | if (SUCCEEDED(hrc))
|
---|
185 | {
|
---|
186 | /*
|
---|
187 | * Enumerate whatever it is we're looking at and try get
|
---|
188 | * the desired property.
|
---|
189 | */
|
---|
190 | BSTR pBstrFilter = rtSystemWinBstrFromUtf8("Win32_ComputerSystemProduct");
|
---|
191 | if (pBstrFilter)
|
---|
192 | {
|
---|
193 | IEnumWbemClassObject *pEnum;
|
---|
194 | hrc = pServices->CreateInstanceEnum(pBstrFilter, 0, NULL, &pEnum);
|
---|
195 | if (SUCCEEDED(hrc))
|
---|
196 | {
|
---|
197 | do
|
---|
198 | {
|
---|
199 | IWbemClassObject *pObj;
|
---|
200 | ULONG cObjRet;
|
---|
201 | hrc = pEnum->Next(WBEM_INFINITE, 1, &pObj, &cObjRet);
|
---|
202 | if ( SUCCEEDED(hrc)
|
---|
203 | && cObjRet >= 1)
|
---|
204 | {
|
---|
205 | VARIANT Var;
|
---|
206 | VariantInit(&Var);
|
---|
207 | hrc = pObj->Get(pBstrPropName, 0, &Var, 0, 0);
|
---|
208 | if ( SUCCEEDED(hrc)
|
---|
209 | && V_VT(&Var) == VT_BSTR)
|
---|
210 | {
|
---|
211 | /*
|
---|
212 | * Convert the BSTR to UTF-8 and copy it
|
---|
213 | * into the return buffer.
|
---|
214 | */
|
---|
215 | char *pszValue;
|
---|
216 | rc = RTUtf16ToUtf8(Var.bstrVal, &pszValue);
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | rc = RTStrCopy(pszBuf, cbBuf, pszValue);
|
---|
220 | RTStrFree(pszValue);
|
---|
221 | hrc = WBEM_S_FALSE;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | VariantClear(&Var);
|
---|
225 | pObj->Release();
|
---|
226 | }
|
---|
227 | } while (hrc != WBEM_S_FALSE);
|
---|
228 |
|
---|
229 | pEnum->Release();
|
---|
230 | }
|
---|
231 | SysFreeString(pBstrFilter);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | hrc = E_OUTOFMEMORY;
|
---|
235 | pServices->Release();
|
---|
236 | }
|
---|
237 | pLoc->Release();
|
---|
238 | }
|
---|
239 | SysFreeString(pBstrPropName);
|
---|
240 | }
|
---|
241 | else
|
---|
242 | hrc = E_OUTOFMEMORY;
|
---|
243 | rtSystemDmiWinTerminate();
|
---|
244 | if (FAILED(hrc) && rc == VERR_NOT_SUPPORTED)
|
---|
245 | rc = VERR_NOT_SUPPORTED;
|
---|
246 | return rc;
|
---|
247 | }
|
---|
248 |
|
---|