VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/RTSystemQueryDmiString-win.cpp@ 30013

Last change on this file since 30013 was 30013, checked in by vboxsync, 15 years ago

scm cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1/* $Id: RTSystemQueryDmiString-win.cpp 30013 2010-06-03 14:40:59Z 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 */
48static 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 if (hrc == RPC_E_TOO_LATE)
63 hrc = S_OK;
64 }
65 return hrc;
66}
67
68
69/**
70 * Undo what rtSystemDmiWinInitialize did.
71 */
72static void rtSystemDmiWinTerminate(void)
73{
74 CoUninitialize();
75}
76
77
78/**
79 * Convert a UTF-8 string to a BSTR.
80 *
81 * @returns BSTR pointer.
82 * @param psz The UTF-8 string.
83 */
84static BSTR rtSystemWinBstrFromUtf8(const char *psz)
85{
86 PRTUTF16 pwsz = NULL;
87 int rc = RTStrToUtf16(psz, &pwsz);
88 if (RT_FAILURE(rc))
89 return NULL;
90 BSTR pBStr = SysAllocString((const OLECHAR *)pwsz);
91 RTUtf16Free(pwsz);
92 return pBStr;
93}
94
95
96/**
97 * Connect to the DMI server.
98 *
99 * @returns COM status code.
100 * @param pLocator The locator.
101 * @param pszServer The server name.
102 * @param ppServices Where to return the services interface.
103 */
104static HRESULT rtSystemDmiWinConnectToServer(IWbemLocator *pLocator, const char *pszServer, IWbemServices **ppServices)
105{
106 AssertPtr(pLocator);
107 AssertPtrNull(pszServer);
108 AssertPtr(ppServices);
109
110 BSTR pBStrServer = rtSystemWinBstrFromUtf8(pszServer);
111 if (!pBStrServer)
112 return E_OUTOFMEMORY;
113
114 HRESULT hrc = pLocator->ConnectServer(pBStrServer,
115 NULL,
116 NULL,
117 0,
118 NULL,
119 0,
120 0,
121 ppServices);
122 if (SUCCEEDED(hrc))
123 {
124 hrc = CoSetProxyBlanket(*ppServices,
125 RPC_C_AUTHN_WINNT,
126 RPC_C_AUTHZ_NONE,
127 NULL,
128 RPC_C_AUTHN_LEVEL_CALL,
129 RPC_C_IMP_LEVEL_IMPERSONATE,
130 NULL,
131 EOAC_NONE);
132 if (FAILED(hrc))
133 (*ppServices)->Release();
134 }
135 SysFreeString(pBStrServer);
136 return hrc;
137}
138
139
140RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
141{
142 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
143 AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
144 *pszBuf = '\0';
145 AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
146
147 /*
148 * Figure the property name before we start.
149 */
150 const char *pszPropName;
151 switch (enmString)
152 {
153 case RTSYSDMISTR_PRODUCT_NAME: pszPropName = "Name"; break;
154 case RTSYSDMISTR_PRODUCT_VERSION: pszPropName = "Version"; break;
155 case RTSYSDMISTR_PRODUCT_UUID: pszPropName = "UUID"; break;
156 case RTSYSDMISTR_PRODUCT_SERIAL: pszPropName = "IdentifyingNumber"; break;
157 case RTSYSDMISTR_MANUFACTURER: pszPropName = "Vendor"; break;
158
159 default:
160 return VERR_NOT_SUPPORTED;
161 }
162
163 /*
164 * Before we do anything with COM, we have to initalize it.
165 */
166 HRESULT hrc = rtSystemDmiWinInitialize();
167 if (FAILED(hrc))
168 return VERR_NOT_SUPPORTED;
169
170 int rc = VERR_NOT_SUPPORTED;
171 BSTR pBstrPropName = rtSystemWinBstrFromUtf8(pszPropName);
172 if (pBstrPropName)
173 {
174 /*
175 * Instantiate the IWbemLocator, whatever that is and connect to the
176 * DMI serve.
177 */
178 IWbemLocator *pLoc;
179 hrc = CoCreateInstance(CLSID_WbemLocator,
180 0,
181 CLSCTX_INPROC_SERVER,
182 IID_IWbemLocator,
183 (LPVOID *)&pLoc);
184 if (SUCCEEDED(hrc))
185 {
186 IWbemServices *pServices;
187 hrc = rtSystemDmiWinConnectToServer(pLoc, "ROOT\\CIMV2", &pServices);
188 if (SUCCEEDED(hrc))
189 {
190 /*
191 * Enumerate whatever it is we're looking at and try get
192 * the desired property.
193 */
194 BSTR pBstrFilter = rtSystemWinBstrFromUtf8("Win32_ComputerSystemProduct");
195 if (pBstrFilter)
196 {
197 IEnumWbemClassObject *pEnum;
198 hrc = pServices->CreateInstanceEnum(pBstrFilter, 0, NULL, &pEnum);
199 if (SUCCEEDED(hrc))
200 {
201 do
202 {
203 IWbemClassObject *pObj;
204 ULONG cObjRet;
205 hrc = pEnum->Next(WBEM_INFINITE, 1, &pObj, &cObjRet);
206 if ( SUCCEEDED(hrc)
207 && cObjRet >= 1)
208 {
209 VARIANT Var;
210 VariantInit(&Var);
211 hrc = pObj->Get(pBstrPropName, 0, &Var, 0, 0);
212 if ( SUCCEEDED(hrc)
213 && V_VT(&Var) == VT_BSTR)
214 {
215 /*
216 * Convert the BSTR to UTF-8 and copy it
217 * into the return buffer.
218 */
219 char *pszValue;
220 rc = RTUtf16ToUtf8(Var.bstrVal, &pszValue);
221 if (RT_SUCCESS(rc))
222 {
223 rc = RTStrCopy(pszBuf, cbBuf, pszValue);
224 RTStrFree(pszValue);
225 hrc = WBEM_S_FALSE;
226 }
227 }
228 VariantClear(&Var);
229 pObj->Release();
230 }
231 } while (hrc != WBEM_S_FALSE);
232
233 pEnum->Release();
234 }
235 SysFreeString(pBstrFilter);
236 }
237 else
238 hrc = E_OUTOFMEMORY;
239 pServices->Release();
240 }
241 pLoc->Release();
242 }
243 SysFreeString(pBstrPropName);
244 }
245 else
246 hrc = E_OUTOFMEMORY;
247 rtSystemDmiWinTerminate();
248 if (FAILED(hrc) && rc == VERR_NOT_SUPPORTED)
249 rc = VERR_NOT_SUPPORTED;
250 return rc;
251}
252
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette