KeyLife富翁笔记
作者?: TYZhang
标题?: 如何在win98/win2000中禁用启用网卡
关键字: 禁用启用网卡
分类?: 个人专区
密级?: 公开
(评分: , 回复: 1, 阅读: 1521) »»
为响应LeeChange大侠的“授人鱼不如授人渔”号召,在此首度公开我的部分业余写作的原码。
如何在win98/win2000中禁用启用网卡,其实原理很简单,就是枚举出windows中的网络设备,然后设置他的状态,关键代码如下:(其实这个代码也非我原创,是我改写别人的VC代码来得)。
unit ChgEthernet;
interface
uses Classes, Windows, SetupApi, CfgMgr32, Cfg, SysUtils;
type
TNetCardStruct=record
Id
WORD; // 网卡设备号
Name :String[255]; // 网卡名
Disabled:Boolean; // 当前是否禁用
Changed :Boolean; // 是否更改过
end;
PNetCardStruct=^TNetCardStruct;
procedure EnumNetCards(NetDeviceList:TList); //枚举系统中的网卡
function NetCardStateChange(var NetCardPoint
NetCardStruct;Enabled:Boolean):Boolean; //设置网卡的状态
implementation
//读取系统的设备属性
function GetRegistryProperty(DeviceInfoSet: HDEVINFO;var DeviceInfoData: SP_DEVINFO_DATA; AProperty: ULONG;var Buffer: Pchar; var BufSize: ULONG): Boolean;
var Temp,OldSize
WORD;
begin
Result:=False;Temp:=0;OldSize:=BufSize;
while not SetupDiGetDeviceRegistryProperty(DeviceInfoSet,DeviceInfoData, AProperty, Temp, PByte(Buffer), OldSize,BufSize) do
begin
if GetLastError()=ERROR_INSUFFICIENT_BUFFER then
begin
if OldSize>0 then FreeMem(Buffer,OldSize);
GetMem(Buffer,BufSize);OldSize:=BufSize;
end else Exit;
end;
Result:=True;
end;
procedure EnumNetCards(NetDeviceList: TList);
var DevValue:String;
NetCard
NetCardStruct;
Status,Problem
WORD;
Buffer
Char;
BufSize
WORD;
hDevInfo
ointer;
DeviceInfoData:SP_DEVINFO_DATA;
DeviceId
WORD;
begin
if Win32MajorVersion>=5 then BufSize:=0
else
begin
BufSize:=1024;
GetMem(Buffer,BufSize);
end;
hDevInfo:=SetupDiGetClassDevs(nil,nil,0,DIGCF_PRESENT or DIGCF_ALLCLASSES);
if DWORD(hDevInfo)=INVALID_HANDLE_VALUE then Exit;
DeviceInfoData.cbSize:=sizeof(SP_DEVINFO_DATA);
DeviceID:=0;
while SetupDiEnumDeviceInfo(hDevInfo,DeviceId,DeviceInfoData) do
begin
if CM_Get_DevNode_Status(@Status,@Problem,DeviceInfoData.DevInst,0)<>CR_SUCCESS then begin Inc(DeviceID); Continue; end;
if GetRegistryProperty(hDevInfo,DeviceInfoData,SPDRP_CLASS,Buffer,BufSize) then DevValue:=Buffer;
if DevValue='Net' then
begin
if GetRegistryProperty(hDevInfo,DeviceInfoData,SPDRP_ENUMERATOR_NAME,Buffer,BufSize) then DevValue:=Buffer;
if DevValue<>'ROOT' then
begin
new(NetCard);
NetCard.Id:=DeviceId;
NetCard.Name:='<Unknown Device>';
if GetRegistryProperty(hDevInfo,DeviceInfoData,SPDRP_DRIVER,Buffer,BufSize) then
if GetRegistryProperty(hDevInfo,DeviceInfoData,SPDRP_DEVICEDESC,Buffer,BufSize)
then NetCard.Name:=Buffer;
NetCard.Disabled:=((Status and DN_HAS_PROBLEM)<>0)and(CM_PROB_DISABLED=Problem);
NetCard.Changed:=false;
NetDeviceList.Add(NetCard);
end;
end;
Inc(DeviceID);
end;
end;
function NetCardStateChange(var NetCardPoint: PNetCardStruct; Enabled: Boolean): Boolean;
var hDevInfo
ointer;
DeviceInfoData:SP_DEVINFO_DATA;
Status,Problem
WORD;
PropChangeParams:SP_PROPCHANGE_PARAMS;
begin
Result:=False;
hDevInfo:=SetupDiGetClassDevs(nil,nil,0,DIGCF_PRESENT or DIGCF_ALLCLASSES);
if INVALID_HANDLE_VALUE=DWORD(hDevInfo) then Exit;
DeviceInfoData.cbSize:=sizeof(SP_DEVINFO_DATA);
if not SetupDiEnumDeviceInfo(hDevInfo,NetCardPoint.Id,DeviceInfoData) then Exit;
if CM_Get_DevNode_Status(@Status,@Problem,DeviceInfoData.DevInst,0)<>CR_SUCCESS then Exit;
PropChangeParams.ClassInstallHeader.cbSize:=sizeof(SP_CLASSINSTALL_HEADER);
PropChangeParams.ClassInstallHeader.InstallFunction:=DIF_PROPERTYCHANGE;
PropChangeParams.Scope:=DICS_FLAG_GLOBAL;
if Enabled then
begin
if not (((Status and DN_HAS_PROBLEM)<>0)and(CM_PROB_DISABLED=Problem)) then
begin
NetCardPoint.Disabled:=False;
Exit;
end;
PropChangeParams.StateChange:=DICS_ENABLE;
end
else
begin
if ((Status and DN_HAS_PROBLEM)<>0)and(CM_PROB_DISABLED=Problem) then
begin
NetCardPoint.Disabled:=True;
Exit;
end;
if not (((Status and DN_DISABLEABLE)<>0)and(CM_PROB_HARDWARE_DISABLED<>Problem)) then Exit;
PropChangeParams.StateChange:=DICS_DISABLE;
end;
if not SetupDiSetClassInstallParams(hDevInfo,@DeviceInfoData,PSPClassInstallHeader(@PropChangeParams),Sizeof(PropChangeParams)) then Exit;
if not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,@DeviceInfoData) then Exit;
if CM_Get_DevNode_Status(@Status,@Problem,DeviceInfoData.DevInst,0)=CR_SUCCESS then
NetCardPoint.Disabled:=((Status and DN_HAS_PROBLEM)<>0)and(CM_PROB_DISABLED=Problem);
Result:=True;
end;
initialization
LoadSetupApi;
LoadCfgMgr32;
finalization
UnLoadCfgMgr32;
UnloadSetupApi;
end.
注:对应部分98下不支持“即插即用”的网卡,上说代码可能无效。怎样判断98下的网卡是否支持“即插即用”,就是在设备管理器中先禁用网卡,然后启用网卡,如果系统不要求重启就表示支持“即插即用”。