2013-01-23 51 views
1

我想檢索知道其SID的用戶AccountName。使用WMI查詢得到 的SID:Select * from Win32_UserProfile,我嘗試使用下面的查詢,以獲取用戶帳戶名:C++ WMI獲取AccountName知道用戶SID

Select * from Win32_SID where SID='S-1-5-21-3949351935-1180888718-2463404063-9346'

ExecQuery方法成功,但是從IEnumWbemClassObjectNext方法失敗與錯誤:H80041024(wbemErrProviderNotCapable)。

任何幫助將是偉大的。謝謝。

+0

另外,我想找到XP這種方法是等效的。 – user1019710

回答

3

正如MSDN文檔所述,Win32_SID WMI類不能被枚舉。

因此,您不能使用ExecQuery方法,而應使用通過正確的WMI object pathIWbemServices::GetObject函數。像Win32_SID.SID='S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334'

試試這個樣本

#include "stdafx.h" 
#define _WIN32_DCOM 
#include <iostream> 
using namespace std; 
#include <comdef.h> 
#include <Wbemidl.h> 
# pragma comment(lib, "wbemuuid.lib") 

#pragma argsused 
int main(int argc, char* argv[]) 
{ 
    BSTR strNetworkResource; 
    strNetworkResource = L"\\\\.\\root\\CIMV2"; 

    // Initialize COM. ------------------------------------------ 

    HRESULT hres; 
    hres = CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres)) 
    { 
     cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Set general COM security levels -------------------------- 

     hres = CoInitializeSecurity(
      NULL, 
      -1,       // COM authentication 
      NULL,      // Authentication services 
      NULL,      // Reserved 
      RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication 
      RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation 
      NULL,      // Authentication info 
      EOAC_NONE,     // Additional capabilities 
      NULL       // Reserved 
      ); 


    if (FAILED(hres)) 
    { 
     cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Obtain the initial locator to WMI ------------------------- 

    IWbemLocator *pLoc = NULL; 
    hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); 

    if (FAILED(hres)) 
    { 
     cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize();  
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Connect to WMI through the IWbemLocator::ConnectServer method 

    IWbemServices *pSvc = NULL; 


    hres = pLoc->ConnectServer(
      _bstr_t(strNetworkResource),  // Object path of WMI namespace 
      NULL,     // User name. NULL = current user 
      NULL,     // User password. NULL = current 
      0,      // Locale. NULL indicates current 
      NULL,     // Security flags. 
      0,      // Authority (e.g. Kerberos) 
      0,      // Context object 
      &pSvc     // pointer to IWbemServices proxy 
      ); 

    if (FAILED(hres)) 
    { 
     cout << "Could not connect. Error code = 0x" << hex << hres << endl;  
     cout << _com_error(hres).ErrorMessage() << endl; 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();   
     return 1;    // Program has failed. 
    } 

    cout << "Connected to root\\CIMV2 WMI namespace" << endl; 

    // Set security levels on the proxy ------------------------- 

     hres = CoSetProxyBlanket(
      pSvc,      // Indicates the proxy to set 
      RPC_C_AUTHN_WINNT,   // RPC_C_AUTHN_xxx 
      RPC_C_AUTHZ_NONE,   // RPC_C_AUTHZ_xxx 
      NULL,      // Server principal name 
      RPC_C_AUTHN_LEVEL_CALL,  // RPC_C_AUTHN_LEVEL_xxx 
      RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 
      NULL,      // client identity 
      EOAC_NONE     // proxy capabilities 
     ); 

    if (FAILED(hres)) 
    { 
     cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 

    // Use the IWbemServices pointer to make requests of WMI ---- 

    IWbemClassObject *pclsObj = NULL; 
    hres = pSvc->GetObject(L"Win32_SID.SID='S-1-5-82-1036420768-1044797643-1061213386-2937092688-4282445334'", 0, NULL, &pclsObj, NULL); 

    if (FAILED(hres)) 
    { 
     cout << "GetObject failed" << " Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 
    else 
    { 

     VARIANT vtProp; 

     HRESULT hr = pclsObj->Get(L"AccountName", 0, &vtProp, 0, 0);// String 
     if (!FAILED(hr)) 
     { 
      if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
      wcout << "AccountName : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
      else 
      wcout << "AccountName : " << vtProp.bstrVal << endl; 
     } 
     VariantClear(&vtProp); 

     pclsObj->Release(); 
     pclsObj=NULL; 
    } 

    // Cleanup 

    pSvc->Release(); 
    pLoc->Release(); 
    if (pclsObj!=NULL) 
    pclsObj->Release(); 

    CoUninitialize(); 
    cout << "press enter to exit" << endl; 
    cin.get(); 
    return 0; // Program successfully completed. 
} 
1

這是WMI的一個問題:它提供了一個統一的接口,但實際上所有的數據都來自其他代碼。即使數據清晰可用,其他代碼中的某些代碼也不會向WMI提供所有數據。如果您仍然需要這些數據,那麼您可能需要繞過WMI並直接從源代碼獲取它。

幸運的是,在這種情況下,這很簡單:您可以撥打LookupAccountSid以獲得您要求的內容。

+0

我知道這個解決方法;我只是好奇爲什麼上述查詢不起作用。感謝您的快速回復。 – user1019710