wince设置ip地址
bool SetIpAddress(CString m_strIp,CString m_strMask,CString m_strGateWay)
{
//获得网卡的设备名
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(NULL,_T("获得网卡名时打开设备错误"),NULL,0);
return false;
}
// Get list of adapter names
if (!DeviceIoControl(m_hFileHandle,
IOCTL_NDIS_GET_ADAPTER_NAMES,
NULL,0,
Names,MAX_PATH,&bytes,NULL))
{
MessageBox(NULL,_T("获得网卡名错误"),NULL,0);
return false;
}
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(NULL,TEXT("打开注册表错误"),NULL,0);
return false;
}
DWORD value;
value = 0;
WCHAR buffer[50];
//set EnableDHCP
if(RegSetValueEx(hkey,TEXT("EnableDHCP"),0,REG_DWORD,(const unsigned char *)&value,sizeof(DWORD)) != ERROR_SUCCESS)
MessageBox(NULL,TEXT("关闭自动获得IP错误"),NULL,0);
//set ip address
memset(buffer,0,100);
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(NULL,TEXT("设置IP错误"),NULL,0);
//set subnet mask
memset(buffer,0,100);
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(NULL,TEXT("设置子网掩码错误"),NULL,0);
//set gateway
memset(buffer,0,100);
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(NULL,TEXT("设置网关错误"),NULL,0);
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(NULL,_T( "重启网络驱动时打开设备错误"),NULL,0);
return false;
}
// Send the device command.
if (!DeviceIoControl( hNdis, IOCTL_NDIS_REBIND_ADAPTER,
Names, _tcslen( Names) + sizeof( TCHAR ), // buf contains the name of the
NULL, 0, NULL, NULL ) )
{
MessageBox(NULL,_T( "重启网络驱动错误"),NULL,0);
}
CloseHandle( hNdis );
return true;
}
wince的网络配置注册表信息
首先,必须知道网卡驱动名字。在s3c2410上,名字叫“CS8900”,用工具连接上wince查看他的注册表。你将可以得到下面的东西。
[HKEY_LOCAL_MACHINE/Comm/CS8900]
"DisplayName"="CS8900"
"Group"="NDIS"
"ImagePath"="CS8900.dll"
[HKEY_LOCAL_MACHINE/Comm/CS8900/Linkage]
"Route"=hex(7):/
43,53,38,39,30,30,31,00,00,00,00
上面的hex值其实是:CS89001,同样可以在[HKEY_LOCAL_MACHINE/Comm/CS8900]项下面看到关于CS89001的表述。
[HKEY_LOCAL_MACHINE/Comm/CS89001]
"DisplayName"="CS8900"
"Group"="NDIS"
"ImagePath"="CS8900.dll"
[HKEY_LOCAL_MACHINE/Comm/CS89001/Parms]
"BusNumber"=dword:00000000
"BusType"=dword:00000000
"InterruptNumber"=dword:0000000a
"Sysintr"=dword:0000001a
"DuplexMode"=dword:00000001
"IoBaseAddress"=dword:a7000300
"Transceiver"=dword:00000003
"CardType"=dword:00000001
[HKEY_LOCAL_MACHINE/Comm/CS89001/Parms/TcpIp]
"IpAddress"="192.168.1.109"
"EnableDHCP"=dword:00000000
"LeaseObtainedLow"=dword:6a806780
"AutoInterval"=dword:0000012c
"AutoMask"="255.255.0.0"
"AutoSubnet"="169.254.0.0"
"AutoIP"="0.0.0.0"
"AutoSeed"=dword:00000000
"PrevReqOptions"=hex:/
07,01,03,06,0f,2c,2e,2f
"T2"=dword:000bae0a
"T1"=dword:0006ac98
"Lease"=dword:000d5930
"LeaseObtainedHigh"=dword:01c84915
"DhcpDNS"=hex(7):/
31,39,32,2e,31,36,38,2e,31,2e,38,00,00,00,00
"DhcpDefaultGateway"="0.0.0.0"
"DhcpServer"="192.168.1.8"
"DhcpSubnetMask"="255.255.255.0"
"DhcpIPAddress"="192.168.1.63"
"AutoCfg"=dword:00000001
"DefaultGateway"=hex(7):/
31,39,32,2e,31,36,38,2e,31,2e,38,00,00,00,00
"DNS"=hex(7):/
31,39,32,2e,31,36,38,2e,31,2e,31,00,00,00,00
"Subnetmask"=hex(7):/
32,35,35,2e,32,35,35,2e,32,35,35,2e,30,00,00,00,00
"LLInterface"=""
"UseZeroBroadcast"=dword:00000000
内容很多,但是我们一般关心的就只有[HKEY_LOCAL_MACHINE/Comm/CS89001/Parms/TcpIp]下的几个。
"EnableDHCP"=dword:0
"DefaultGateway"="192.168.1.1"
"IpAddress"="192.168.1.100"
"Subnetmask"="255.255.255.0"
"DNS"="192.168.1.1"
"DefaultGateway"=hex(7):/
31,39,32,2e,31,36,38,2e,31,2e,38,00,00,00,00
"EnableDHCP"--控制DHCP,如果把他的值改为1,那么系统将用DHCP分配IP,设置的IP等信息是没有效果的。也就是自动获取。
如果"EnableDHCP"为0,那么可以通过"IpAddress","Subnetmask","DefaultGateway"等设置网络信息。其他信息如果有需要自己修改。
至于修改注册表的编程也不是难事,在wince中有函数可以帮助你完成这件事情。
RegCreateKeyEx
RegQueryValueEx
RegSetValueEx
RegCloseKey
其中RegSetValueEx函数可以通过不同的宏写如不同类型的值,比如:
REG_SZ
REG_BINARY
REG_DWORD
还有更多的,具体看winnt.h头文件。
已于读取我想就不用说了,自己看看evc的帮助吧。
DHCP自动获取IP的具体信息相信你也从上面的注册表信息看到了,需要就读取一下。
PS:注册表操作还有其他函数,根据自己情况用吧。