1 | /* $Id: RTSystemQueryDmiString-win.cpp 26645 2010-02-19 11:53:51Z 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 | #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 | #define _WIN32_DCOM
|
---|
43 | #include <comdef.h>
|
---|
44 | #include <Wbemidl.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | HRESULT rtSystemInitializeDmiLookup()
|
---|
48 | {
|
---|
49 | HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
|
---|
50 | if (SUCCEEDED(hr))
|
---|
51 | {
|
---|
52 | hr = CoInitializeSecurity(NULL,
|
---|
53 | -1, /* COM authentication. */
|
---|
54 | NULL, /* Which authentication services. */
|
---|
55 | NULL, /* Reserved. */
|
---|
56 | RPC_C_AUTHN_LEVEL_DEFAULT, /* Default authentication. */
|
---|
57 | RPC_C_IMP_LEVEL_IMPERSONATE, /* Default impersonation. */
|
---|
58 | NULL, /* Authentication info. */
|
---|
59 | EOAC_NONE, /* Additional capabilities. */
|
---|
60 | NULL); /* Reserved. */
|
---|
61 | }
|
---|
62 | return hr;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | void rtSystemShutdownDmiLookup()
|
---|
67 | {
|
---|
68 | CoUninitialize();
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | HRESULT rtSystemConnectToDmiServer(IWbemLocator *pLocator, const char *pszServer, IWbemServices **ppServices)
|
---|
73 | {
|
---|
74 | AssertPtr(pLocator);
|
---|
75 | AssertPtrNull(pszServer);
|
---|
76 | AssertPtr(ppServices);
|
---|
77 |
|
---|
78 | HRESULT hr = pLocator->ConnectServer(_bstr_t(TEXT(pszServer)),
|
---|
79 | NULL,
|
---|
80 | NULL,
|
---|
81 | 0,
|
---|
82 | NULL,
|
---|
83 | 0,
|
---|
84 | 0,
|
---|
85 | ppServices);
|
---|
86 | if (SUCCEEDED(hr))
|
---|
87 | {
|
---|
88 | hr = CoSetProxyBlanket(*ppServices,
|
---|
89 | RPC_C_AUTHN_WINNT,
|
---|
90 | RPC_C_AUTHZ_NONE,
|
---|
91 | NULL,
|
---|
92 | RPC_C_AUTHN_LEVEL_CALL,
|
---|
93 | RPC_C_IMP_LEVEL_IMPERSONATE,
|
---|
94 | NULL,
|
---|
95 | EOAC_NONE);
|
---|
96 | }
|
---|
97 | return hr;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
|
---|
102 | {
|
---|
103 | AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
|
---|
104 | AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
|
---|
105 | *pszBuf = '\0';
|
---|
106 | AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
|
---|
107 |
|
---|
108 | HRESULT hr = rtSystemInitializeDmiLookup();
|
---|
109 | if (FAILED(hr))
|
---|
110 | return VERR_NOT_SUPPORTED;
|
---|
111 |
|
---|
112 | IWbemLocator *pLoc;
|
---|
113 | hr = CoCreateInstance(CLSID_WbemLocator,
|
---|
114 | 0,
|
---|
115 | CLSCTX_INPROC_SERVER,
|
---|
116 | IID_IWbemLocator, (LPVOID *)&pLoc);
|
---|
117 | int rc = VINF_SUCCESS;
|
---|
118 | if (SUCCEEDED(hr))
|
---|
119 | {
|
---|
120 | IWbemServices *pServices;
|
---|
121 | hr = rtSystemConnectToDmiServer(pLoc, "ROOT\\CIMV2", &pServices);
|
---|
122 | if (SUCCEEDED(hr))
|
---|
123 | {
|
---|
124 | IEnumWbemClassObject *pEnum;
|
---|
125 | hr = pServices->CreateInstanceEnum(L"Win32_ComputerSystemProduct", 0, NULL, &pEnum);
|
---|
126 | if (SUCCEEDED(hr))
|
---|
127 | {
|
---|
128 | IWbemClassObject *pObj;
|
---|
129 | ULONG uCount;
|
---|
130 |
|
---|
131 | do
|
---|
132 | {
|
---|
133 | hr = pEnum->Next(WBEM_INFINITE,
|
---|
134 | 1,
|
---|
135 | &pObj,
|
---|
136 | &uCount);
|
---|
137 | if ( SUCCEEDED(hr)
|
---|
138 | && uCount > 0)
|
---|
139 | {
|
---|
140 | const char *pszPropName;
|
---|
141 | switch (enmString)
|
---|
142 | {
|
---|
143 | case RTSYSDMISTR_PRODUCT_NAME: pszPropName = "Name"; break;
|
---|
144 | case RTSYSDMISTR_PRODUCT_VERSION: pszPropName = "Version"; break;
|
---|
145 | case RTSYSDMISTR_PRODUCT_UUID: pszPropName = "UUID"; break;
|
---|
146 | case RTSYSDMISTR_PRODUCT_SERIAL: pszPropName = "IdentifyingNumber"; break;
|
---|
147 | default:
|
---|
148 | rc = VERR_NOT_SUPPORTED;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (RT_SUCCESS(rc))
|
---|
152 | {
|
---|
153 | _variant_t v;
|
---|
154 | hr = pObj->Get(_bstr_t(TEXT(pszPropName)), 0, &v, 0, 0);
|
---|
155 | if ( SUCCEEDED(hr)
|
---|
156 | && V_VT(&v) == VT_BSTR)
|
---|
157 | {
|
---|
158 | RTStrPrintf(pszBuf, cbBuf, "%s", (char*)_bstr_t(v.bstrVal));
|
---|
159 | VariantClear(&v);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | pObj->Release();
|
---|
163 | }
|
---|
164 | } while(hr != WBEM_S_FALSE);
|
---|
165 | pEnum->Release();
|
---|
166 | }
|
---|
167 | pServices->Release();
|
---|
168 | }
|
---|
169 | pLoc->Release();
|
---|
170 | }
|
---|
171 |
|
---|
172 | rtSystemShutdownDmiLookup();
|
---|
173 | if (FAILED(hr))
|
---|
174 | rc = VERR_NOT_FOUND; /** @todo Find a better error, since neither of the RTErrConvert* can do the conversion here. */
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 | RT_EXPORT_SYMBOL(RTSystemQueryDmiString);
|
---|
178 |
|
---|
179 |
|
---|