VirtualBox

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

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

iprt: Enabled RTSystemQueryDmiString-win.cpp. Fixed the header from the DDK instead of the SDK issue, by dropping the C++ conveniences and just program COM the good old C way. Btw. TEXT doesn't work on variables.

  • 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 27618 2010-03-23 04:39:41Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryDmiString, windows ring-3.
4 */
5
6/*
7 * Copyright (C) 2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define _WIN32_DCOM
36#include <Windows.h>
37#include <WbemCli.h>
38
39#include <iprt/system.h>
40#include "internal/iprt.h"
41
42#include <iprt/err.h>
43#include <iprt/assert.h>
44#include <iprt/string.h>
45
46
47/**
48 * Initialize COM.
49 *
50 * @returns COM status code.
51 */
52static HRESULT rtSystemDmiWinInitialize(void)
53{
54 HRESULT hrc = CoInitializeEx(0, COINIT_MULTITHREADED);
55 if (SUCCEEDED(hrc))
56 {
57 hrc = CoInitializeSecurity(NULL,
58 -1, /* COM authentication. */
59 NULL, /* Which authentication services. */
60 NULL, /* Reserved. */
61 RPC_C_AUTHN_LEVEL_DEFAULT, /* Default authentication. */
62 RPC_C_IMP_LEVEL_IMPERSONATE, /* Default impersonation. */
63 NULL, /* Authentication info. */
64 EOAC_NONE, /* Additional capabilities. */
65 NULL); /* Reserved. */
66 }
67 return hrc;
68}
69
70
71/**
72 * Undo what rtSystemDmiWinInitialize did.
73 */
74static void rtSystemDmiWinTerminate(void)
75{
76 CoUninitialize();
77}
78
79
80/**
81 * Convert a UTF-8 string to a BSTR.
82 *
83 * @returns BSTR pointer.
84 * @param psz The UTF-8 string.
85 */
86static BSTR rtSystemWinBstrFromUtf8(const char *psz)
87{
88 PRTUTF16 pwsz = NULL;
89 int rc = RTStrToUtf16(psz, &pwsz);
90 if (RT_FAILURE(rc))
91 return NULL;
92 BSTR pBStr = SysAllocString((const OLECHAR *)pwsz);
93 RTUtf16Free(pwsz);
94 return pBStr;
95}
96
97
98/**
99 * Connect to the DMI server.
100 *
101 * @returns COM status code.
102 * @param pLocator The locator.
103 * @param pszServer The server name.
104 * @param ppServices Where to return the services interface.
105 */
106static HRESULT rtSystemDmiWinConnectToServer(IWbemLocator *pLocator, const char *pszServer, IWbemServices **ppServices)
107{
108 AssertPtr(pLocator);
109 AssertPtrNull(pszServer);
110 AssertPtr(ppServices);
111
112 BSTR pBStrServer = rtSystemWinBstrFromUtf8(pszServer);
113 if (!pBStrServer)
114 return E_OUTOFMEMORY;
115
116 HRESULT hrc = pLocator->ConnectServer(pBStrServer,
117 NULL,
118 NULL,
119 0,
120 NULL,
121 0,
122 0,
123 ppServices);
124 if (SUCCEEDED(hrc))
125 {
126 hrc = CoSetProxyBlanket(*ppServices,
127 RPC_C_AUTHN_WINNT,
128 RPC_C_AUTHZ_NONE,
129 NULL,
130 RPC_C_AUTHN_LEVEL_CALL,
131 RPC_C_IMP_LEVEL_IMPERSONATE,
132 NULL,
133 EOAC_NONE);
134 if (FAILED(hrc))
135 (*ppServices)->Release();
136 }
137 SysFreeString(pBStrServer);
138 return hrc;
139}
140
141
142RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
143{
144 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
145 AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
146 *pszBuf = '\0';
147 AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
148
149 /*
150 * Figure the property name before we start.
151 */
152 const char *pszPropName;
153 switch (enmString)
154 {
155 case RTSYSDMISTR_PRODUCT_NAME: pszPropName = "Name"; break;
156 case RTSYSDMISTR_PRODUCT_VERSION: pszPropName = "Version"; break;
157 case RTSYSDMISTR_PRODUCT_UUID: pszPropName = "UUID"; break;
158 case RTSYSDMISTR_PRODUCT_SERIAL: pszPropName = "IdentifyingNumber"; break;
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.

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