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

📄 setipaddrarray.cpp

📁 James Antognini和Tom Divine提供的PASSTHRU的编成实例。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
               {                                      
                PULONG pNbr = (PULONG)IPAddrArray.parray->pvData + i;
                PUCHAR pC = (PUCHAR)pNbr;
                if (0==i)
                  printf(_T("    IP address = %d.%d.%d.%d\n"), *pC, *(pC+1), *(pC+2), *(pC+3));
                else
                  printf(_T("               = %d.%d.%d.%d\n"), *pC, *(pC+1), *(pC+2), *(pC+3));
               }

            }
          else
            printf(_T("    No IP addresses.\n"));     // Say no IP addresses.
         }                                            // End 'if' command = get.
       else
       if (CmdSet==OperType)                          // Command = set?
         {
          if (
              NULL!=pAdapterName                      // Adapter name supplied?
                &&
              0!=                                     // Not the same?
                wcscmp((const wchar_t *)pAdapterName, InstName.bstrVal)
             )
            goto ReleaseInstance;

          bSetAdapterFd = TRUE;                       // Remember that sought adapter was found.

          hr =                                        // Get the current value of NumberElements.
            pInstance->Get(_bstr_t(L"NumberElements"), 0, &NumberElements, NULL, NULL);
          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("IWbemClassObject::Get(NumberElements)"), hr);
             pInstance->Release();
             continue;
            }

          // Change NumberElements to that specified.

          NumberElements.lVal = szIPAddrArr;

          hr =                                        // Set value (locally).
            pInstance->Put(_bstr_t(L"NumberElements"), 0, &NumberElements, 0);
          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("IWbemClassObject::Put(NumberElements)"), hr);
             continue;
            }

          SAFEARRAY      * pSAPropx2;
          SAFEARRAYBOUND   saBounds = {NumberElements.lVal, 0};

          pSAPropx2 = SafeArrayCreate(VT_I4, 1, &saBounds);

          VARIANT          NewArrVariant;

          V_VT(&NewArrVariant) = VT_I4 | VT_ARRAY;    // Make the variant of type = int and = array.
          V_ARRAY(&NewArrVariant) = pSAPropx2;        // Point the variant to the array.

          // Set the array values to those specified.

          for (ULONG i = 0; i < NewArrVariant.parray->rgsabound->cElements; i ++)
            {
             PULONG pNbr = (PULONG)NewArrVariant.parray->pvData + i;
             *pNbr = *((PULONG)pIPAddrArr + i);
            }

          hr =                                        // Save the variant into IPAddrArray (locally).
            pInstance->Put(_bstr_t(L"IPAddrArray"), 0, &NewArrVariant, 0);
          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("IWbemClassObject::Put(IPAddrArray)"), hr);
             continue;
            }

          hr = SafeArrayDestroy(pSAPropx2);           // Release array.
          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("SafeArrayDestroy failed"), hr);
             continue;
            }

          hr = pServices->PutInstance(                // Save the changes in WMI (via the NDIS IM driver).
                                      pInstance,
                                      WBEM_FLAG_UPDATE_ONLY,
                                      NULL,           // No context (qualifier or additional information).
                                      NULL
                                     );
          if (FALSE==SUCCEEDED(hr))                   // A problem?
            {
             printf(_T("\n"));
             ReportError(_T("IWbemServices::PutInstance"), hr);
            }
         }                                            // End 'if' command = set.
       else
         {                                            // Unknown operation type.  Shouldn't happen.
          printf(_T("Unsupported command value = %d\n"), OperType);
          hr = E_INVALIDARG;
         }

ReleaseInstance:
       pInstance->Release();                          // Release instance.
      }                                               // End 'while' go through the instances.

    if (                                              // In a set, didn't find specified adapter?
        CmdSet==OperType
          &&
        FALSE==bSetAdapterFd
       )
      {
       printf(_T("Failed to find adapter named '%ws'\n"), pAdapterName);
       hr = E_FAIL;
      }

    pEnum->Release();

done:
    return hr;
   }                                                  // End GetSetWMIInfo().

