1

我正在尝试从远程机器查询 WMI 的 Win32_Tpm 类。但它失败并显示 HRESULT 0x80041013 和描述:提供程序加载失败。是否可以远程访问 MicrosoftTPM 命名空间?以下是代码,

hres = pSvc ->CreateInstanceEnum(
    bstr_t(Class),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &Enumerator);

hres = CoSetProxyBlanket(
    Enumerator,                    // Indicates the proxy to set
    RPC_C_AUTHN_DEFAULT,            // RPC_C_AUTHN_xxx
    RPC_C_AUTHZ_DEFAULT,            // RPC_C_AUTHZ_xxx
    COLE_DEFAULT_PRINCIPAL,         // Server principal name 
    RPC_C_AUTHN_LEVEL_PKT_PRIVACY,  // RPC_C_AUTHN_LEVEL_xxx 
    RPC_C_IMP_LEVEL_IMPERSONATE,    // RPC_C_IMP_LEVEL_xxx
    mUserAcct,                       // client identity
    EOAC_NONE                       // proxy capabilities 
    );

    hres = Enumerator->Next(WBEM_INFINITE, 1, &TPMClass, &uReturn);   

//这里我失败了hrs = 0x80041013和uReturn = 0,TPMClass的值在执行上述语句后也为NULL。

下面是详细的代码,

IEnumWbemClassObject* Enumerator;
IWbemClassObject* TPMClass = NULL;
ULONG uReturn = 0;

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 --------------------------

hres = CoInitializeSecurity(
    NULL,
    -1,                          // COM authentication
    NULL,                        // Authentication services
    NULL,                        // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
    RPC_C_IMP_LEVEL_IDENTIFY,    // 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

IWbemServices *pSvc = NULL;

hres = pLoc->ConnectServer(
    _bstr_t(L"\\\\REMOTE\\root\\cimv2\\Security\\MicrosoftTpm"),
    _bstr_t(useToken ? NULL : pszName),    // User name
    _bstr_t(useToken ? NULL : pszPwd),     // User password
    NULL,                              // Locale             
    NULL,                              // Security flags
    _bstr_t(useNTLM ? NULL : pszAuthority),// Authority        
    NULL,                              // Context object 
    &pSvc                              // IWbemServices proxy
    );

COAUTHIDENTITY *userAcct = NULL;
COAUTHIDENTITY authIdent;

//Created COAUTHIDENTITY  with internal function
    userAcct = &authIdent;

hres = CoSetProxyBlanket(
    pSvc,                           // Indicates the proxy to set
    RPC_C_AUTHN_DEFAULT,            // RPC_C_AUTHN_xxx
    RPC_C_AUTHZ_DEFAULT,            // RPC_C_AUTHZ_xxx
    COLE_DEFAULT_PRINCIPAL,         // Server principal name 
    RPC_C_AUTHN_LEVEL_PKT_PRIVACY,  // RPC_C_AUTHN_LEVEL_xxx 
    RPC_C_IMP_LEVEL_IMPERSONATE,    // RPC_C_IMP_LEVEL_xxx
    userAcct,                       // client identity
    EOAC_NONE                       // proxy capabilities 
    );

hres = pSvc ->CreateInstanceEnum(
    bstr_t(Class),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &Enumerator);

hres = CoSetProxyBlanket(
    Enumerator,                    // Indicates the proxy to set
    RPC_C_AUTHN_DEFAULT,            // RPC_C_AUTHN_xxx
    RPC_C_AUTHZ_DEFAULT,            // RPC_C_AUTHZ_xxx
    COLE_DEFAULT_PRINCIPAL,         // Server principal name 
    RPC_C_AUTHN_LEVEL_PKT_PRIVACY,  // RPC_C_AUTHN_LEVEL_xxx 
    RPC_C_IMP_LEVEL_IMPERSONATE,    // RPC_C_IMP_LEVEL_xxx
    mUserAcct,                       // client identity
    EOAC_NONE                       // proxy capabilities 
    );

    //Here I am failing , value of TPMClass is NULL after execution of      following statement also.

    hres = Enumerator->Next(WBEM_INFINITE, 1, &TPMClass, &uReturn);   
4

1 回答 1

0

上面的类名是函数 CreateInstanceEnum() 中的 L"Win32_Tpm"。我可以在从本地机器访问时获取实例,但在从远程机器访问时失败。

注意:我已经为远程机器上的用户分配了适当的权限。

于 2015-03-18T06:09:10.530 回答