vc下不重启改ip、网关、dns

原文地址:http://hi.baidu.com/9908006/blog/item/bed174998952a8186f068c3e.html

看雪上某人发表的,貌似其它都可以改,就是dns有些问题,研究后发现,原来是类型有问题,改了后正常运行了

 
 
  1. //VC-ConsoleWithApi
  2. #include <windows.h>
  3. #include <string>
  4. #include <iostream>
  5. #pragma comment(lib,"Ws2_32.lib")
  6. using namespace std;
  7. void GetLanAdapterName( char* szLanAdapterName );
  8. BOOL RegIP( LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate, LPCTSTR pDNSServer1, LPCTSTR pDNSServer2 );
  9. BOOL NotifyIPChange( LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask );
  10. int main()
  11. {
  12. char AdapterName[ MAX_PATH ] ="";
  13. GetLanAdapterName( AdapterName );
  14. RegIP( AdapterName, "172.20.10.252","255.255.255.0", "172.20.10.1", "211.136.17.107", "211.136.18.171" );
  15. NotifyIPChange( AdapterName, 0, "172.20.10.252", "172.20.10.1" );
  16. return 0;
  17. }
  18. //读取注册表取得适配器名称
  19. void GetLanAdapterName(char* szLanAdapterName)
  20. {
  21. HKEY hKey, hSubKey, hNdiIntKey;
  22. if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &hKey ) != ERROR_SUCCESS )
  23. return;
  24. DWORD dwIndex = 0;
  25. DWORD dwBufSize = 256;
  26. DWORD dwDataType;
  27. char szSubKey[256];
  28. unsigned char szData[256];
  29. while( ::RegEnumKeyEx( hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL ) == ERROR_SUCCESS )
  30. {
  31. if( ::RegOpenKeyEx( hKey, szSubKey, 0, KEY_READ, &hSubKey ) == ERROR_SUCCESS )
  32. {
  33. if( ::RegOpenKeyEx( hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey ) == ERROR_SUCCESS )
  34. {
  35. dwBufSize = 256;
  36. if( ::RegQueryValueEx( hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
  37. {
  38. if( strcmp( (char*)szData, "ethernet" ) == 0 ) // 判断是不是以太网卡
  39. {
  40. dwBufSize = 256;
  41. if( ::RegQueryValueEx( hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
  42. {
  43. // szData 中便是适配器详细描述
  44. dwBufSize = 256;
  45. if( ::RegQueryValueEx( hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
  46. {
  47. //szData中便是适配器名称
  48. strcpy( szLanAdapterName, (const char *)szData );
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. ::RegCloseKey( hNdiIntKey );
  55. }
  56. ::RegCloseKey(hSubKey);
  57. }
  58. dwBufSize = 256;
  59. }
  60. /* end of while */
  61. ::RegCloseKey(hKey);
  62. }
  63. BOOL RegIP( LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate, LPCTSTR pDNSServer1, LPCTSTR pDNSServer2 )
  64. {
  65. HKEY hKey;
  66. string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
  67. strKeyName += lpszAdapterName;
  68. if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_WRITE, &hKey ) != ERROR_SUCCESS )
  69. return FALSE;
  70. string DNSServer = pDNSServer1;
  71. DNSServer += ",";
  72. DNSServer += pDNSServer2;
  73. char mszIPAddress[ 100 ];
  74. char mszNetMask[ 100 ];
  75. char mszNetGate[ 100 ];
  76. char mszDNSServer[ 100 ];
  77. strncpy( mszIPAddress, pIPAddress, 98 );
  78. strncpy( mszNetMask, pNetMask, 98 );
  79. strncpy( mszNetGate, pNetGate, 98 );
  80. strncpy( mszDNSServer, DNSServer.c_str(), 98 );
  81. int nIP, nMask, nGate, nDNSServer;
  82. nIP = strlen( mszIPAddress );
  83. nMask = strlen( mszNetMask );
  84. nGate = strlen( mszNetGate );
  85. nDNSServer = strlen( mszDNSServer );
  86. *( mszIPAddress + nIP + 1 ) = 0x00; // REG_MULTI_SZ数据需要在后面再加个0
  87. nIP += 2;
  88. *( mszNetMask + nMask + 1 ) = 0x00;
  89. nMask += 2;
  90. *( mszNetGate + nGate + 1 ) = 0x00;
  91. nGate += 2;
  92. *( mszDNSServer + nDNSServer + 1 ) = 0x00;
  93. nGate += 2;
  94. ::RegSetValueEx( hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP );
  95. ::RegSetValueEx( hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask );
  96. ::RegSetValueEx( hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate );
  97. ::RegSetValueEx( hKey, "NameServer", 0, REG_SZ, (unsigned char*)mszDNSServer, nDNSServer );
  98. ::RegCloseKey(hKey);
  99. return TRUE;
  100. }
  101. //未公开函数DhcpNotifyConfigChange位于 dhcpcsvc.dll中.
  102. //原型声明
  103. typedef BOOL ( CALLBACK* DHCPNOTIFYPROC) (
  104. LPWSTR lpwszServerName, // 本地机器为NULL
  105. LPWSTR lpwszAdapterName, // 适配器名称
  106. BOOL bNewIpAddress, // TRUE表示更改IP
  107. DWORD dwIpIndex, // 指明第几个IP地址,如果只有该接口只有一个IP地址则为0
  108. DWORD dwIpAddress, // IP地址
  109. DWORD dwSubNetMask, // 子网掩码
  110. int nDhcpAction // 对DHCP的操作 0:不修改, 1:启用 DHCP,2:禁用 DHCP
  111. );
  112. BOOL NotifyIPChange( LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask )
  113. {
  114. BOOL bResult = FALSE;
  115. HINSTANCE hDhcpDll;
  116. DHCPNOTIFYPROC pDhcpNotifyProc;
  117. WCHAR wcAdapterName[256];
  118. MultiByteToWideChar( CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256 );
  119. if( ( hDhcpDll = ::LoadLibrary("dhcpcsvc.dll") ) == NULL )
  120. return FALSE;
  121. if( ( pDhcpNotifyProc = (DHCPNOTIFYPROC)::GetProcAddress( hDhcpDll, "DhcpNotifyConfigChange" ) ) != NULL )
  122. if( (pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0 ) == ERROR_SUCCESS )
  123. bResult = TRUE;
  124. ::FreeLibrary(hDhcpDll);
  125. return bResult;
  126. }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值