IEnumWbemClassObject->下一个方法在查询进程详细信息时填充ureturn = 0

时间:2014-10-20 07:25:59

标签: c wmi

我编写了一个使用c ++的wmi程序来查询不在我的域中且不属于任何域的远程PC。建立连接,查询成功运行。但是枚举中的Next方法返回S_FALSE和ureturn = 0.除了与win32_process和win32_perfFormattedData_PerfProc_Process等进程细节相关外,所有查询都能正常工作。

这是我的代码

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


int main(int argc, char **argv)
{
    HRESULT hres;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

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

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    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;
        CoUninitialize();
        return 1;                    // Program has failed.
    }

    // Step 3: ---------------------------------------------------
    // 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;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    // Get the user name and password for the remote computer
    //TCHAR pszName[100] = L"Administrator";
    //TCHAR pszPwd[100]  = L"magnaquest@02";
    TCHAR pszName[100] = L"Ankit";
    TCHAR pszPwd[100]  = L"magnaquest@02";
    // NOTE: The value for pszDom will depend whether you are
    //       using a domain credential or a local machine account.
    //       Specify the NETBIOS name of the domain if the credential
    //       is for a domain user. For local machine account, specify
    //       the NETBIOS name of the remote machine
    TCHAR pszDom[100]  = {0};  

    BOOL fSave;
    DWORD dwErr;


    // Connect to the remote root\cimv2 namespace
    // and obtain pointer pSvc to make IWbemServices calls.
    //---------------------------------------------------------
    // change the computerName and domain 
    // strings below to the full computer name and domain 
    // of the remote computer
    IWbemServices*  pSvc    =   NULL;
    hres = pLoc->ConnectServer(
        //_bstr_t(L"\\\\WIN-F0E3MR4HILN\\root\\cimv2"),
        _bstr_t(L"\\\\Ankit-PC\\root\\cimv2"),
        _bstr_t(pszName),                 // User name
        _bstr_t(pszPwd),                  // User password
        _bstr_t(L"MS_409"),               // Locale             
        NULL,                             // Security flags
        _bstr_t(L"ntlmdomain:DomainOrMachine"),    // Authority        
        0,                                // Context object 
        &pSvc                             // IWbemServices proxy
        );

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

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


    // Step 5: --------------------------------------------------
    // Set security levels on a WMI connection ------------------
    COAUTHIDENTITY cID;

    cID.User           = (USHORT*)&pszName;
    cID.UserLength     = lstrlen(pszName); //bstrUsername.length();
    cID.Password       = (USHORT*)&pszPwd;
    cID.PasswordLength = lstrlen(pszPwd); //bstrPassword.length();
    cID.Domain         = (USHORT*)&pszDom;
    cID.DomainLength   = lstrlen(pszDom); // bstrDomain.length();
    cID.Flags          = SEC_WINNT_AUTH_IDENTITY_UNICODE;

    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
       &cID,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

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

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_Process WHERE IDProcess = '2472'"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        cout << "Query for Logical Disk failed."
            << " Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }
    //
    // Must set the security on the enumerator interface as well
    // or you will received an access denied error
    //
    hres = CoSetProxyBlanket(
       pEnumerator,                 // 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
       &cID,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

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

    // When you have finished using the credentials,
    // erase them from memory.
    SecureZeroMemory(pszName, sizeof(pszName));
    SecureZeroMemory(pszPwd, sizeof(pszPwd));
    SecureZeroMemory(pszDom, sizeof(pszDom));


    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;

    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
            &pclsObj, &uReturn);

        if(0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property
        hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
        wcout << " Name  : " << vtProp.bstrVal << endl;

        VariantClear(&vtProp);
    }

    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    pclsObj->Release();
    CoUninitialize();

    getchar();
    return 0;   // Program successfully completed.

}

0 个答案:

没有答案