原文地址:http://hi.baidu.com/9908006/blog/item/bed174998952a8186f068c3e.html
看雪上某人发表的,貌似其它都可以改,就是dns有些问题,研究后发现,原来是类型有问题,改了后正常运行了
-
- #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 )
- {
-
- dwBufSize = 256;
- if( ::RegQueryValueEx( hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )
- {
-
- strcpy( szLanAdapterName, (const char *)szData );
- break;
- }
- }
- }
- }
- ::RegCloseKey( hNdiIntKey );
- }
- ::RegCloseKey(hSubKey);
- }
- dwBufSize = 256;
- }
-
-
- ::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;
- 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;
- }
-
-
-
-
-
- typedef BOOL ( CALLBACK* DHCPNOTIFYPROC) (
- LPWSTR lpwszServerName,
- LPWSTR lpwszAdapterName,
- BOOL bNewIpAddress,
- DWORD dwIpIndex,
- DWORD dwIpAddress,
- DWORD dwSubNetMask,
- int nDhcpAction
- );
-
-
- 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;
- }