#include "stdafx.h"
#define _WIN32_DCOM
#include <comdef.h>
#include <iostream>
using namespace std;
#include <Wbemidl.h>
#include <atlbase.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
 
    IWbemServices *pSvc = NULL;
    
    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // 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;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }
 
    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
 
 
    // Step 5: --------------------------------------------------
    // 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;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }
 
    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----
     IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"),//
         WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);
    
    if (FAILED(hres))
    {
        cout << "Query for operating system name failed."
            << " Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }
 
    // 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 vArray;
VariantInit(&vArray);
SAFEARRAY *psa; SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound=0;
rgsabound[0].cElements=2; psa=SafeArrayCreate(VT_VARIANT,1,rgsabound);
if(psa==NULL)return 1; BSTR pBstr[]={L"9008",L"9009",0}; long lbound(0),ubound(0);
SafeArrayGetLBound(psa,1,&lbound);
SafeArrayGetUBound(psa,1,&ubound);
long size=ubound-lbound; BSTR pNewBstr[2]; SafeArrayAccessData(psa,(void HUGEP **)pNewBstr);
for(long idx=lbound;idx<size;idx++)
{
pNewBstr[idx]=pBstr[idx];
} SafeArrayUnaccessData(psa);
vArray.vt=VT_ARRAY;
vArray.parray=psa; //注释,这里不成功
hr=pclsObj->Put(L"IPSecPermitTCPPorts",0,&vArray,0);
VariantClear(&vArray);
        pclsObj->Release();
    }
 
    // Cleanup
    // ========    pEnumerator->Release();
    pSvc->Release();
    pLoc->Release();    CoUninitialize();    return 0;   // Program successfully completed.
}

