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

📄 passthrustats.cpp

📁 James Antognini和Tom Divine提供的PASSTHRU的编成实例。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
          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);
             rc = rcErr;
             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);
          rc = rcErr;
          break;
         }

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

       hr = GetSetWMIInfo(
                          pServices,
                          DriverClassStatsW,          // (Pointer to) name of concerned class.
                          pUserid,
                          pPassword
                         );
      } while(0);                                     // End big 'do' group.

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

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

    if (TRUE==bInitialized)
      CoUninitialize();

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

    return rc;
   }                                                  // End main().

/**************************************************************************************************/
/*                                                                                                */
/*                                                                                                */
/**************************************************************************************************/
HRESULT
GetSetWMIInfo(
              IWbemServices * pServices,
              LPCWSTR         pClassName,             // Address of name of PassThru statistics class.
              WCHAR         * pUserid,
              WCHAR         * pPassword
             )
   {                           
    HRESULT hr;
    BOOLEAN bSetAdapterFd = FALSE;
    const struct
      {
        PWCHAR  pStatName;
        char  * pStatDesc;
      }
        PassThruStatsInfo[] =
                              {
                               {L"MPSendPktsSeen",    "Packets seen in MPSendPackets      "},
                               {L"MPSendPktsDropped", "Packets dropped in MPSendPackets   "},
                               {L"PTRcvSeen",         "Packets seen in PTReceive          "},
                               {L"PTRcvDropped",      "Packets dropped in PTReceive       "},
                               {L"PTRcvPktsSeen",     "Packets seen in PTReceivePackets   "},
                               {L"PTRcvPktsDropped",  "Packets dropped in PTReceivePackets"},
                              };
    ULONG const IdxLimPassThruStatsInfo = sizeof(PassThruStatsInfo)/sizeof(PassThruStatsInfo[0]);

    // 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))                         // Instance enumerator not opened?
      {                    
       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,
                          Stats,
                          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;
         }

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

       // Get the statistics.

       for (ULONG i = 0; i < IdxLimPassThruStatsInfo; i ++)
         {
          PWCHAR pX = PassThruStatsInfo[i].pStatName;

          hr = pInstance->Get(_bstr_t(PassThruStatsInfo[i].pStatName), 0, &Stats, NULL, NULL);

          if (FALSE==SUCCEEDED(hr))
            {
             ReportError(_T("IWbemClassObject::Get()"), hr);
             printf("i = %s\n", i);
             goto ReleaseInstance;
            }
          if (VT_NULL!=Stats.vt)
            {                                         // Print statistics.
             if (2==i || 4==i)
               printf(_T("       \n"));
             printf(_T("         %s = %d\n"), PassThruStatsInfo[i].pStatDesc, Stats.lVal);
            }
         }

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

    pEnum->Release();

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

/**************************************************************************************************/
/*                                                                                                */
/* Parse input string.                                                                            */
/*                                                                                                */
/**************************************************************************************************/
int
ParseIt(
        int       nbrArgs,                            // Number of string parameters.
        WCHAR   * pArgv[],                            // Array of addresses of parameters.
        int     * pCmdNbr                             // Pointer to variable to receive parsed command number.
       )
{
 int       rc = rcOK,
           i,
           CmdNbr = CmdUnknown;

 if (                                                 // Not supported number parameters?
     nbrArgs!=2
       &&
     nbrArgs!=3
       &&
     nbrArgs!=5
    )
   {
    printf("\nBad number of parameters\n");
    ParmInfo();

    rc = rcErr;
    goto ExitPoint;
   }

 if (nbrArgs>=2)
   {
    for (i = 1; i < lnInStrArr; i ++)                // Go through expected parameters.
      if (0==                                        // Does the second parm match?
            _wcsnicmp(pArgv[1], InStrArr[i].pInStr, wcslen(InStrArr[i].pInStr)))
        break;                                       // If yes, break.
   }
 else
   i = lnInStrArr;                                   // Ensure next conditional yields supported parameter information.

 if (i<lnInStrArr)                                   // Found a supported command?
   CmdNbr = InStrArr[i].CmdCode;                     // Set command code.
 else
   {
    printf("\nUnsupported command\n\n");
    ParmInfo();

    rc = rcErr;
    goto ExitPoint;
   }

ExitPoint:
 *pCmdNbr = CmdNbr;                                   // Return command number.

 return rc;
}                                                     // End ParseIt().

/**************************************************************************************************/                 
/*                                                                                                */                 
/*                                                                                                */                 
/**************************************************************************************************/                 
void                                                                                                                 
ParmInfo()                                                                                                           
   {                                                                                                                 
    printf("\nUsage: " JARtnName " < operation >\n\n");
    printf("       where < operation > is one of these:\n\n");
    for (int i = 1; i < lnInStrArr; i ++)
      printf("         %S\n", InStrArr[i].pInStr);
   }                                                  // End ParmInfo().
                                                                                                                     

⌨️ 快捷键说明

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