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

📄 setipaddrarray.cpp

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

/**************************************************************************************************/
/*                                                                                                */
/* Parameters:  get | set | setdflt | help | ?                                                    */
/*                                                                                                */
/*   get     -- return the current information (about all adapters).                              */
/*   set     -- set the information.  The optional adapter name specifies to which adapter the    */
/*              operation is applied.  If none is given, the operation is applied to all adapters */
/*              found.                                                                            */
/*   setdflt -- set the information to no IP addresses.  The optional adapter name specifies to   */
/*              which adapter the operation is applied.  If none is given, the operation is       */
/*              applied to all adapters found.                                                    */
/*                                                                                                */
/*   help    -- supply parameter usage.                                                           */
/*                                                                                                */
/*   ?       -- supply parameter usage.                                                           */
/*                                                                                                */
/*                                                                                                */
/* Usage:                                                                                         */
/*                                                                                                */
/*   SetIPAddrArray < operation < operands > >                                                    */
/*                                                                                                */
/*   where <operation> is one of these:                                                           */
/*                                                                                                */
/*     get                                                                                        */
/*     set                                                                                        */
/*     setdflt                                                                                    */
/*     help                                                                                       */
/*     ?                                                                                          */
/*                                                                                                */
/*   <operands> is one or more of the following:                                                  */
/*                                                                                                */
/*     < system name >                                                                            */
/*                                                                                                */
/*     < system name <userid password> >                                                          */
/*                                                                                                */
/*       < userid password > is allowed only after < system name >, eg,                           */
/*                                                                                                */
/*         mysystem myid mypassword                                                               */
/*                                                                                                */
/*     a=< adapter name> >                                                                        */
/*                                                                                                */
/*       < adapter name > is allowed only with 'set' and must be the final parameter, eg,         */
/*                                                                                                */
/*         set a="My Favorite NIC"                                                                */
/*                                                                                                */
/*       or                                                                                       */
/*                                                                                                */
/*         set mysystem myid mypassword a="My Favorite NIC"                                       */
/*                                                                                                */
/*       (Double quotation marks are required if the name has imbedded blanks.)                   */
/*                                                                                                */
/*                                                                                                */
/* Notes:                                                                                         */
/*                                                                                                */
/*   1) An entirely empty input file (as opposed to one with simply a blank line) causes the      */
/*      IP address array of an adapter to be set to empty, namely, no addresses.                  */
/*                                                                                                */
/**************************************************************************************************/

#define JARtnName           "SetIPAddrArray"
#define JARtnVer            "2.12"
#define DriverClassNameW    L"PassthruIPAddrArray"

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

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

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

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

int
GetData(DWORD, PVOID *, PULONG);

int
ArrangeData(PVOID, int, PVOID, int *);

int
_cdecl
CompareIPAddrs(const void *, const void *);

int
ParseIt(int, PWCHAR [], PULONG, PWCHAR *, PWCHAR *, PWCHAR *, PWCHAR *);
        
void        
ParmInfo();        
        
/**************************************************************************************************/
/*                                                                                                */
/* Globals.                                                                                       */
/*                                                                                                */
/**************************************************************************************************/

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

// The next is the name of the file that provides IP addresses in the case of a set operation.

#define FileName            "c:\\Temp\\Passthru\\AddrArray.txt"

#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 CmdSet       2
#define CmdSetDflt   3
#define CmdHelp      4

const struct
  {
   DWORD   CmdCode;
   PWCHAR  pInStr;
  }
   InStrArr[] =
     {
      {CmdUnknown,  L""       },
      {CmdGet,      L"get"    },
      {CmdSet,      L"set"    },
      {CmdSetDflt,  L"setdflt"},
      {CmdHelp,     L"help"   },
      {CmdHelp,     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;
    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
                           };
    PVOID                pIPAddrArr = NULL;
    ULONG                szArr,
                         CmdOper;
    BOOLEAN              bInitialized  = FALSE,
                         bHaveLocator  = FALSE,
                         bHaveServices = FALSE;
    IWbemLocator       * pLocator;
    IWbemServices      * pServices;
    PWCHAR               pAdapterName,
                         pSystemName,
                         pUserid,
                         pPassword;
    BSTR                 in,
                         WMIRoot = NULL,
                         bUserid,
                         bPassword;
    HRESULT              hr = NOERROR;

    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);
    
    rc2 = ParseIt(                                    // Get parameters.
                  nbrArgs,
                  pArgv,
                  &CmdOper,                           // Returned command number.
                  &pSystemName,                       // Returned system name, if given.
                  &pUserid,                           // Returned userid, if given,
                  &pPassword,                         // Returned password, if given.
                  &pAdapterName                       // Returned adapter name (if command = set and if given).
                 );
    
    if (rcOK!=rc2)                                    // A problem?
      {
       printf(_T("main():  Parsing problem\n"));
       rc = rcErr;    
       goto done;
      }    
    
    if (CmdHelp==CmdOper)                             // Help?    
      {    
       ParmInfo();    
       goto done;    
      }
    
    rc2 = GetData(                                    // Read data into an array of IP addresses.
                  CmdOper,                            // Command operation value.
                  &pIPAddrArr,
                  &szArr
                 );

    if (rcOK!=rc2)                                    // A problem?
      {
       printf(_T("main():  GetData() had a problem, rc = 0x%08x\n"), rc2);
       rc = rcErr;
       goto done;
      }
    
    if (CmdSetDflt==CmdOper)                          // Is command 'set default"?
      CmdOper = CmdSet;                               // Hereafter use 'set'.

    szArr /= sizeof(ULONG);                           // Get number of IP addresses in array.
    
    qsort(                                            // Sort the addresses.
          pIPAddrArr,
          szArr,
          sizeof(ULONG),
          CompareIPAddrs
         );
    
    // 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.

    do
      {                                               // Big 'do' group.
       hr = CoInitializeEx(NULL,                      // Initialize COM environment for multi-threaded concurrency.
                           COINIT_MULTITHREADED
                          );

⌨️ 快捷键说明

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