如何使用 WMI 更改 Win32_NetworkAdapter NetConnectionID 属性

发布于 2025-01-05 18:23:40 字数 1821 浏览 1 评论 0原文

我正在编写一个 VC++ 程序,需要更改网络连接名称(例如“本地连接”)。

Windows的IPHELPER提供了获取网络连接的API,但不提供写入方法。 而且我知道我可以使用“netsh”来更改此网络连接名称,是的,在英文Windows中,此方法可以正常工作,但在日文或中文Windows中,此方法无法正常工作。

在阅读了MSDN中的一些文档后,我发现WMI Win32_NetworkAdapter类有一个属性“NetConnectionID”。该属性包含适配器的网络连接名称及其读/写属性。

http://msdn.microsoft。 com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx

NetConnectionID 
Data type: string 
Access type: Read/write

因此,我编写了一个 WMI 代码来更改此属性 价值。

int RasWmiAdapterSetInfo(void *pWmiAdapterCfg)
{
    if(pWmiAdapterCfg)
    {
        int retVal = -1;
        HRESULT hres;
        VARIANT varWrite;
        RAS_WMI_W32_ADAPTER_PTR *pRasWmiAdapterCfg = (RAS_WMI_W32_ADAPTER_PTR*)pWmiAdapterCfg;

        VariantInit(&varWrite);
        V_VT(&varWrite) = VT_BSTR;
        V_BSTR(&varWrite) = SysAllocString(L"My New Connection 1");
        hres = pRasWmiAdapterCfg->pclsObj->Put(L"NetConnectionID", 0, &varWrite, 0);
        if(FAILED(hres))
        {
            printf("RasWmiAdapterSetInfo Failed HR=%08x\n", hres);
            goto CLEARUP;
        }
        retVal = 0;
CLEARUP:

        VariantClear(&varWrite);
        return retVal;
    }
    return -1;
}

返回值hrs没问题,但是这个属性值没有改变。

IWbemLocator         *pLoc;
IWbemServices        *pSvc;
IEnumWbemClassObject *pEnumerator; 
 ...
 ... (ignore some code to init pLoc, pSvc, pEnumerator)
 ...
IWbemClassObject *pclsObj;
IWbemClassObject *pClass;
pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
pSvc->GetObject( bstr_t(adapterClsName), 0, NULL, &pClass, NULL );

有人能告诉我为什么 pclsObj->Put 方法没有生效吗?

谢谢。

I am writing a VC++ program which needs to change Network Connection name (eg. "Local Connection").

The Windows's IPHELPER provides API to get network connection, but it doesn't provide write method.
And I know I can use "netsh" to change this network connection name, yes, in English Windows, this method works properly, but in Japanese or Chinese Windows this method can't work properly.

After read some documents in MSDN, I found WMI Win32_NetworkAdapter class has a property "NetConnectionID". This property contains the adapter's network connection name, and its read/write perperty.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx

NetConnectionID 
Data type: string 
Access type: Read/write

So, I wrote a WMI code to change this property value.

int RasWmiAdapterSetInfo(void *pWmiAdapterCfg)
{
    if(pWmiAdapterCfg)
    {
        int retVal = -1;
        HRESULT hres;
        VARIANT varWrite;
        RAS_WMI_W32_ADAPTER_PTR *pRasWmiAdapterCfg = (RAS_WMI_W32_ADAPTER_PTR*)pWmiAdapterCfg;

        VariantInit(&varWrite);
        V_VT(&varWrite) = VT_BSTR;
        V_BSTR(&varWrite) = SysAllocString(L"My New Connection 1");
        hres = pRasWmiAdapterCfg->pclsObj->Put(L"NetConnectionID", 0, &varWrite, 0);
        if(FAILED(hres))
        {
            printf("RasWmiAdapterSetInfo Failed HR=%08x\n", hres);
            goto CLEARUP;
        }
        retVal = 0;
CLEARUP:

        VariantClear(&varWrite);
        return retVal;
    }
    return -1;
}

The return value hres was OK, but this property value was not changed.

IWbemLocator         *pLoc;
IWbemServices        *pSvc;
IEnumWbemClassObject *pEnumerator; 
 ...
 ... (ignore some code to init pLoc, pSvc, pEnumerator)
 ...
IWbemClassObject *pclsObj;
IWbemClassObject *pClass;
pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
pSvc->GetObject( bstr_t(adapterClsName), 0, NULL, &pClass, NULL );

Is anyone can tell me why the pclsObj->Put method did not take effect?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-01-12 18:23:40

据我了解, IWbemClassObject::Put() 仅更新内存中类实例的属性值。要将更改提交到 WMI 存储库,您需要调用 IWbemServices::PutInstance() 来更新整个类实例。

因此,在您的代码中添加这样的内容应该可以使其正常工作:

IWbemCallResult* callResult = nullptr;
HRESULT hr = wbemSvc_->PutInstance(pClass, WBEM_FLAG_UPDATE_ONLY,
    nullptr, &callResult);
if (SUCCEEDED(hr) && callResult)
    callResult->Release();

希望有帮助!

As far as I understand, IWbemClassObject::Put() only updates a class instance's property value in memory. To commit changes to WMI repository, you need to call IWbemServices::PutInstance() to update entire class instance.

So adding something like this to your code should get it to work:

IWbemCallResult* callResult = nullptr;
HRESULT hr = wbemSvc_->PutInstance(pClass, WBEM_FLAG_UPDATE_ONLY,
    nullptr, &callResult);
if (SUCCEEDED(hr) && callResult)
    callResult->Release();

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文