⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setipaddrarray.cpp

📁 James Antognini和Tom Divine提供的PASSTHRU的编成实例。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
       if (FALSE==SUCCEEDED(hr))
         {
          ReportError(_T("main():  CoInitialize failed"), hr);
          break;
         }

       bInitialized = TRUE;

       hr = CoInitializeSecurity(                     // Initialize security.
                                 NULL,
                                 -1,
                                 NULL,
                                 NULL,
                                 RPC_C_AUTHN_LEVEL_PKT,// Per Brugiolo, 17.10.2002.
                                 RPC_C_IMP_LEVEL_IMPERSONATE,                                                                     
                                 NULL,                // Pointer to security information.         
                                 EOAC_NONE,           // Per Brugiolo, 17.10.2002.                
                                 0                                                                
                                );

       if (FALSE==SUCCEEDED(hr))
         {                    
          ReportError(_T("main():  CoInitializeSecurity failed"), hr);
          break;
         }

       hr = CoCreateInstance(                         // Create uninitialized object associated with class id given in parm 1.
                             CLSID_WbemLocator,
                             NULL,
                             CLSCTX_INPROC_SERVER,
                             IID_IWbemLocator,
                             (PVOID*)&pLocator
                            );

       if (FALSE==SUCCEEDED(hr))
         {
          ReportError(_T("main():  CoCreateInstance(WbemLocator)"), hr);
          break;
         }

       bHaveLocator = TRUE;

       if (NULL!=pSystemName)                         // System specified?
         {                                            // Build root qualified by system, eg, '\\MyComputer\root\WMI'.
          hr = VarBstrCat(_bstr_t(L"\\\\"), _bstr_t(pSystemName), &in);

          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("main():  VarBstrCat #1"), hr);
             break;
            }

          hr = VarBstrCat(in, _bstr_t(L"\\root\\WMI"), &WMIRoot);

          SysFreeString(in);

          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("main():  VarBstrCat #2"), hr);
             break;
            }
         }
       else
         WMIRoot =                                    // Win2003 requires \\.\root\WMI instead of \root\WMI.
           SysAllocString(L"\\\\.\\root\\WMI");

       if (NULL!=pUserid)                             // Userid and password specified?
         {
          bUserid = SysAllocString(pUserid);
          bPassword = SysAllocString(pPassword);
         }
       else
         {
          bUserid = NULL;                             // Use identity inherited from process.
          bPassword = NULL;                           // "
         }

       for (;;)
         {
          hr = pLocator->ConnectServer(               // Connect to the WMI server on this computer and, possibly, through it to another system.
                                       WMIRoot,
                                       bUserid,
                                       bPassword,
                                       NULL,
                                       0,
                                       NULL,
                                       NULL,
                                       &pServices
                                      );

          if (NULL!=bUserid)                          // BSTRs allocated?
            {
             SysFreeString(bUserid);
             SysFreeString(bPassword);
            }

          if (TRUE==SUCCEEDED(hr))                    // Success?
            break;

          if (WBEM_E_LOCAL_CREDENTIALS!=hr)           // Error is not credentials used in local connection?
            {
             ReportError(_T("main():  IWbemLocator::ConnectServer"), hr);
             goto done;
            }

          bUserid = NULL;                             // Use identity inherited from process.
          bPassword = NULL;                           // "
          pUserid = NULL;
          pPassword = NULL;
         }

       bHaveServices = TRUE;

       hr =                                           // Set authentication information for interface.        
         SetSecurity(pServices, pUserid, pPassword);

       if (FALSE==SUCCEEDED(hr))                      // Problem?
         {
          ReportError(_T("main():  SetSecurity()"), hr);
          break;
         }

       // Get/set WMI information about the specified class.

       hr = GetSetWMIInfo(
                          pServices,
                          DriverClassNameW,           // (Pointer to) name of concerned class.
                          pIPAddrArr,                 // Pointer to sorted IP-address array.
                          szArr,                      // Number of elements in the array.
                          CmdOper,                    // Operation type.
                          pAdapterName,               // Pointer to adapter name, if command = set and if supplied.  NULL otherwise.
                          pUserid,                    // Pointer to userid, if supplied.  NULL otherwise.
                          pPassword                   // Pointer to password, if supplied.  NULL otherwise.
                         );
    } while(0);                                       // End big 'do' group.

