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

📄 passthrustats.cpp

📁 James Antognini和Tom Divine提供的PASSTHRU的编成实例。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************************************/
/*                                                                                                */
/* Copyright (C) 2003, James Antognini, antognini@mindspring.com.                                 */
/*                                                                                                */
/**************************************************************************************************/

/**************************************************************************************************/
/*                                                                                                */
/* Parameters:                                                                                    */
/*                                                                                                */
/*              get < system name or IP address < userid password > >                             */
/*                                                                                                */
/*          returns and displays statistics from PassThru IM driver (about all adapters).         */
/*                                                                                                */
/*          1) If no system name is given, the current system is used.                            */
/*          2) If a system name is given, that system is used.                                    */
/*          3) If, in addition to a system name, a userid and password are given, those are       */
/*             used.  If the access results in WBEM_E_LOCAL_CREDENTIALS, the opeartion is tried   */
/*             again without userid and password.                                                 */
/*                                                                                                */
/**************************************************************************************************/

#define JARtnName           "PassThruStats"
#define JARtnVer            "1.09"
#define DriverClassStatsW   L"PassthruStatistics"

#include <winsock2.h>
#include <stdio.h>
#include <comdef.h>

#include "stdafx.h"
#include "wmirtns.h"

/**************************************************************************************************/
/*                                                                                                */
/* Forward definitions.                                                                           */
/*                                                                                                */
/**************************************************************************************************/

HRESULT
GetSetWMIInfo(IWbemServices *, LPCWSTR, PWCHAR, PWCHAR);

int
ParseIt(int, PWCHAR [], int *);
        
void
ParmInfo();

/**************************************************************************************************/
/*                                                                                                */
/* Globals.                                                                                       */
/*                                                                                                */
/**************************************************************************************************/

#pragma comment( exestr, JARtnName " v" JARtnVer " compiled on " __DATE__ " at " __TIME__ )

#define CompDateTimeStr     "dd mmm yyyy hh:mm:ss"  
                                                
char RtnCompileInfo[sizeof(CompDateTimeStr)+1]; 

#define rcOK  0
#define rcErr 8

// Define command numbers.

#define CmdUnknown   0
#define CmdGet       1
#define CmdHelp      2
#define CmdQuestion  2

const struct
  {
   DWORD   CmdCode;
   PWCHAR  pInStr;
  }
   InStrArr[] =
     {
      {CmdUnknown,  L""    },
      {CmdGet,      L"get" },
      {CmdGet,      L"get <system name or IP address <userid password> >" },
      {CmdHelp,     L"help"},
      {CmdQuestion, L"?"},
     };

int const lnInStrArr = sizeof(InStrArr)/sizeof(InStrArr[0]);

/**************************************************************************************************/
/*                                                                                                */
/*                                                                                                */
/**************************************************************************************************/
int                                                                                                         
_cdecl                                                                                                      
wmain(                                                // wmain yields WCHAR input string vector.            
      int     nbrArgs,                                                                                      
      PWCHAR  pArgv[]                                                                                       
     )                                                                                                      
   {                           
    int                  rc = rcOK,
                         rc2,
                         CmdNbr;
    char           const DateCompiledBase[] = __DATE__,                                                                                     
                         TimeCompiledBase[] = " "__TIME__;
    char                 DateCompiled[] =             // Build date in preferred (dd mmm yyyy) format.
                           {DateCompiledBase[4], DateCompiledBase[5], DateCompiledBase[6],
                            DateCompiledBase[0], DateCompiledBase[1], DateCompiledBase[2], DateCompiledBase[3],
                            DateCompiledBase[7], DateCompiledBase[8], DateCompiledBase[9], DateCompiledBase[10],
                            0x0
                           };
    BOOLEAN              bInitialized  = FALSE,
                         bHaveLocator  = FALSE,
                         bHaveServices = FALSE;
    IWbemLocator       * pLocator;
    IWbemServices      * pServices;
    BSTR                 in,                   
                         WMIRoot = NULL,
                         bUserid,
                         bPassword;
    PWCHAR               pUserid,
                         pPassword;
    HRESULT              hr;

    if (' '==DateCompiled[0])
     strcpy(RtnCompileInfo, DateCompiled+1);
    else
     strcpy(RtnCompileInfo, DateCompiled+0);

    strcat(RtnCompileInfo, TimeCompiledBase);

    printf(_T("\n" JARtnName " v" JARtnVer " (compiled %s)\n"), RtnCompileInfo);
    
    do
      {                                               // Big 'do' group.
       rc2 = ParseIt(                                 // Get parameters.
                     nbrArgs,
                     pArgv,
                     &CmdNbr                          // Returned command number.
                    );

       if (rcOK!=rc2)                                 // A problem?
         {
          printf(_T("\n\nmain(): Parsing problem\n"));
          rc = rcErr;
          break;
         }
    
       if (CmdHelp==CmdNbr)                           // Need help?    
         {    
          ParmInfo();
          break;    
         }
    
       // Initialize COM and get a pointer to the WbemLocator interface. We must use
       // the DCOM flavor of services on a computer where DCOM is installed. WBEMTEST
       // loads ole32.dll and uses GetProcAddress to learn these addresses. This test
       // program simply assumes that it's running in a DCOM-enabled environment.

       hr = CoInitializeEx(NULL,                      // Initialize COM environment for multi-threaded concurrency.
                           COINIT_MULTITHREADED
                          );

       if (FALSE==SUCCEEDED(hr))
         {
          ReportError(_T("main():  CoInitialize failed"), hr);
          rc = rcErr;
          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);
          rc = rcErr;
          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);
          rc = rcErr;
          break;
         }

       bHaveLocator = TRUE;

       if (3<=nbrArgs)                                // System specified?
         {                                            // Build root qualified by system, eg, '\\MyComputer\root\WMI'.
          hr = VarBstrCat(_bstr_t(L"\\\\"), _bstr_t(pArgv[2]), &in);

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

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

          SysFreeString(in);

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

       if (5==nbrArgs)                                // Userid and password specified?
         {
          bUserid = SysAllocString(pArgv[3]);
          bPassword = SysAllocString(pArgv[4]);
          pUserid = pArgv[3];
          pPassword = pArgv[4];
         }
       else
         {
          bUserid = NULL;                             // Use identity inherited from process.
          bPassword = NULL;                           // "
          pUserid = NULL;
          pPassword = 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
                                      );

⌨️ 快捷键说明

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