找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 6851|回复: 4

[开源] vc 不重启改ip

  [复制链接]

16

主题

81

回帖

0

精华

银牌会员

积分
611
发表于 2010-11-25 22:45:55 | 显示全部楼层 |阅读模式
看雪上某人发表的,貌似其它都可以改,就是dns有些问题,研究后发现,原来是类型有问题,改了后正常运行了

//VC-ConsoleWithApi
#include <windows.h>
#include <string>

#include <iostream>
#pragma comment(lib,"Ws2_32.lib")
using namespace std;
void GetLanAdapterName( char* szLanAdapterName );
BOOL RegIP( LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate,  LPCTSTR pDNSServer1,  LPCTSTR pDNSServer2 );
BOOL NotifyIPChange( LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask );

int main()
{

   char AdapterName[ MAX_PATH ] ="";

   GetLanAdapterName( AdapterName );

   RegIP( AdapterName, "172.20.10.252","255.255.255.0", "172.20.10.1", "211.136.17.107", "211.136.18.171" );
   
   NotifyIPChange( AdapterName, 0, "172.20.10.252",  "172.20.10.1" );
   

   return 0;
}



//读取注册表取得适配器名称
void GetLanAdapterName(char* szLanAdapterName)
{   
   HKEY hKey, hSubKey, hNdiIntKey;
    if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &hKey ) != ERROR_SUCCESS )
        return;
    DWORD dwIndex = 0;
    DWORD dwBufSize = 256;
    DWORD dwDataType;
    char szSubKey[256];
    unsigned char szData[256];
    while( ::RegEnumKeyEx( hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL ) == ERROR_SUCCESS )
    {        
      if( ::RegOpenKeyEx( hKey, szSubKey, 0, KEY_READ, &hSubKey ) == ERROR_SUCCESS )
        {            
         if( ::RegOpenKeyEx( hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey ) == ERROR_SUCCESS )
         {              
            dwBufSize = 256;
                if( ::RegQueryValueEx( hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
                {
                    if( strcmp( (char*)szData, "ethernet" ) == 0 ) // 判断是不是以太网卡
               {            
                  dwBufSize = 256;
                        if( ::RegQueryValueEx( hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
                        {   
                     // szData 中便是适配器详细描述
                     dwBufSize = 256;
                            if( ::RegQueryValueEx( hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
                     {  
                        //szData中便是适配器名称  
                        strcpy( szLanAdapterName, (const char *)szData );
                        break;
                            }
                        }
                    }
               }
                ::RegCloseKey( hNdiIntKey );
            }  
         ::RegCloseKey(hSubKey);
        }  
      dwBufSize = 256;
    }
   /* end of while */   
   
   ::RegCloseKey(hKey);
}


BOOL RegIP( LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate,  LPCTSTR pDNSServer1,  LPCTSTR pDNSServer2 )
{
   HKEY hKey;
   string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
   strKeyName += lpszAdapterName;
   if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_WRITE, &hKey ) != ERROR_SUCCESS )
      return FALSE;

   string DNSServer = pDNSServer1;
   DNSServer += ",";
   DNSServer += pDNSServer2;

   char mszIPAddress[ 100 ];
   char mszNetMask[ 100 ];
   char mszNetGate[ 100 ];
   char mszDNSServer[ 100 ];
   
   
   strncpy( mszIPAddress, pIPAddress, 98 );
   strncpy( mszNetMask, pNetMask, 98 );
   strncpy( mszNetGate, pNetGate, 98 );
   strncpy( mszDNSServer, DNSServer.c_str(), 98 );

   int nIP, nMask, nGate, nDNSServer;

   nIP = strlen( mszIPAddress );
   nMask = strlen( mszNetMask );
   nGate = strlen( mszNetGate );
   nDNSServer = strlen( mszDNSServer );

   *( mszIPAddress + nIP + 1 ) = 0x00; // REG_MULTI_SZ数据需要在后面再加个0
   nIP += 2;

   *( mszNetMask + nMask + 1 ) = 0x00;
   nMask += 2;

   *( mszNetGate + nGate + 1 ) = 0x00;
   nGate += 2;

   *( mszDNSServer + nDNSServer + 1 ) = 0x00;
   nGate += 2;


   ::RegSetValueEx( hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP );
   ::RegSetValueEx( hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask );
   ::RegSetValueEx( hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate );
   ::RegSetValueEx( hKey, "NameServer", 0, REG_SZ, (unsigned char*)mszDNSServer, nDNSServer );

   ::RegCloseKey(hKey);

   return TRUE;
}



//未公开函数DhcpNotifyConfigChange位于 dhcpcsvc.dll中.
//原型声明
typedef BOOL ( CALLBACK* DHCPNOTIFYPROC) (
      LPWSTR lpwszServerName,      // 本地机器为NULL
      LPWSTR lpwszAdapterName,   // 适配器名称
      BOOL bNewIpAddress,         // TRUE表示更改IP
      DWORD dwIpIndex,         // 指明第几个IP地址,如果只有该接口只有一个IP地址则为0
      DWORD dwIpAddress,         // IP地址
      DWORD dwSubNetMask,         // 子网掩码
      int nDhcpAction            // 对DHCP的操作 0:不修改, 1:启用 DHCP,2:禁用 DHCP
);


BOOL NotifyIPChange( LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask )
{
   BOOL bResult = FALSE;
   HINSTANCE hDhcpDll;
   DHCPNOTIFYPROC pDhcpNotifyProc;
   WCHAR wcAdapterName[256];

   MultiByteToWideChar( CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256 );

   if( ( hDhcpDll = ::LoadLibrary("dhcpcsvc.dll") ) == NULL )
      return FALSE;

   if( ( pDhcpNotifyProc = (DHCPNOTIFYPROC)::GetProcAddress( hDhcpDll, "DhcpNotifyConfigChange" ) ) != NULL )
      if(  (pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0 ) == ERROR_SUCCESS )
         bResult = TRUE;

   ::FreeLibrary(hDhcpDll);
   return bResult;
}

评分

参与人数 2 +40 +40 水晶币 +20 +40 收起 理由
Tesla.Angela + 20 + 20 + 10 + 20 先加分,哪天我改成VB的。。。
阿杰 + 20 + 20 + 10 + 20 希望以后多发这样实用的代码

查看全部评分

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2010-11-26 15:06:25 | 显示全部楼层
这代码不错,非常实用。
【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755

0

主题

92

回帖

0

精华

铜牌会员

积分
166
发表于 2012-3-16 14:35:30 | 显示全部楼层
代码不错,谢谢提供
头像被屏蔽

4

主题

101

回帖

0

精华

初来乍到

积分
19658
发表于 2014-1-16 13:27:39 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

30

主题

693

回帖

0

精华

钻石会员

积分
2815
发表于 2015-4-12 23:34:44 | 显示全部楼层
真不错 感谢楼主分享
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表