VirtualBox

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

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

RTSystemQueryDmiString: Added RTSYSDMISTR_MANUFACTURER and implemented it on a few systems.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/* $Id: RTSystemQueryDmiString-win.cpp 29560 2010-05-17 15:08:09Z 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 }
63 return hrc;
64}
65
66
67/**
68 * Undo what rtSystemDmiWinInitialize did.
69 */
70static 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 */
82static 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 */
102static 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
138RTDECL(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 /// @todo case RTSYSDMISTR_MANUFACTURER: pszPropName = "Vendor"; break;
156
157 default:
158 return VERR_NOT_SUPPORTED;
159 }
160
161 /*
162 * Before we do anything with COM, we have to initalize it.
163 */
164 HRESULT hrc = rtSystemDmiWinInitialize();
165 if (FAILED(hrc))
166 return VERR_NOT_SUPPORTED;
167
168 int rc = VERR_NOT_SUPPORTED;
169 BSTR pBstrPropName = rtSystemWinBstrFromUtf8(pszPropName);
170 if (pBstrPropName)
171 {
172 /*
173 * Instantiate the IWbemLocator, whatever that is and connect to the
174 * DMI serve.
175 */
176 IWbemLocator *pLoc;
177 hrc = CoCreateInstance(CLSID_WbemLocator,
178 0,
179 CLSCTX_INPROC_SERVER,
180 IID_IWbemLocator,
181 (LPVOID *)&pLoc);
182 if (SUCCEEDED(hrc))
183 {
184 IWbemServices *pServices;
185 hrc = rtSystemDmiWinConnectToServer(pLoc, "ROOT\\CIMV2", &pServices);
186 if (SUCCEEDED(hrc))
187 {
188 /*
189 * Enumerate whatever it is we're looking at and try get
190 * the desired property.
191 */
192 BSTR pBstrFilter = rtSystemWinBstrFromUtf8("Win32_ComputerSystemProduct");
193 if (pBstrFilter)
194 {
195 IEnumWbemClassObject *pEnum;
196 hrc = pServices->CreateInstanceEnum(pBstrFilter, 0, NULL, &pEnum);
197 if (SUCCEEDED(hrc))
198 {
199 do
200 {
201 IWbemClassObject *pObj;
202 ULONG cObjRet;
203 hrc = pEnum->Next(WBEM_INFINITE, 1, &pObj, &cObjRet);
204 if ( SUCCEEDED(hrc)
205 && cObjRet >= 1)
206 {
207 VARIANT Var;
208 VariantInit(&Var);
209 hrc = pObj->Get(pBstrPropName, 0, &Var, 0, 0);
210 if ( SUCCEEDED(hrc)
211 && V_VT(&Var) == VT_BSTR)
212 {
213 /*
214 * Convert the BSTR to UTF-8 and copy it
215 * into the return buffer.
216 */
217 char *pszValue;
218 rc = RTUtf16ToUtf8(Var.bstrVal, &pszValue);
219 if (RT_SUCCESS(rc))
220 {
221 rc = RTStrCopy(pszBuf, cbBuf, pszValue);
222 RTStrFree(pszValue);
223 hrc = WBEM_S_FALSE;
224 }
225 }
226 VariantClear(&Var);
227 pObj->Release();
228 }
229 } while (hrc != WBEM_S_FALSE);
230
231 pEnum->Release();
232 }
233 SysFreeString(pBstrFilter);
234 }
235 else
236 hrc = E_OUTOFMEMORY;
237 pServices->Release();
238 }
239 pLoc->Release();
240 }
241 SysFreeString(pBstrPropName);
242 }
243 else
244 hrc = E_OUTOFMEMORY;
245 rtSystemDmiWinTerminate();
246 if (FAILED(hrc) && rc == VERR_NOT_SUPPORTED)
247 rc = VERR_NOT_SUPPORTED;
248 return rc;
249}
250
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