• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Life & Code

            代碼是咒語,我是魔法師

            網卡類

            //說明:平時做更改網卡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;
            }

            posted on 2006-04-01 09:46 橙子 閱讀(1915) 評論(2)  編輯 收藏 引用 所屬分類: Win32

            評論

            # re: 網卡類 2006-04-01 21:19 音樂蟲子

            DOS批處理的解決方案:

            netsh < officeip.dat

            // file : officeip.dat
            int
            ip
            set address "Local Area Connection" static 10.0.8.68 255.0.0.0 10.20.30.239 1
            show address


              回復  更多評論   

            # re: 網卡類 2008-10-27 00:16 0czc0

            在vista下有什么好的方法解決?
            zcnet4@gmail.com  回復  更多評論   

            <2006年4月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導航

            統計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            相冊

            收藏夾

            搜索

            最新評論

            閱讀排行榜

            韩国三级大全久久网站| 国产午夜精品理论片久久影视| 香蕉久久夜色精品国产小说| 伊人色综合久久天天| 久久久受www免费人成| 久久成人国产精品免费软件| 国产精品一区二区久久精品| 人妻少妇精品久久| 久久不见久久见免费视频7| 久久国产成人午夜AV影院| 欧美亚洲色综久久精品国产| 国内精品免费久久影院| 久久国产高潮流白浆免费观看| 国产成人无码精品久久久免费| 伊人久久综合无码成人网| 激情综合色综合久久综合| 久久精品人人槡人妻人人玩AV| 久久精品无码免费不卡| 99久久无色码中文字幕| 一本色道久久综合狠狠躁| 日韩美女18网站久久精品| 久久久青草久久久青草| 久久亚洲精精品中文字幕| 中文字幕久久精品 | 久久高清一级毛片| 丰满少妇高潮惨叫久久久| 囯产精品久久久久久久久蜜桃 | 久久久久久一区国产精品| 国产精品久久久久9999| 久久大香香蕉国产| 精品综合久久久久久888蜜芽| 久久中文字幕人妻熟av女| 怡红院日本一道日本久久 | 久久无码专区国产精品发布| 久久国产精品二国产精品| 久久免费精品视频| 爱做久久久久久| 精品国产乱码久久久久久浪潮| 国产69精品久久久久99| 很黄很污的网站久久mimi色| 深夜久久AAAAA级毛片免费看|