在Visual-C ++中使用IWBemServices更改适配器NetConnectionID

时间:2015-08-05 19:27:05

标签: c++ visual-c++ wmi adapter

我试图通过使用IWBemServices创建Win32_NetworkAdapter类的实例来更改网络适配器的NetConnectionID值。我通过其DeviceId找到适配器,然后尝试将新的NetConnectionID放入实例中。

我没有收到任何错误,但NetConnectionID保持不变。

这是我目前的功能:

int CIPConfigModule::setAdapterName(CString CAdapterName, unsigned int idAdapter)
{

    int bNoErrors = NO_ERROR;
    IWbemClassObject *pNewInstance = NULL;
    IWbemClassObject *pObjectClass = NULL;
    IWbemContext *pCtx = NULL;
    IWbemCallResult *pResult = NULL;

    // Get the class definition.
    string strIndex = std::to_string((long double)idAdapter);
    string strIndexCommand = "Win32_NetworkAdapter.DeviceID=\"";
    strIndexCommand.append(strIndex).append("\"");
    CString CSIndexCommand = strIndexCommand.c_str();
    // BSTR PathToClass = SysAllocString(L"Example");

    HRESULT hRes = m_pSvc->GetObject((bstr_t)CSIndexCommand, 0, pCtx, 
                 &pObjectClass, &pResult);

    if (hRes != WBEM_S_NO_ERROR){
        bNoErrors=ERROR_ATTEMPTING_TO_CHANGE_NAME;
        return bNoErrors;
    }

    // Create a new instance.
    pObjectClass->SpawnInstance(0, &pNewInstance);
    pObjectClass->Release();  // Don't need the class any more

    VARIANT v;
    VariantInit(&v);

    // Set the NetConnectioID property.
    V_VT(&v) = VT_BSTR;
    V_BSTR(&v) = CAdapterName.AllocSysString();
    //V_BSTR(&v) = SysAllocString(L"Testing");
    BSTR KeyProp = SysAllocString(L"NetConnectionID");
    pNewInstance->Put(KeyProp, 0, &v, 0);
    SysFreeString(KeyProp);
    VariantClear(&v);

    // Other properties acquire the 'default' value specified
    // in the class definition unless otherwise modified here.

    // Write the instance to WMI. 
    hRes = m_pSvc->PutInstance(pNewInstance, 0, pCtx, &pResult);
    pNewInstance->Release();

    if (hRes != WBEM_S_NO_ERROR){
        bNoErrors=ERROR_ATTEMPTING_TO_CHANGE_NAME;
        return bNoErrors;
    }

    return bNoErrors;
}

有没有人知道我可能会缺少什么?

0 个答案:

没有答案