done:
    if (FALSE==SUCCEEDED(hr))                         // Problem?
      rc = rcErr;

    if (TRUE==bHaveServices)
      pServices->Release();

    if (TRUE==bHaveLocator)
      pLocator->Release();

    if (TRUE==bInitialized)
      CoUninitialize();

    if (NULL!=pIPAddrArr)
      free(pIPAddrArr);

    return rc;
   }                                                  // End main().

/**************************************************************************************************/
/*                                                                                                */
/*                                                                                                */
/**************************************************************************************************/
HRESULT
GetSetWMIInfo(
              IWbemServices * pServices,
              LPCWSTR         pClassName,             // Address of name of NDIS IM driver IP-Address-Array class.
              PVOID           pIPAddrArr,             // Address of array of IP addresses in network form.
              ULONG           szIPAddrArr,            // Number of such addresses.
              ULONG           OperType,               // Type of operation.
              PWCHAR          pAdapterName,           // Address of adapter name in Unicode, if command = set and if supplied.  Unpredictable otherwise.
              PWCHAR          pUserid,                // Address of userid in Unicode, or NULL.
              PWCHAR          pPassword               // Address of password in Unicode, or NULL.
             )
   {                           
    HRESULT hr;
    BOOLEAN bSetAdapterFd = FALSE;

    // Open an instance enumerator for the specified class.

    IEnumWbemClassObject * pEnum;

    hr = pServices->CreateInstanceEnum(
                                       _bstr_t(pClassName),
                                       WBEM_FLAG_SHALLOW              |
                                         WBEM_FLAG_RETURN_IMMEDIATELY |
                                         WBEM_FLAG_FORWARD_ONLY,
                                       NULL,
                                       &pEnum
                                      );

    if (FALSE==SUCCEEDED(hr))
      {                    
       ReportError(_T("CreateInstanceEnum"), hr);
       goto done;
      }

    hr = SetSecurity(pEnum, pUserid, pPassword);      // Set authentication information for interface.             
                                                                                                                   
    if (FALSE==SUCCEEDED(hr))                         // Problem?                                                  
      {                                                                                                            
       ReportError("GetSetWMIInfo();:SetSecurity()", hr);                                                          
       goto done;
      }                                                                                                            
                                                                                                                   
    while(TRUE)                                       // Go through the NDIS IM driver instances.
      {
       ULONG              nbrObjsReturned,
                          nbrObjsSought = 1,
                          InstanceCt = 0;
       IWbemClassObject * pInstance = NULL;
       _variant_t         NumberElements,
                          IPAddrArray,
                          InstName;

       hr =                                           // Get next instance of the specified class.
         pEnum->Next(INFINITE, nbrObjsSought, &pInstance, &nbrObjsReturned);

       if (hr==WBEM_S_FALSE)                          // None left?
         break;

       if (hr)                                        // A problem?
         {
          ReportError(_T("IEnumWbemClassObject::Next"), hr);
          if (WBEM_E_INVALID_CLASS==hr)               // Invalid class?  If so, perhaps NDIS IM driver not active.
            printf(_T("  Possible reason:  Passthru IM driver is not active.\n"));
          break;
         }

       // Get the name of the current instance.

       hr = pInstance->Get(_bstr_t(L"InstanceName"), 0, &InstName, NULL, NULL);
       if (FALSE==SUCCEEDED(hr))
         {
          ReportError(_T("IWbemClassObject::Get(InstanceName)"), hr);
          pInstance->Release();
          continue;
         }

       if (CmdGet==OperType)                          // Command = get?
         {
          // Get the array.                 

          hr = pInstance->Get(_bstr_t(L"IPAddrArray"), 0, &IPAddrArray, NULL, NULL);
          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("IWbemClassObject::Get(IPAddrArray)"), hr);
             pInstance->Release();
             continue;
            }

          printf(_T("\n  %ws\n\n"), InstName.bstrVal);// Print the instance name.

          if (VT_NULL!=IPAddrArray.vt)
            {                                         // Print each IP address.
             for (ULONG i = 0; i < IPAddrArray.parray->rgsabound->cElements; i ++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -