EDN Admin
Well-known member
I am using WMI to get the userName on windows 7 machine. The same code works on XP but on windows 7 i get the usreName as null. Any thought on this?
here is the code:
<span style="font-size:x-small <span style="font-size:x-small
sObjectPath = "Root\CIMV2
sParam = "UserName"
squery = "SELECT * FROM Win32_ComputerSystem"
<font size="2
tstring sValue = _T(
</font>
<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try
<span style="font-size:x-small
{
HRESULT hres;
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj = NULL;
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 1: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Initialize COM. ------------------------------------------
<span style="font-size:x-small
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 2: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Set general COM security levels --------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Note: If you are using Windows 2000, you need to specify -
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // the default authentication credentials for a user by using
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // parameter of CoInitializeSecurity ------------------------
<span style="font-size:x-small
hres = CoInitializeSecurity(
NULL,
<font size="2
-1,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // COM authentication
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Authentication services
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Reserved
<span style="font-size:x-small <font size="2
RPC_C_AUTHN_LEVEL_DEFAULT,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Default authentication
<span style="font-size:x-small <font size="2
RPC_C_IMP_LEVEL_IMPERSONATE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Default Impersonation
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Authentication info
<span style="font-size:x-small <font size="2
EOAC_NONE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Additional capabilities
<span style="font-size:x-small <font size="2
NULL
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Reserved
<span style="font-size:x-small
);
DWORD dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 3: ---------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Obtain the initial locator to WMI -------------------------
<span style="font-size:x-small
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 4: -----------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Connect to WMI through the IWbemLocator::ConnectServer method
<span style="font-size:x-small
IWbemServices *pSvc = NULL;
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try
<span style="font-size:x-small
{
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Connect to the rootcimv2 namespace with
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // the current user and obtain pointer pSvc
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // to make IWbemServices calls.
<span style="font-size:x-small
hres = pLoc->ConnectServer(
<font size="2
_bstr_t(sObjectPath.c_str()),
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Object path of WMI namespace
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // User name. NULL = current user
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // User password. NULL = current
<span style="font-size:x-small <font size="2
0,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Locale. NULL indicates current
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Security flags.
<span style="font-size:x-small <font size="2
0,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Authority (e.g. Kerberos)
<span style="font-size:x-small <font size="2
0,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Context object
<span style="font-size:x-small <font size="2
&pSvc
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // pointer to IWbemServices proxy
<span style="font-size:x-small
);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 5: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Set security levels on the proxy -------------------------
<span style="font-size:x-small
hres = CoSetProxyBlanket(
<font size="2
pSvc,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Indicates the proxy to set
<span style="font-size:x-small <font size="2
RPC_C_AUTHN_WINNT,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_AUTHN_xxx
<span style="font-size:x-small <font size="2
RPC_C_AUTHZ_NONE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_AUTHZ_xxx
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Server principal name
<span style="font-size:x-small <font size="2
RPC_C_AUTHN_LEVEL_CALL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_AUTHN_LEVEL_xxx
<span style="font-size:x-small <font size="2
RPC_C_IMP_LEVEL_IMPERSONATE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_IMP_LEVEL_xxx
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // client identity
<span style="font-size:x-small <font size="2
EOAC_NONE
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // proxy capabilities
<span style="font-size:x-small
);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 6: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Use the IWbemServices pointer to make requests of WMI ----
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // For example, get the name of the operating system
<span style="font-size:x-small
hres = pSvc->ExecQuery(
<font size="2
bstr_t(
</font>
<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 "WQL"<span style="font-size:x-small ),
bstr_t(sQuery.c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 7: -------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Get the data from the query in step 6 -------------------
<span style="font-size:x-small
ULONG uReturn = 0;
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff while<span style="font-size:x-small (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (0 == uReturn)
{
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff break<span style="font-size:x-small ;
}
VARIANT vtProp;
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Get the value of the Name property
<span style="font-size:x-small
hr = pclsObj->Get((BSTR)sParam.c_str(), 0, &vtProp, 0, 0);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (vtProp.vt != VT_NULL)
sValue = vtProp.bstrVal;
VariantClear(&vtProp);
pclsObj->Release();
}
}
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...)
{
<font size="2
CUtils::AddToLog_Error(_T(
</font>
<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 "Exception in GetWMIValue: "<span style="font-size:x-small ) + sParam, MSG_GENERAL);
}
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Cleanup
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // ========
<span style="font-size:x-small
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pSvc)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pSvc->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pLoc)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pLoc->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pEnumerator)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pEnumerator->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pclsObj)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pclsObj->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}
CoUninitialize();
}
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...)
{
<font size="2
</font>
<span style="font-size:x-small
}
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small sValue;
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Program successfully completed.
View the full article
here is the code:
<span style="font-size:x-small <span style="font-size:x-small
sObjectPath = "Root\CIMV2
sParam = "UserName"
squery = "SELECT * FROM Win32_ComputerSystem"
<font size="2
tstring sValue = _T(
</font>
<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try
<span style="font-size:x-small
{
HRESULT hres;
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj = NULL;
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 1: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Initialize COM. ------------------------------------------
<span style="font-size:x-small
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 2: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Set general COM security levels --------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Note: If you are using Windows 2000, you need to specify -
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // the default authentication credentials for a user by using
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // parameter of CoInitializeSecurity ------------------------
<span style="font-size:x-small
hres = CoInitializeSecurity(
NULL,
<font size="2
-1,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // COM authentication
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Authentication services
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Reserved
<span style="font-size:x-small <font size="2
RPC_C_AUTHN_LEVEL_DEFAULT,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Default authentication
<span style="font-size:x-small <font size="2
RPC_C_IMP_LEVEL_IMPERSONATE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Default Impersonation
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Authentication info
<span style="font-size:x-small <font size="2
EOAC_NONE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Additional capabilities
<span style="font-size:x-small <font size="2
NULL
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Reserved
<span style="font-size:x-small
);
DWORD dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 3: ---------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Obtain the initial locator to WMI -------------------------
<span style="font-size:x-small
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 4: -----------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Connect to WMI through the IWbemLocator::ConnectServer method
<span style="font-size:x-small
IWbemServices *pSvc = NULL;
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try
<span style="font-size:x-small
{
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Connect to the rootcimv2 namespace with
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // the current user and obtain pointer pSvc
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // to make IWbemServices calls.
<span style="font-size:x-small
hres = pLoc->ConnectServer(
<font size="2
_bstr_t(sObjectPath.c_str()),
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Object path of WMI namespace
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // User name. NULL = current user
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // User password. NULL = current
<span style="font-size:x-small <font size="2
0,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Locale. NULL indicates current
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Security flags.
<span style="font-size:x-small <font size="2
0,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Authority (e.g. Kerberos)
<span style="font-size:x-small <font size="2
0,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Context object
<span style="font-size:x-small <font size="2
&pSvc
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // pointer to IWbemServices proxy
<span style="font-size:x-small
);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 5: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Set security levels on the proxy -------------------------
<span style="font-size:x-small
hres = CoSetProxyBlanket(
<font size="2
pSvc,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Indicates the proxy to set
<span style="font-size:x-small <font size="2
RPC_C_AUTHN_WINNT,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_AUTHN_xxx
<span style="font-size:x-small <font size="2
RPC_C_AUTHZ_NONE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_AUTHZ_xxx
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Server principal name
<span style="font-size:x-small <font size="2
RPC_C_AUTHN_LEVEL_CALL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_AUTHN_LEVEL_xxx
<span style="font-size:x-small <font size="2
RPC_C_IMP_LEVEL_IMPERSONATE,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // RPC_C_IMP_LEVEL_xxx
<span style="font-size:x-small <font size="2
NULL,
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // client identity
<span style="font-size:x-small <font size="2
EOAC_NONE
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // proxy capabilities
<span style="font-size:x-small
);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 6: --------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Use the IWbemServices pointer to make requests of WMI ----
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // For example, get the name of the operating system
<span style="font-size:x-small
hres = pSvc->ExecQuery(
<font size="2
bstr_t(
</font>
<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 "WQL"<span style="font-size:x-small ),
bstr_t(sQuery.c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small _T(<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 ""<span style="font-size:x-small );
}
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Step 7: -------------------------------------------------
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Get the data from the query in step 6 -------------------
<span style="font-size:x-small
ULONG uReturn = 0;
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff while<span style="font-size:x-small (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
dw = GetLastError();
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (0 == uReturn)
{
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff break<span style="font-size:x-small ;
}
VARIANT vtProp;
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Get the value of the Name property
<span style="font-size:x-small
hr = pclsObj->Get((BSTR)sParam.c_str(), 0, &vtProp, 0, 0);
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (vtProp.vt != VT_NULL)
sValue = vtProp.bstrVal;
VariantClear(&vtProp);
pclsObj->Release();
}
}
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...)
{
<font size="2
CUtils::AddToLog_Error(_T(
</font>
<span style="font-size:x-small; color:#a31515 <span style="font-size:x-small; color:#a31515 "Exception in GetWMIValue: "<span style="font-size:x-small ) + sParam, MSG_GENERAL);
}
<font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Cleanup
<span style="font-size:x-small <font size="2
</font>
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // ========
<span style="font-size:x-small
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pSvc)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pSvc->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pLoc)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pLoc->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pEnumerator)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pEnumerator->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff if<span style="font-size:x-small (pclsObj)<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff try<span style="font-size:x-small {pclsObj->Release();}<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...){}
CoUninitialize();
}
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff catch<span style="font-size:x-small (...)
{
<font size="2
</font>
<span style="font-size:x-small
}
<font size="2
</font>
<span style="font-size:x-small; color:#0000ff <span style="font-size:x-small; color:#0000ff return<span style="font-size:x-small sValue;
<span style="font-size:x-small; color:#008000 <span style="font-size:x-small; color:#008000 // Program successfully completed.
View the full article