副标题[/!--empirenews.page--]
https://blog.csdn.net/bbdxf/article/details/7548443
Windows下程序修改IP的三种方法
????以下讨论的平台依据是Window?XP?+?SP1,?不考虑Windows其它版本的兼容性问题,?但对NT系列的系统,?理论上是通用的.?
方法一:?网卡重启? ????更改Windows网卡属性选项中IP地址,?通过对比前后注册表,?可以发现以下几处发生变化? [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParametersInterfaces{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}]? "IPAddress"? "SubnetMask"? "DefaultGateway"? "NameServer"?
[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}ParametersTcpip]? "IPAddress"? "SubnetMask"? "DefaultGateway"?
[HKEY_LOCAL_MACHINESYSTEMControlSet001ServicesTcpipParametersInterfaces{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}]? "IPAddress"? "SubnetMask"? "DefaultGateway"? "NameServer"?
[HKEY_LOCAL_MACHINESYSTEMControlSet001Services{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}ParametersTcpip]? "IPAddress"? "SubnetMask"? "DefaultGateway"?
????其中{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}是网卡名称(AdapterName),?不同的网卡,?不同的接入位置,?不同的接入的时间,?对应的值都不一样,?它的值是第一次接入系统时,?由系统生成的GUID值.? ????此处CurrentControlSet实际是ControlSet001的别名.????? [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParametersInterfaces{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}]? "IPAddress"? "SubnetMask"? "DefaultGateway"? "NameServer"? ????是主要的设置处.?
[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices{97EFDAD8-EB2D-4F40-9B07-0FCD706FCB6D}ParametersTcpip]? "IPAddress"? "SubnetMask"? "DefaultGateway"? ????对一些服务有影响,?如不设置,?用netstat可以看到原来的IP地址仍处于监听状态(?).?
????但为了使设置生效,?还有很重要的一步,?即重启网卡.?
????更改网卡的配置,?一般而言需要重启网卡,?如? ????Linux系统,?只需运行? ????????#ifconfig?eth0?down? ????????#ifconfig?eht0?up? ????就可以实现网卡的重启.?
????Windows环境下的步骤与之类似:?先禁用本地连接(网卡),?再启用本地连接(网卡).?但没有相应的命令或者直接的API.?所幸的是DDK提供一套设备安装函数,?用于控制系统设备,?包括控制设备的状态改变.?
/****************************************************************************************? ?Purpose:????change?state?of?the?selected?device? ?Input????:????hDevInfo????device?info?set????? ????????????pDeviceInfoData????????selected?device?info? ????????????NewState????one?of?enable/disable? ?Output????:????TRUE?for?success,?FALSE?for?failed? ?****************************************************************************************/? BOOL?ChangeDeviceState(HDEVINFO?hDevInfo,?PSP_DEVINFO_DATA?pDeviceInfoData,?DWORD?NewState)? {? ????SP_PROPCHANGE_PARAMS?PropChangeParams?=?{sizeof(SP_CLASSINSTALL_HEADER)};? ????SP_DEVINSTALL_PARAMS?devParams;?
????if?(!pDeviceInfoData)?{? ????????return?FALSE;? ????}?
????PropChangeParams.ClassInstallHeader.cbSize?=?sizeof(SP_CLASSINSTALL_HEADER);? ????PropChangeParams.ClassInstallHeader.InstallFunction?=?DIF_PROPERTYCHANGE;? ????PropChangeParams.Scope?=?DICS_FLAG_CONFIGSPECIFIC;? ????PropChangeParams.StateChange?=?NewState;?? ????PropChangeParams.HwProfile?=?0;?
????if?(!SetupDiSetClassInstallParams(hDevInfo,pDeviceInfoData,? ??????(SP_CLASSINSTALL_HEADER?*)&PropChangeParams,sizeof(PropChangeParams))? ??????||?!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,pDeviceInfoData))????{? ????????return?FALSE;? ????}? ????reutrn?TRUE;? }?
/*?hDevInfo如何得到***********************************************************/? ????m_hDevInfo?=?SetupDiGetClassDevs(? ??????(LPGUID)?&GUID_DEVCLASS_NET,????/*?GUID_DEVCLASS_NET表示仅列出网络设备?*/? ??????NULL,?? ??????this->m_hWnd,?? ??????DIGCF_PRESENT);? ????if?(INVALID_HANDLE_VALUE?==?m_hDevInfo)?{? ????????return?FALSE;? ????}?
/*?pDeviceInfoData如何得到**************************************************/? ????k?=?0;? ????while?(SetupDiEnumDeviceInfo(m_hDevInfo,?k?,&DeviceInfoData))????{? ????????k++;? ????????if?(CR_SUCCESS?!=?CM_Get_DevNode_Status(&Status,?&Problem,?? ??????????DeviceInfoData.DevInst,0))?{? ????????????continue;? ????????}? ????????if?((Status?&?DN_NO_SHOW_IN_DM))?{? ????????????continue;? ????????}? ????????if?(GetRegistryProperty(m_hDevInfo,? ??????????&DeviceInfoData,? ??????????SPDRP_FRIENDLYNAME,? ??????????&pBuffer,? ??????????&Length))?{? ????????????m_Adapter[adapter_num].index?=?k?-?1;????????/*?当前网卡在设备信息集中的索引?*/? ????????????_tcscpy(m_Adapter[adapter_num].desc,?pBuffer);????/*?当前网卡?*/? ????????????GetRegistryProperty(m_hDevInfo,? ??????????????&DeviceInfoData,? ??????????????SPDRP_DRIVER,? ??????????????&pBuffer,? ??????????????&Length);? ????????????_tcscpy(m_Adapter[adapter_num].driver,?pBuffer);? ????????????adapter_num++;? ????????}? ????}?
(编辑:温州站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|