解决方案 »

  1.   

    有出现新的问题,执行方法的时候,提示我方法的参数无效#define _WIN32_DCOM
    #include <comdef.h>
    #include <iostream>
    using namespace std;
    #include <Wbemidl.h>
    #include <atlbase.h>#pragma comment(lib,"wbemuuid.lib")int main(int argc,char **argv)
    {
    HRESULT hres;
    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.
    } 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.
    } 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.
    } //创建连接
    IWbemServices *pSvc = NULL;
    hres = pLoc->ConnectServer(
    _bstr_t(L"ROOT\\CIMV2"), // 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;
    pLoc->Release();
    CoUninitialize();
    return 1; // Program has failed.
    } cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl; //设置连接为安全的连接
    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;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1; // Program has failed.
    } //启用或关闭TCP/IP筛选过滤
    BSTR methodName = SysAllocString(L"EnableIPSec");//在类中需要查找的方法
    BSTR classPath =SysAllocString(L"Win32_NetWorkAdapterConfiguration");//要查找的类 //从连接中获取指定的类
    IWbemClassObject * pClass = NULL;
    hres = pSvc->GetObject(classPath, 0, NULL, &pClass, NULL); if( hres != WBEM_S_NO_ERROR )
    {
    cout<<"获取类失败"<<endl;
    return 0;
    } //从类中根据方法名称,获取参数定义
    IWbemClassObject * pInParamsDefinition = NULL;
    hres = pClass->GetMethod(methodName, 0, &pInParamsDefinition,NULL);
    if( hres != WBEM_S_NO_ERROR )
    {
    cout<<"获取参数定义失败"<<endl;
    pClass->Release();
    return 0;
    } //通过参数定义创建一个新的实例
    IWbemClassObject * pInParams = NULL;
    hres = pInParamsDefinition->SpawnInstance(0,&pInParams);
    if( hres != WBEM_S_NO_ERROR )
    {
    cout<<"通过参数定义创建一个新的实例失败"<<endl;
    pClass->Release();
    pInParamsDefinition->Release();
    } //创建参数开始**********************************
        VARIANT vTCPArr;
    VariantInit(&vTCPArr);
    SAFEARRAY *psa; VARTYPE vt = VT_BSTR;
    psa=SafeArrayCreateVector(vt,0,2);
    ATLASSERT(psa); BSTR pPorts[]={L"50001",L"50002"};
    BSTR pNewPorts[2];
    //通过指向数组的指针来对二维数组的元素进行间接赋值
    SafeArrayAccessData(psa,(void HUGEP **)pNewPorts);
    memcpy(pNewPorts,pPorts,sizeof(pPorts));
    SafeArrayUnaccessData(psa);

    vTCPArr.vt=VT_ARRAY|vt;///*vt必须和psa的数据类型保持一致*/
    vTCPArr.parray=psa; VARIANT varIp;
    VariantInit(&varIp);
    SAFEARRAY *psaip; psaip=SafeArrayCreateVector(vt,0,1);
    ATLASSERT(psaip); BSTR pIpList[]={L"0"};
    BSTR pNewIpList[1];
    SafeArrayAccessData(psaip,(void HUGEP**)pNewIpList);
    memcpy(pNewIpList,pIpList,sizeof(pIpList));
    SafeArrayUnaccessData(psaip);
    varIp.vt=VT_ARRAY|VT_BSTR;
    varIp.parray=psaip; //SafeArrayDestroy(psa);
    //创建参数结束**********************************

    CComVariant;
    CIM_UINT16;
    //保存参数
    hres = pInParams->Put(L"IPSecPermitTCPPorts",0,&vTCPArr,0);
    HRESULT hres2 = pInParams->Put(L"IPSecPermitUDPPorts",0,&varIp,0);
    HRESULT hres3 = pInParams->Put(L"IPSecPermitIPProtocols",0,&varIp,0);
    if(hres!=WBEM_S_NO_ERROR)
    {
    cout<<"保存IPSecPermitTCPPorts参数失败 0x"<<hex<<hres<<endl;
    return 0;
    }
    if(hres2!=WBEM_S_NO_ERROR)
    {
    cout<<"保存IPSecPermitUDPPorts参数失败 0x"<<hex<<hres2<<endl;
    return 0;
    }
    if(hres3!=WBEM_S_NO_ERROR)
    {
    cout<<"保存IPSecPermitIPProtocols参数失败 0x"<<hex<<hres3<<endl;
    return 0;
    }

    //通过IWbemServices执行方法,并把执行结果保存到pOutParams
    IWbemClassObject* pOutParams = NULL;
    hres=pSvc->ExecMethod(classPath,methodName,0,NULL,pInParams,&pOutParams,NULL);

    /*
    IWbemServices
    //查看执行结果,执行结果保存到varReturnValue中
    VARIANT varReturnValue;
    pOutParams->Get(L"ReturnValue",0,&varReturnValue,NULL,NULL);
    */

    if(hres==WBEM_S_NO_ERROR)
    {
    wcout<<L"ExecMethod Successfuly"<<endl;
    }
    else
    {
    wcout<<L"ExecMethod Fail 0x"<<hex<<hres<<endl;
    }

    VariantClear(&vTCPArr);
    pInParams->Release();
    pInParamsDefinition->Release();
    pClass->Release();
    pSvc->Release();

    if(pOutParams)
    {
    pOutParams->Release();
    }

    return 0;
    }
      

  2.   

    EnableIPSec方法的原型和参数说明是这样的:[Visual Basic]
    NetworkAdapterConfiguration.EnableIPSec( _
      IPSecPermitTCPPorts As String, _
      IPSecPermitUDPPorts As String, _
      IPSecPermitIPProtocols As String _
    )
    IPSecPermitTCPPorts 
    Data type: String 
    List of ports that should be granted access permission for TCP. A numeric value of 0 indicates access permission is granted for all ports. An empty string indicates that no ports should be granted access permission. 
      

  3.   

    请问,这两种定义有什么区别?
    _bstr_t port[]={"50001","50002"};
    BSTR pPorts[]={L"50008",L"50009"};VARIANT var;
    VariantInit(&vTCPArr);
    SAFEARRAY *psa;VARTYPE vt = VT_BSTR;
    psa=SafeArrayCreateVector(vt,0,2);
    ATLASSERT(psa);_bstr_t port[]={"50001","50002"};
    BSTR pPorts[]={L"50008",L"50009"};long lIndex=0;
    for(int   i=0;i<2;   i++)
    {
        SafeArrayPutElement(psa,&lIndex,ports[i].Copy());//这个方式接口可以接收到正确的参数
        //SafeArrayPutElement(psa,&lIndex,pPorts[i]);//这个方式接口接收的参数为空,请问是为什么啊?
        lIndex++;
    }
    var.vt=VT_ARRAY|vt;
    var.parray=psa;