不重起Windows直接更改IP地址
原文没有对98、ME、NT的支持。
家里装着VMWARE,也就完善它一下了。
这里还加了个对HOSTS文件的处理。因为LOTUS的需要。
OSVERSIONINFO osvi;
memset(&osvi, 0, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&osvi);
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) //2k3
winver=7;
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) //xp
winver=6;
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) //2k
winver=5;
if ( osvi.dwMajorVersion <= 4 && osvi.dwPlatformId==VER_PLATFORM_WIN32_NT) //nt
winver=4;
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) //me
winver=3;
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) //98
winver=2;
TCHAR szDir [MAX_PATH];
if(winver>=4)
{
:: GetSystemDirectory (szDir, MAX_PATH);
Path=szDir;
Path+="\\drivers\\etc\\hosts";
}
else
{
::GetWindowsDirectory(szDir, MAX_PATH);
Path=szDir;
Path+="\\Hosts";
}
bool GetAdapterInfo()
{
HKEY hKey, hSubKey, hNdiIntKey;
LPCTSTR _key="";
if(winver>4)
_key="System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}";
if(winver==4)
_key="SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
if(winver<4)
_key="System\\CurrentControlSet\\Services\\Class\\NetTrans";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_key,
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;
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(winver>3)
{
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)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
pAI->strName = (LPCTSTR)szData;
RegGetIP(pAI, (LPCTSTR)szData);
}
AdapterInfoVector.push_back(pAI); // 加入到容器中
}
}
}
RegCloseKey(hNdiIntKey);
}
}
else
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
if(strcmp((char*)szData, "TCP/IP") == 0)
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = (LPCTSTR)szData;
pAI->strName = szSubKey;
RegGetIP(pAI, (LPCTSTR)szData);
AdapterInfoVector.push_back(pAI); // 加入到容器中
}
}
}
RegCloseKey(hSubKey);
}
dwBufSize = 256;
}
RegCloseKey(hKey);
return TRUE;
}
BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
ASSERT(pAI);
HKEY hKey;
string strKeyName="\\";
if(winver>=4)
{
strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
}
else
{
strKeyName = "System\\CurrentControlSet\\Services\\Class\\NetTrans\\";
strKeyName += pAI->strName;
}
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;
unsigned char szData[256];
DWORD dwDataType, dwBufSize;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = (LPCTSTR)szData;
dwBufSize = 256;
if(winver>=4)
{
if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;
}
else
{
if(RegQueryValueEx(hKey, "IPMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;
}
dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = (LPCTSTR)szData;
RegCloseKey(hKey);
return TRUE;
}
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName="\\";
if(winver>=4)
{
strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
}
else
{
strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Class\\NetTrans\\";
strKeyName += AdapterInfoVector[0]->strName;
}
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);
int nIP, nMask, nGate;
nIP = (int)strlen(mszIPAddress);
nMask = (int)strlen(mszNetMask);
nGate = (int)strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
if(winver>=4)
{
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
}
else
{
RegSetValueEx(hKey, "IPMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
}
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
void CChangeIPDlg::WriteHost()
{
CStdioFile mFile;
CFileException mExcept;
CString str,newstr;
if(mFile.Open( Path, CFile::modeReadWrite, &mExcept))
{
while(mFile.ReadString(str))
{
if(str.Find("*.*.136.*")!=-1)
str.Replace("*.*.136.*","*.*.158.*");
newstr+=str+"\n";
}
mFile.SetLength(0);
mFile.SeekToEnd();
mFile.WriteString(newstr);
mFile.Close();
AfxMessageBox("办公自动化设置已更新!");
}
else
AfxMessageBox("无法打开HOST文件!如果您的办公自动化软件是5.0版,请运行连接配置向导!");
}
9x系列仍需要重启,因为不支持DhcpNotifyConfigChange。网上找了找,也没什么好方法。
原文没有对98、ME、NT的支持。
家里装着VMWARE,也就完善它一下了。
这里还加了个对HOSTS文件的处理。因为LOTUS的需要。
9x系列仍需要重启,因为不支持DhcpNotifyConfigChange。网上找了找,也没什么好方法。
浙公网安备 33010602011771号