网卡类

网卡类

//说明:平时做更改网卡IP这样的活挺多的,粘来粘去麻烦,简单的打了个包.
//引用:VCkbase_不重起Windows直接更改IP地址
//代码:


//Adapter_.h
#ifndef _ADAPTER_H
#define _ADAPTER_H
#include <tchar.h>
#include <Windows.h>
#include <stdio.h>
#include <Iphlpapi.h>
#pragma comment(lib,"iphlpapi.lib")
#pragma comment(lib,"ws2_32.lib")
#include <assert.h>
#include <string>
#include <vector>
using   namespace  std;


typedef int (CALLBACK* DHCPNOTIFYPROC)(LPWSTR, LPWSTR, BOOL, DWORD, DWORD, DWORD, int);

class CAdapter
{
 private:
  class ADAPTER_INFO
  {
    string strName;   // 适配器名称
    string strDriverDesc; // 适配器描述
    string strIP;   // IP地址
    string strSubnetMask;  // 子网掩码
    string strNetGate;  // 网关
    string strDNS;         //DNS
    string strMAC;
    ::MIB_IFROW IfRow;  //用于流量,状态显示

    BOOL   RegSetIP();
    BOOL   ChangeSysSet();
    
   public:
    void   SetInx(DWORD _dwpIndex) { IfRow.dwIndex  = _dwpIndex; }

    //取得IP信息
    string& GetName(){  return strName; }
    string& GetDriverDesc(){ return strDriverDesc; }
    string& GetIP(){ return strIP; }
    string& GetSubnetMask(){ return strSubnetMask; }
    string& GetNetGate(){ return strNetGate; }
    string& GetDNS(){ return strDNS; }
    string& GetMAC(){ return strMAC; }
    
    //取得状态,流量
    DWORD    GetState(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     /*
     #define MIB_IF_OPER_STATUS_NON_OPERATIONAL      0
     #define MIB_IF_OPER_STATUS_UNREACHABLE          1
     #define MIB_IF_OPER_STATUS_DISCONNECTED         2
     #define MIB_IF_OPER_STATUS_CONNECTING           3
     #define MIB_IF_OPER_STATUS_CONNECTED            4
     #define MIB_IF_OPER_STATUS_OPERATIONAL          5
     */
     return IfRow.dwOperStatus ;
    }
    
    DWORD    GetSendBytes(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     return IfRow.dwOutOctets;
    }
    DWORD    GetReceiveBytes(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }  
     return IfRow.dwInOctets;
    }
    DWORD   GetSpeed(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     return IfRow.dwSpeed;
    }

    DWORD   GetOutUcastPkts(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     return IfRow.dwOutUcastPkts;
    } 
    DWORD   GetOutNUcastPkts(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     return IfRow.dwOutNUcastPkts;
    } 
    DWORD   GetInUcastPkts(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     return IfRow.dwInUcastPkts;
    } 
    DWORD   GetInNUcastPkts(){
     if(GetIfEntry(&IfRow) != NO_ERROR)
     {
      return 0;
     }
     return IfRow.dwInNUcastPkts;
    } 
    //设置IP信息
    BOOL SetName(string _PstrName  ,BOOL bChange = 0)  { 
     strName = _PstrName ;
     if( bChange)
      return ChangeSysSet();
     return TRUE;
    }
    BOOL SetDriverDesc(string _PstrDriverDesc,BOOL bChange = 0){  
     strDriverDesc = _PstrDriverDesc;
     if( bChange)
      ChangeSysSet();
     return TRUE;
    }
    BOOL SetIP(string _PstrIP,BOOL bChange = 0){  
     strIP = _PstrIP ;
     if( bChange)
      ChangeSysSet();
     return TRUE;
    }
    BOOL SetSubnetMask(string _PstrSubnetMask,BOOL bChange = 0){  
     strSubnetMask = _PstrSubnetMask;
     if( bChange)
      ChangeSysSet();
     return TRUE;
    }
    BOOL SetNetGate(string _PstrNetGate,BOOL bChange = 0){ 
     strNetGate = _PstrNetGate;
     if( bChange)
      ChangeSysSet();
     return TRUE;
    }
    BOOL SetDNS(string strSetDNS = ""){
     if( !strSetDNS.length() )
     {
      HKEY hKey;
      DWORD dwType;
      char  szReadBuf[64];
      DWORD cbData = 64;
      string strKeyName ="SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
      strKeyName += this->strName;
      if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,strKeyName.c_str(),0,
         KEY_READ,&hKey) != ERROR_SUCCESS)
      {
       return FALSE;
      }
      if( RegQueryValueExA(hKey,"NameServer",0,&dwType,(BYTE*)szReadBuf,&cbData) != ERROR_SUCCESS)
      {
       return FALSE;
      }
      RegCloseKey(hKey);
      strDNS = szReadBuf;
     }
     else
     {
      strDNS = strSetDNS;
      return ChangeSysSet();
     }
     return TRUE;
    }
    void SetMAC(BYTE *Address){ 
      char  buf[6];
      for(int i= 0;i< 6;i++)
      {
       sprintf( buf,"%02x",Address[i]);
       strMAC += string(buf);
      }
      /*
      sprintf(pAI->mac, "%02X%02X%02X%02X%02X%02X",
       int (pAdapterInfo->Address[0]),
       int (pAdapterInfo->Address[1]),
       int (pAdapterInfo->Address[2]),
       int (pAdapterInfo->Address[3]),
       int (pAdapterInfo->Address[4]),
       int (pAdapterInfo->Address[5]));
      */
    }
  };
 vector<ADAPTER_INFO> m_AdapterVec;
 public:
  CAdapter(void);
  ~CAdapter(void);

  size_t GetAdapterCount ()
  {
   return m_AdapterVec.size();
  }

  ADAPTER_INFO& operator[](DWORD iInx)
  {
   assert(iInx < m_AdapterVec.size());
   return m_AdapterVec[iInx];
  }
};


