怎么通过程序控制网络连接的禁用和启用

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

怎么通过程序控制网络连接的禁用和启用/*还是帖出来吧,我可是花了三天时间才搞定了.*/#includeSetupAPI.h#includecfgmgr32.h//cfgmgr32.h在MicrosoftWindows2000DDK中.//要用CM_Get_DevNode_Status(...)来查询状态.//---------------------------------------------------------------------------typedefstructNetCardStruct{DWORDId;//网卡设备号AnsiStringName;//网卡名boolDisabled;//当前是否禁用boolChanged;//是否更改过}TNetCardStruct;typedefTNetCardStruct*PNetCardStruct;//---------------------------------------------------------------------------//EnumNetCards枚举出网卡//---------------------------------------------------------------------------void__fastcallEnumNetCards(TList*NetDeviceList){AnsiStringDevValue;PNetCardStructNetCard;DWORDStatus,Problem;LPTSTRBuffer=NULL;DWORDBufSize=0;HDEVINFOhDevInfo=0;if(INVALID_HANDLE_VALUE==(hDevInfo=SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES)))return;SP_DEVINFO_DATADeviceInfoData={sizeof(SP_DEVINFO_DATA)};HKEYhKeyClass;charDeviceName[200];for(DWORDDeviceId=0;SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData);DeviceId++){if(CM_Get_DevNode_Status(&Status,&Problem,DeviceInfoData.DevInst,0)!=CR_SUCCESS)continue;DevValue.SetLength(0);if(GetRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_CLASS,&Buffer,(PULONG)&BufSize))DevValue=Buffer;if(DevValue==Net){DevValue.SetLength(0);if(GetRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_ENUMERATOR_NAME,&Buffer,(PULONG)&BufSize))DevValue=Buffer;if(DevValue!=ROOT){NetCard=newTNetCardStruct;NetCard-Id=DeviceId;NetCard-Name=UnknownDevice;if(GetRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_DRIVER,&Buffer,(PULONG)&BufSize))if(GetRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_DEVICEDESC,&Buffer,(PULONG)&BufSize))NetCard-Name=Buffer;NetCard-Disabled=(Status&DN_HAS_PROBLEM)&&(CM_PROB_DISABLED==Problem);NetCard-Changed=false;NetDeviceList-Add(NetCard);}}}}//---------------------------------------------------------------------------bool__fastcallGetRegistryProperty(HDEVINFODeviceInfoSet,PSP_DEVINFO_DATADeviceInfoData,ULONGProperty,PVOIDBuffer,PULONGLength){while(!SetupDiGetDeviceRegistryProperty(DeviceInfoSet,DeviceInfoData,Property,NULL,(BYTE*)*(TCHAR**)Buffer,*Length,Length)){if(GetLastError()==ERROR_INSUFFICIENT_BUFFER){if(*(LPTSTR*)Buffer)LocalFree(*(LPTSTR*)Buffer);*(LPTSTR*)Buffer=(PCHAR)LocalAlloc(LPTR,*Length);}elsereturnfalse;}return(*(LPTSTR*)Buffer)[0];}//---------------------------------------------------------------------------//NetCardStateChange网卡的启用与禁用//NetCardPoint是PNetCardStruct的指针.//Enabledtrue=启用false=禁用//---------------------------------------------------------------------------bool__fastcallNetCardStateChange(void*NetCardPoint,boolEnabled){PNetCardStructNetCard=(PNetCardStruct)NetCardPoint;DWORDDeviceId=NetCard-Id;HDEVINFOhDevInfo=0;if(INVALID_HANDLE_VALUE==(hDevInfo=SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES)))returnfalse;SP_DEVINFO_DATADeviceInfoData={sizeof(SP_DEVINFO_DATA)};DWORDStatus,Problem;if(!SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData))returnfalse;if(CM_Get_DevNode_Status(&Status,&Problem,DeviceInfoData.DevInst,0)!=CR_SUCCESS)returnfalse;SP_PROPCHANGE_PARAMSPropChangeParams={sizeof(SP_CLASSINSTALL_HEADER)};PropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE;PropChangeParams.Scope=DICS_FLAG_GLOBAL;if(Enabled){if(!((Status&DN_HAS_PROBLEM)&&(CM_PROB_DISABLED==Problem))){NetCard-Disabled=false;returnfalse;}PropChangeParams.StateChange=DICS_ENABLE;}else{if((Status&DN_HAS_PROBLEM)&&(CM_PROB_DISABLED==Problem)){NetCard-Disabled=true;returnfalse;}if(!((Status&DN_DISABLEABLE)&&(CM_PROB_HARDWARE_DISABLED!=Problem)))returnfalse;PropChangeParams.StateChange=DICS_DISABLE;}if(!SetupDiSetClassInstallParams(hDevInfo,&DeviceInfoData,(SP_CLASSINSTALL_HEADER*)&PropChangeParams,sizeof(PropChangeParams)))returnfalse;if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&DeviceInfoData))returnfalse;if(CM_Get_DevNode_Status(&Status,&Problem,DeviceInfoData.DevInst,0)==CR_SUCCESS)NetCard-Disabled=(Status&DN_HAS_PROBLEM)&&(CM_PROB_DISABLED==Problem);returntrue;}///////////////////////////////////////////////////////////////////////////////////////////试试这个://NetCard.h#includestdafx.h#includetchar.h#includewindows.h#includesetupapi.h#defineUnknownDeviceTEXT(UnknownDevice)BOOLStateChange(DWORDNewState,DWORDSelectedItem,HDEVINFOhDevInfo);BOOLIsClassNet(GUID*ClassGuid);///////////////////////NetCard.cpp#includestdafx.h#includeNetCard.hBOOLStateChange(DWORDNewState,DWORDSelectedItem,HDEVINFOhDevInfo){SP_PROPCHANGE_PARAMSPropChangeParams={sizeof(SP_CLASSINSTALL_HEADER)};SP_DEVINFO_DATADeviceInfoData={sizeof(SP_DEVINFO_DATA)};HCURSORhCursor;////Thismaytakeawhile:^(//hCursor=SetCursor(LoadCursor(NULL,IDC_WAIT));////GetahandletotheSelectedItem.//if(!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData)){printf(EnumDeviceInfo);returnFALSE;}////SetthePropChangeParamsstructure.//PropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE;PropChangeParams.Scope=DICS_FLAG_GLOBAL;PropChangeParams.StateChange=NewState;if(!Setu

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功