/**************************************************************************************************/
/*                                                                                                */
/* Convert IP addresses from ASCII to an array of network-form 4-byte words.                      */
/*                                                                                                */
/**************************************************************************************************/
int
ArrangeData(
            PVOID   pDataIn,                          // Input ASCII IP addresses, each ended by return and newline characters.
            int     ulBytesRead,                      // Bytes supplied.
            PVOID   pDataOut,                         // Output area for network-form 4-byte IP addresses.
            int   * pUlBytesReturned                  // Output variable of bytes returned. 
           )
   {
    char const usDelim[] = {0x0d, 0x0a};              // Return and newline characters.
    char       wk[64];
    int        rc = rcOK,
               i,
               prev = 0,
               bytesUsed = 0;

    for (                                             // Byte by byte ...
         i = 0;
         i < ulBytesRead - (int)sizeof(usDelim) + 1;
         i ++
        )
      if (*(PUSHORT)&usDelim==                        // Found a delimiter?
            *(PUSHORT)((PUCHAR)pDataIn+i))
        {
         ULONG len = i - prev;                        // Get length of current IP-addres sstring.
         memcpy(wk, (PUCHAR)pDataIn+prev, len);       // Copy string to work area.
         wk[len] = 0;                                 // Append null terminator.

         ULONG IPAddr = inet_addr((const char *)wk);  // Get network form of string.

         if (INADDR_NONE==IPAddr)                     // Invalid?
           {
            printf(_T("Invalid address >>%s<<\n"), wk);
            rc = rcErr;
            bytesUsed = 0;
            goto done;
           }
          
         *(PULONG)((PUCHAR)pDataOut+bytesUsed) =      // Copy IP address to output area.
           IPAddr;
         bytesUsed += sizeof(IPAddr);                 // Adjust byte-used count.
         prev = i + sizeof(usDelim);                  // Advance past delimiter
         i = prev + 1;                                // Update index to next byte (or beyond input data).
        }                                             // End 'if' found a delimiter.

done:
    *pUlBytesReturned = bytesUsed;                    // Show amount returned.

    return rc;
   }                                                  // End ArrangeData().

/**************************************************************************************************/
/*                                                                                                */
/* Read IP addresses from a file.                                                                 */
/*                                                                                                */
/* Note:  The storage whose pointer is returned (in success) has to be freed.                     */
/*                                                                                                */
/**************************************************************************************************/
int
GetData(
        DWORD    CmdOper,                             // Command operation value.
        PVOID  * pOutVar,                             // Returned array of IP addresses in network form.
        PULONG   pSzArr                               // Returned number of bytes in array.
       )
   {
    int     rc = rcOK;
    HANDLE  hFile = INVALID_HANDLE_VALUE;
    PVOID   pRecords = NULL;
    ULONG   ulBytesUsed;
    #define szBfr  4096
    #define szBfr2 8192

    do
      {
       PVOID pArr =                                   // Allocate storage to be returned (in success).
         *pOutVar =
           malloc(szBfr);

       if (NULL==pArr)                                // A problem?
         {
          printf(_T("GetData():  Failed to get storage\n"));
          rc = rcErr;
          break;
         }

       if (CmdSetDflt==CmdOper)                       // Is command 'set default'?
         {
          ulBytesUsed = 0;                            // Show no elements.
          free(*pOutVar);
          *pOutVar = NULL;
          break;                                      // Leave the 'do' group.
         }

       pRecords = malloc(szBfr2);                     // Allocate working storage.

       if (NULL==pRecords)                            // A problem?
         {
          printf(_T("GetData():  Failed to get record storage\n"));
          rc = rcErr;
          break;
         }

       hFile = CreateFile(                            // Open file.
                          FileName,
                          FILE_READ_DATA,
                          FILE_SHARE_READ,
                          NULL,                       // No inheritance by child processes. 
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL                        // Handle to template file (whatever that is).
                         );

       if (INVALID_HANDLE_VALUE==hFile)               // A problem?
         {
          printf(_T("GetData():  Failed to open file '%s'\n"), FileName);
          rc = rcErr;
          break;
         }

       for (;;)                                       // Go through records.
         {
          BOOL bRead;
          ULONG ulBytesRead;
                
          bRead = ReadFile(                           // Read the requested number of bytes.
                           hFile,
                           pRecords,
                           szBfr2,
                           &ulBytesRead,
                           NULL

⌨️ 快捷键说明

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