#endif


//------------------------------------------
//Adapter_.cpp
#include ".\adapter_.h"


CAdapter::CAdapter(void)
{
 DWORD ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO);
 IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
 if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大
 {
  delete pAdapterInfo;
  pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
 }
 if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS )
 {
  do {
   if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
   {
    ADAPTER_INFO *pAI = new ADAPTER_INFO;
    pAI->SetInx(pAdapterInfo->Index);
    pAI->SetName(pAdapterInfo->AdapterName);
    pAI->SetDriverDesc(pAdapterInfo->Description);
    pAI->SetMAC(pAdapterInfo->Address);
    pAI->SetIP(pAdapterInfo->IpAddressList.IpAddress.String);
    pAI->SetNetGate(pAdapterInfo->GatewayList.IpAddress.String);
    pAI->SetSubnetMask(pAdapterInfo->IpAddressList.IpMask.String);
    pAI->SetDNS();
    m_AdapterVec.push_back(*pAI);
   }
   pAdapterInfo = pAdapterInfo->Next;
  } while(pAdapterInfo);
 }
 delete pAdapterInfo;
}

CAdapter::~CAdapter(void)
{
 m_AdapterVec.clear();
}

BOOL CAdapter::ADAPTER_INFO::ChangeSysSet()
{
 //在注册表中修改信息
 if(!RegSetIP())
 {
  return FALSE;
 }

 HINSTANCE  hDhcpDll;
 DHCPNOTIFYPROC pDhcpNotifyProc;
 WCHAR wcAdapterName[256];
 MultiByteToWideChar(CP_ACP, 0, this->strName.c_str(), -1, wcAdapterName,256);

 if((hDhcpDll = LoadLibraryA("dhcpcsvc")) == NULL)
 {
  return FALSE;
 }

 if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
 {
  if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE,
   0,    //指明第几个IP地址,如果只有该接口只有一个IP地址则为0
   inet_addr(strIP.c_str()), //
   inet_addr(strSubnetMask.c_str()),
   0    //对DHCP的操作 0:不修改, 1:启用 DHCP,2:禁用 DHCP
   ) != ERROR_SUCCESS)
  {
   FreeLibrary(hDhcpDll);
   return FALSE;
  }
  FreeLibrary(hDhcpDll);
 }
 return TRUE;
}

BOOL CAdapter::ADAPTER_INFO::RegSetIP()
{
 HKEY hKey;
 string strKeyName ="SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
 strKeyName += strName;

 if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  strKeyName.c_str(),
  0,
  KEY_WRITE,
  &hKey) != ERROR_SUCCESS)
 {
  return FALSE;
 }
 strIP.push_back('\0');
 strSubnetMask.push_back('\0');
 strNetGate.push_back('\0');

 RegSetValueExA(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)strIP.data(), (DWORD)strIP.length()+2);
 RegSetValueExA(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)strSubnetMask.data(),(DWORD)strSubnetMask.length()+2 );
 RegSetValueExA(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)strNetGate.data(), (DWORD)strNetGate.length()+2);
 RegSetValueExA(hKey, "NameServer", 0, REG_SZ, (unsigned char*)strDNS.data(),(DWORD) strDNS.length());
 RegCloseKey(hKey);
 return TRUE;
}

你可能感兴趣的:(网卡类)