wince修改网卡IP地址
WCHAR Names[50];
DWORD bytes;
HANDLE m_hFileHandle = CreateFile(_T("NDS0:"),0,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE)INVALID_HANDLE_VALUE);
if( m_hFileHandle == INVALID_HANDLE_VALUE )
{
MessageBox(_T("获得网卡名时打开设备错误"));
return;
}
// Get list of adapter names
if (!DeviceIoControl(m_hFileHandle,IOCTL_NDIS_GET_ADAPTER_NAMES, NULL,0,Names,MAX_PATH,&bytes,NULL))
{
MessageBox(_T("获得网卡名错误"));
return;
}
DWORD len = wcslen(Names);
Names[len] = 0;
Names[len+1] = 0;
CString strKeyName;
strKeyName.Format(_T("Comm\\%s\\Parms\\TCPIP"),Names);
//打开注册表对网卡IP信息对应子健进行修改
HKEY hkey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,strKeyName,0,KEY_WRITE,&hkey) != ERROR_SUCCESS)
{
MessageBox(_T("打开注册表错误"));
return;
}
DWORD value;
value = 0;
//set EnableDHCP
if(RegSetValueEx(hkey,TEXT("EnableDHCP"),0,REG_DWORD,(const unsigned char *)&value,sizeof(DWORD))
!= ERROR_SUCCESS)
{
MessageBox(_T("关闭自动获得IP错误"));
}
WCHAR buffer[32];
memset(buffer,0,64);
memcpy(buffer,m_strIp.GetBuffer(0),m_strIp.GetLength()*2);
if(RegSetValueEx(hkey,TEXT("IpAddress"),0,REG_MULTI_SZ,(const unsigned char *)buffer, m_strIp.GetLength()*2+2) != ERROR_SUCCESS)
{
MessageBox(TEXT("设置IP错误"));
}
memset(buffer,0,64);
memcpy(buffer,m_strMask.GetBuffer(0),m_strMask.GetLength()*2);
if(RegSetValueEx(hkey,TEXT("SubnetMask"),0,REG_MULTI_SZ,(const unsigned char *)buffer, m_strMask.GetLength()*2+2) != ERROR_SUCCESS)
{
MessageBox(TEXT("设置子网掩码错误"));
}
memset(buffer,0,64);
memcpy(buffer,m_strGateWay.GetBuffer(0),m_strGateWay.GetLength()*2);
if(RegSetValueEx(hkey,TEXT("DefaultGateway"),0,REG_MULTI_SZ,(const unsigned char *)buffer, m_strGateWay.GetLength()*2+2) != ERROR_SUCCESS)
{
MessageBox(TEXT("设置网关错误"));
}
RegFlushKey(hkey);
RegCloseKey(hkey);
// 重启网卡,不用机器热启动
HANDLE hNdis = CreateFile(_T("NDS0:"),0,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE) INVALID_HANDLE_VALUE);
if( hNdis == INVALID_HANDLE_VALUE )
{
MessageBox(_T( "重启网络驱动时打开设备错误"));
return;
}
// Send the device command. // buf contains the name of the
if (!DeviceIoControl( hNdis, IOCTL_NDIS_REBIND_ADAPTER,
Names, _tcslen( Names) + sizeof( TCHAR ),NULL, 0, NULL, NULL ) )
{
MessageBox(_T( "重启网络驱动错误"));
}
CloseHandle( hNdis );