谁有在2000,XP以上系统下,能够实现系统断开网络和连接上网张的代码(INTERNET和局域网都要求可以)(高分求救) ( 积分: 300 )

  • 主题发起人 主题发起人 _Murray
  • 开始时间 开始时间
M

_Murray

Unregistered / Unconfirmed
GUEST, unregistred user!
最好连网断网速度快,最好是DELPHI的代码,C/C++也可以。(不要给我NetFilter的代码,不稳定,速度很慢),先谢了
 
自已顶,如果感觉分不够的话,可以再加300分,比较急,大家帮帮忙吧
 
給你一段代碼,但不是我寫得。

unit DeviceForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, Common, RegStr;

const
DEV_CLASS_NAME = 'Net';
UNKNOWN_DEVICE = '<未知設備>';

type
TDevForm = class(TForm)
lbDev: TLabel;
btApply: TButton;
btExit: TButton;
clbDevList: TCheckListBox;
btRefresh: TButton;
procedure FormCreate(Sender: TObject);
procedure btExitClick(Sender: TObject);
procedure btApplyClick(Sender: TObject);
procedure btRefreshClick(Sender: TObject);
private
{ Private declarations }
DevState: Array of Boolean;
procedure RefreshDevState;

procedure EnumNetDevice;
function IsClassHidden(const ClassGUID: PGUID): Boolean;
function ConstructDeviceName(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; Buffer: PAnsiChar; Length: ULONG): Boolean;
function GetRegistryProperty(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; AProperty: ULONG; Buffer: PAnsiChar;
Length: ULONG): Boolean;
function IsDevDisable(DevIndex: DWORD; hDevInfo: HDEVINFO): Boolean;
function ChangeDevState(DevIndex, NewState: DWORD): BOOL;
public
{ Public declarations }
end;

var
DevForm: TDevForm;

implementation

{$R *.dfm}

procedure TDevForm.EnumNetDevice;
var
DeviceInfoSet: HDEVINFO;
DeviceInfoData: SP_DEVINFO_DATA;
i: Integer;
Status, Problem: DWORD;
ClassName: PChar;
ClassSize, ReqClassSize: DWORD;
DeviceName: PChar;
begin
clbDevList.Clear;

DeviceInfoSet:=SetupDiGetClassDevs(Nil,Nil,0,DIGCF_ALLCLASSES or DIGCF_PRESENT);
if DeviceInfoSet = Pointer(INVALID_HANDLE_VALUE) then
Exit;

ClassSize:=255;
GetMem(ClassName,256);
try
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA);

i:=0;
while SetupDiEnumDeviceInfo(DeviceInfoSet,i,@DeviceInfoData) do
begin
Inc(i);

if not SetupDiClassNameFromGuid(@DeviceInfoData.ClassGuid,ClassName,ClassSize,
@ReqClassSize) then
begin
if ReqClassSize>ClassSize then
begin
FreeMem(ClassName);
ClassSize:=ReqClassSize;
GetMem(ClassName,ClassSize+1);
if not SetupDiClassNameFromGuid(@DeviceInfoData.ClassGuid,ClassName,ClassSize,
@ReqClassSize) then
Exit;
end
else
Exit;
end;

if not SameText(ClassName,DEV_CLASS_NAME) then
Continue;

if CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst,0)
<> CR_SUCCESS then
Exit;

if ((Status and DN_NO_SHOW_IN_DM)<>0) or
IsClassHidden(@DeviceInfoData.ClassGuid) then
Continue;

GetMem(DeviceName,256);
ZeroMemory(DeviceName,256);
ConstructDeviceName(DeviceInfoSet,@DeviceInfoData,DeviceName,255);
clbDevList.Items.AddObject(StrPas(DeviceName),TObject(i-1));
clbDevList.Checked[clbDevList.Count-1]:=IsDevDisable(i-1,DeviceInfoSet);
FreeMem(DeviceName);
end;
finally
FreeMem(ClassName);
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
end;
end;

function TDevForm.ConstructDeviceName(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; Buffer: PAnsiChar;
Length: ULONG): Boolean;
begin
Result:=True;

if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_FRIENDLYNAME,
Buffer,Length) then
begin
if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_DEVICEDESC,
Buffer,Length) then
begin
if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_CLASS,
Buffer,Length) then
begin
if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_CLASSGUID,
Buffer,Length) then
begin
StrCopy(Buffer,UNKNOWN_DEVICE);
end
else
Result:=False;
end
end
end;
end;

function TDevForm.GetRegistryProperty(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; AProperty: ULONG; Buffer: PAnsiChar;
Length: ULONG): Boolean;
var
ReqLen: DWORD;
begin
Result:=False;

while not SetupDiGetDeviceRegistryProperty(DeviceInfoSet,DeviceInfoData,
AProperty,Nil,Buffer,Length,@ReqLen) do
begin
if GetLastError() = ERROR_INVALID_DATA then
break
else if GetLastError() = ERROR_INSUFFICIENT_BUFFER then
begin
if Assigned(Buffer) then
FreeMem(Buffer);
Length:=ReqLen;
GetMem(Buffer,Length+1);
end
else
Exit;
end;

Result:=Buffer^<>#0;
end;

function TDevForm.IsClassHidden(const ClassGUID: PGUID): Boolean;
var
hKeyClass: HKEY;
begin
Result:=False;

hKeyClass := SetupDiOpenClassRegKey(ClassGuid,KEY_READ);
if hKeyClass<>0 then
begin
Result:= RegQueryValueEx(hKeyClass,REGSTR_VAL_NODISPLAYCLASS,Nil,Nil,NIl,Nil) = ERROR_SUCCESS;
RegCloseKey(hKeyClass);
end;
end;

function TDevForm.IsDevDisable(DevIndex: DWORD;
hDevInfo: HDEVINFO): Boolean;
var
DeviceInfoData: SP_DEVINFO_DATA;
Status, Problem: DWORD;
begin
Result:=False;
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA);

if not SetupDiEnumDeviceInfo(hDevInfo,DevIndex,@DeviceInfoData) then
Exit;

if CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS then
Exit;

Result:=((Status and DN_DISABLEABLE)<>0) and (CM_PROB_HARDWARE_DISABLED <> Problem);
end;

function TDevForm.ChangeDevState(DevIndex, NewState: DWORD): BOOL;
var
DeviceInfoSet: HDEVINFO;
DeviceInfoData: SP_DEVINFO_DATA;
PropChangeParams: SP_PROPCHANGE_PARAMS;
Cursor: HCURSOR;
begin
Result:=False;

DeviceInfoSet:=SetupDiGetClassDevs(Nil,Nil,0,DIGCF_ALLCLASSES or DIGCF_PRESENT);
if DeviceInfoSet = Pointer(INVALID_HANDLE_VALUE) then
Exit;

try
PropChangeParams.ClassInstallHeader.cbSize:=SizeOf(SP_CLASSINSTALL_HEADER);
DeviceInfoData.cbSize:=SizeOf(SP_DEVINFO_DATA);

Cursor := SetCursor(LoadCursor(0, IDC_WAIT));

if not SetupDiEnumDeviceInfo(DeviceInfoSet,DevIndex,@DeviceInfoData) then
Exit;

PropChangeParams.ClassInstallHeader.InstallFunction := DIF_PROPERTYCHANGE;
PropChangeParams.Scope := DICS_FLAG_GLOBAL;
PropChangeParams.StateChange := NewState;

if not SetupDiSetClassInstallParams(DeviceInfoSet,@DeviceInfoData,
@PropChangeParams,Sizeof(PropChangeParams)) then
Exit;

if not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,DeviceInfoSet,
@DeviceInfoData) then
Exit;

SetCursor(Cursor);
Result:=True;
finally
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
end;
end;

procedure TDevForm.FormCreate(Sender: TObject);
begin
btRefresh.Click;
end;

procedure TDevForm.btExitClick(Sender: TObject);
begin
Close;
end;

procedure TDevForm.btApplyClick(Sender: TObject);
var
i: Integer;
begin
for i:=0 to clbDevList.Count-1 do
begin
if clbDevList.Checked<>DevState then
begin
if clbDevList.Checked then
ChangeDevState(Cardinal(clbDevList.Items.Objects),DICS_ENABLE)
else
ChangeDevState(Cardinal(clbDevList.Items.Objects),DICS_DISABLE)
end;
end;
RefreshDevState;
end;

procedure TDevForm.RefreshDevState;
var
i: Integer;
begin
SetLength(DevState,clbDevList.Count);
for i:=0 to clbDevList.Count-1 do
DevState:=clbDevList.Checked;
end;

procedure TDevForm.btRefreshClick(Sender: TObject);
begin
EnumNetDevice;
RefreshDevState;
end;

end.

--------------------------------------------------------------
unit Common;

interface

uses
Windows;

type
HDEVINFO = Pointer;
ULONG_PTR = DWORD;
DEVINST = DWORD;
DI_FUNCTION = UINT;

_SP_DEVINFO_DATA = packed record
cbSize: DWORD;
ClassGuid: TGUID;
DevInst: DWORD;
Reserved: ULONG_PTR;
end;

SP_DEVINFO_DATA = _SP_DEVINFO_DATA;
PSP_DEVINFO_DATA = ^_SP_DEVINFO_DATA;

_SP_CLASSINSTALL_HEADER = packed record
cbSize: DWORD;
InstallFunction: DI_FUNCTION;
end;
SP_CLASSINSTALL_HEADER = _SP_CLASSINSTALL_HEADER;
PSP_CLASSINSTALL_HEADER = ^_SP_CLASSINSTALL_HEADER;

_SP_PROPCHANGE_PARAMS = packed record
ClassInstallHeader: SP_CLASSINSTALL_HEADER;
StateChange: DWORD;
Scope: DWORD;
HwProfile: DWORD;
end;
SP_PROPCHANGE_PARAMS = _SP_PROPCHANGE_PARAMS;
PSP_PROPCHANGE_PARAMS = ^SP_PROPCHANGE_PARAMS;


//
// Flags controlling what is included in the device information set built
// by SetupDiGetClassDevs
//
const
DIGCF_DEFAULT = $00000001; // only valid with DIGCF_DEVICEINTERFACE
{$EXTERNALSYM DIGCF_DEFAULT}
DIGCF_PRESENT = $00000002;
{$EXTERNALSYM DIGCF_PRESENT}
DIGCF_ALLCLASSES = $00000004;
{$EXTERNALSYM DIGCF_ALLCLASSES}
DIGCF_PROFILE = $00000008;
{$EXTERNALSYM DIGCF_PROFILE}
DIGCF_DEVICEINTERFACE = $00000010;
{$EXTERNALSYM DIGCF_DEVICEINTERFACE}

function SetupDiGetClassDevs(const ClassGuid: PGUID; Enumerator: PAnsiChar;
hwndParent: THandle; Flags: DWORD): HDEVINFO;
stdcall external 'setupapi.dll' name 'SetupDiGetClassDevsA';

function SetupDiDestroyDeviceInfoList(DeviceInfoSet: HDEVINFO): BOOL;
stdcall external 'setupapi.dll' name 'SetupDiDestroyDeviceInfoList';

function SetupDiEnumDeviceInfo(DeviceInfoSet: HDEVINFO; MemberIndex: DWORD;
DeviceInfoData: PSP_DEVINFO_DATA): BOOL;
stdcall external 'setupapi.dll' name 'SetupDiEnumDeviceInfo';

function SetupDiOpenClassRegKey(const ClassGuid: PGUID;
samDesired: REGSAM): HKEY;
stdcall external 'setupapi.dll' name 'SetupDiOpenClassRegKey';

//
// Device registry property codes
// (Codes marked as read-only (R) may only be used for
// SetupDiGetDeviceRegistryProperty)
//
// These values should cover the same set of registry properties
// as defined by the CM_DRP codes in cfgmgr32.h.
//
const
SPDRP_DEVICEDESC = $00000000; // DeviceDesc (R/W)
{$EXTERNALSYM SPDRP_DEVICEDESC}
SPDRP_HARDWAREID = $00000001; // HardwareID (R/W)
{$EXTERNALSYM SPDRP_HARDWAREID}
SPDRP_COMPATIBLEIDS = $00000002; // CompatibleIDs (R/W)
{$EXTERNALSYM SPDRP_COMPATIBLEIDS}
SPDRP_UNUSED0 = $00000003; // unused
{$EXTERNALSYM SPDRP_UNUSED0}
SPDRP_SERVICE = $00000004; // Service (R/W)
{$EXTERNALSYM SPDRP_SERVICE}
SPDRP_UNUSED1 = $00000005; // unused
{$EXTERNALSYM SPDRP_UNUSED1}
SPDRP_UNUSED2 = $00000006; // unused
{$EXTERNALSYM SPDRP_UNUSED2}
SPDRP_CLASS = $00000007; // Class (R--tied to ClassGUID)
{$EXTERNALSYM SPDRP_CLASS}
SPDRP_CLASSGUID = $00000008; // ClassGUID (R/W)
{$EXTERNALSYM SPDRP_CLASSGUID}
SPDRP_DRIVER = $00000009; // Driver (R/W)
{$EXTERNALSYM SPDRP_DRIVER}
SPDRP_CONFIGFLAGS = $0000000A; // ConfigFlags (R/W)
{$EXTERNALSYM SPDRP_CONFIGFLAGS}
SPDRP_MFG = $0000000B; // Mfg (R/W)
{$EXTERNALSYM SPDRP_MFG}
SPDRP_FRIENDLYNAME = $0000000C; // FriendlyName (R/W)
{$EXTERNALSYM SPDRP_FRIENDLYNAME}
SPDRP_LOCATION_INFORMATION = $0000000D; // LocationInformation (R/W)
{$EXTERNALSYM SPDRP_LOCATION_INFORMATION}
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = $0000000E; // PhysicalDeviceObjectName (R)
{$EXTERNALSYM SPDRP_PHYSICAL_DEVICE_OBJECT_NAME}
SPDRP_CAPABILITIES = $0000000F; // Capabilities (R)
{$EXTERNALSYM SPDRP_CAPABILITIES}
SPDRP_UI_NUMBER = $00000010; // UiNumber (R)
{$EXTERNALSYM SPDRP_UI_NUMBER}
SPDRP_UPPERFILTERS = $00000011; // UpperFilters (R/W)
{$EXTERNALSYM SPDRP_UPPERFILTERS}
SPDRP_LOWERFILTERS = $00000012; // LowerFilters (R/W)
{$EXTERNALSYM SPDRP_LOWERFILTERS}
SPDRP_BUSTYPEGUID = $00000013; // BusTypeGUID (R)
{$EXTERNALSYM SPDRP_BUSTYPEGUID}
SPDRP_LEGACYBUSTYPE = $00000014; // LegacyBusType (R)
{$EXTERNALSYM SPDRP_LEGACYBUSTYPE}
SPDRP_BUSNUMBER = $00000015; // BusNumber (R)
{$EXTERNALSYM SPDRP_BUSNUMBER}
SPDRP_ENUMERATOR_NAME = $00000016; // Enumerator Name (R)
{$EXTERNALSYM SPDRP_ENUMERATOR_NAME}
SPDRP_SECURITY = $00000017; // Security (R/W, binary form)
{$EXTERNALSYM SPDRP_SECURITY}
SPDRP_SECURITY_SDS = $00000018; // Security (W, SDS form)
{$EXTERNALSYM SPDRP_SECURITY_SDS}
SPDRP_DEVTYPE = $00000019; // Device Type (R/W)
{$EXTERNALSYM SPDRP_DEVTYPE}
SPDRP_EXCLUSIVE = $0000001A; // Device is exclusive-access (R/W)
{$EXTERNALSYM SPDRP_EXCLUSIVE}
SPDRP_CHARACTERISTICS = $0000001B; // Device Characteristics (R/W)
{$EXTERNALSYM SPDRP_CHARACTERISTICS}
SPDRP_ADDRESS = $0000001C; // Device Address (R)
{$EXTERNALSYM SPDRP_ADDRESS}
SPDRP_UI_NUMBER_DESC_FORMAT = $0000001E; // UiNumberDescFormat (R/W)
{$EXTERNALSYM SPDRP_UI_NUMBER_DESC_FORMAT}
SPDRP_MAXIMUM_PROPERTY = $0000001F; // Upper bound on ordinals
{$EXTERNALSYM SPDRP_MAXIMUM_PROPERTY}

function SetupDiGetDeviceRegistryProperty(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; AProperty: DWORD;
PropertyRegDataType: PDWORD; PropertyBuffer: PAnsiChar;
PropertyBufferSize: DWORD; RequiredSize: PDWORD): BOOL;
stdcall external 'setupapi.dll' name 'SetupDiGetDeviceRegistryPropertyA';


//
// Class installer function codes
//
const
DIF_SELECTDEVICE = $00000001;
{$EXTERNALSYM DIF_SELECTDEVICE}
DIF_INSTALLDEVICE = $00000002;
{$EXTERNALSYM DIF_INSTALLDEVICE}
DIF_ASSIGNRESOURCES = $00000003;
{$EXTERNALSYM DIF_ASSIGNRESOURCES}
DIF_PROPERTIES = $00000004;
{$EXTERNALSYM DIF_PROPERTIES}
DIF_REMOVE = $00000005;
{$EXTERNALSYM DIF_REMOVE}
DIF_FIRSTTIMESETUP = $00000006;
{$EXTERNALSYM DIF_FIRSTTIMESETUP}
DIF_FOUNDDEVICE = $00000007;
{$EXTERNALSYM DIF_FOUNDDEVICE}
DIF_SELECTCLASSDRIVERS = $00000008;
{$EXTERNALSYM DIF_SELECTCLASSDRIVERS}
DIF_VALIDATECLASSDRIVERS = $00000009;
{$EXTERNALSYM DIF_VALIDATECLASSDRIVERS}
DIF_INSTALLCLASSDRIVERS = $0000000A;
{$EXTERNALSYM DIF_INSTALLCLASSDRIVERS}
DIF_CALCDISKSPACE = $0000000B;
{$EXTERNALSYM DIF_CALCDISKSPACE}
DIF_DESTROYPRIVATEDATA = $0000000C;
{$EXTERNALSYM DIF_DESTROYPRIVATEDATA}
DIF_VALIDATEDRIVER = $0000000D;
{$EXTERNALSYM DIF_VALIDATEDRIVER}
DIF_MOVEDEVICE = $0000000E;
{$EXTERNALSYM DIF_MOVEDEVICE}
DIF_DETECT = $0000000F;
{$EXTERNALSYM DIF_DETECT}
DIF_INSTALLWIZARD = $00000010;
{$EXTERNALSYM DIF_INSTALLWIZARD}
DIF_DESTROYWIZARDDATA = $00000011;
{$EXTERNALSYM DIF_DESTROYWIZARDDATA}
DIF_PROPERTYCHANGE = $00000012;
{$EXTERNALSYM DIF_PROPERTYCHANGE}
DIF_ENABLECLASS = $00000013;
{$EXTERNALSYM DIF_ENABLECLASS}
DIF_DETECTVERIFY = $00000014;
{$EXTERNALSYM DIF_DETECTVERIFY}
DIF_INSTALLDEVICEFILES = $00000015;
{$EXTERNALSYM DIF_INSTALLDEVICEFILES}
DIF_UNREMOVE = $00000016;
{$EXTERNALSYM DIF_UNREMOVE}
DIF_SELECTBESTCOMPATDRV = $00000017;
{$EXTERNALSYM DIF_SELECTBESTCOMPATDRV}
DIF_ALLOW_INSTALL = $00000018;
{$EXTERNALSYM DIF_ALLOW_INSTALL}
DIF_REGISTERDEVICE = $00000019;
{$EXTERNALSYM DIF_REGISTERDEVICE}
DIF_NEWDEVICEWIZARD_PRESELECT = $0000001A;
{$EXTERNALSYM DIF_NEWDEVICEWIZARD_PRESELECT}
DIF_NEWDEVICEWIZARD_SELECT = $0000001B;
{$EXTERNALSYM DIF_NEWDEVICEWIZARD_SELECT}
DIF_NEWDEVICEWIZARD_PREANALYZE = $0000001C;
{$EXTERNALSYM DIF_NEWDEVICEWIZARD_PREANALYZE}
DIF_NEWDEVICEWIZARD_POSTANALYZE = $0000001D;
{$EXTERNALSYM DIF_NEWDEVICEWIZARD_POSTANALYZE}
DIF_NEWDEVICEWIZARD_FINISHINSTALL = $0000001E;
{$EXTERNALSYM DIF_NEWDEVICEWIZARD_FINISHINSTALL}
DIF_UNUSED1 = $0000001F;
{$EXTERNALSYM DIF_UNUSED1}
DIF_INSTALLINTERFACES = $00000020;
{$EXTERNALSYM DIF_INSTALLINTERFACES}
DIF_DETECTCANCEL = $00000021;
{$EXTERNALSYM DIF_DETECTCANCEL}
DIF_REGISTER_COINSTALLERS = $00000022;
{$EXTERNALSYM DIF_REGISTER_COINSTALLERS}
DIF_ADDPROPERTYPAGE_ADVANCED = $00000023;
{$EXTERNALSYM DIF_ADDPROPERTYPAGE_ADVANCED}
DIF_ADDPROPERTYPAGE_BASIC = $00000024;
{$EXTERNALSYM DIF_ADDPROPERTYPAGE_BASIC}
DIF_RESERVED1 = $00000025;
{$EXTERNALSYM DIF_RESERVED1}
DIF_TROUBLESHOOTER = $00000026;
{$EXTERNALSYM DIF_TROUBLESHOOTER}
DIF_POWERMESSAGEWAKE = $00000027;
{$EXTERNALSYM DIF_POWERMESSAGEWAKE}

//
// Values indicating a change in a device's state
//
const
DICS_ENABLE = $00000001;
{$EXTERNALSYM DICS_ENABLE}
DICS_DISABLE = $00000002;
{$EXTERNALSYM DICS_DISABLE}
DICS_PROPCHANGE = $00000003;
{$EXTERNALSYM DICS_PROPCHANGE}
DICS_START = $00000004;
{$EXTERNALSYM DICS_START}
DICS_STOP = $00000005;
{$EXTERNALSYM DICS_STOP}

//
// Values specifying the scope of a device property change
//
DICS_FLAG_GLOBAL = $00000001; // make change in all hardware profiles
{$EXTERNALSYM DICS_FLAG_GLOBAL}
DICS_FLAG_CONFIGSPECIFIC = $00000002; // make change in specified profile only
{$EXTERNALSYM DICS_FLAG_CONFIGSPECIFIC}
DICS_FLAG_CONFIGGENERAL = $00000004; // 1 or more hardware profile-specific
{$EXTERNALSYM DICS_FLAG_CONFIGGENERAL} // changes to follow.

function SetupDiCallClassInstaller(InstallFunction: DI_FUNCTION;
DeviceInfoSet: HDEVINFO; DeviceInfoData: PSP_DEVINFO_DATA): BOOL;
stdcall external 'setupapi.dll' name 'SetupDiCallClassInstaller';

function SetupDiSetClassInstallParams(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; ClassInstallParams: PSP_CLASSINSTALL_HEADER;
ClassInstallParamsSize: DWORD): BOOL;
stdcall external 'setupapi.dll' name 'SetupDiSetClassInstallParamsA';

function SetupDiClassNameFromGuid(const ClassGuid: PGUID; ClassName: PAnsiChar;
ClassNameSize: DWORD; RequiredSize: PDWORD): LongBool;
stdcall external 'setupapi.dll' name 'SetupDiClassNameFromGuidA';

const
CR_SUCCESS = $00000000;
CR_DEFAULT = $00000001;
CR_OUT_OF_MEMORY = $00000002;
CR_INVALID_POINTER = $00000003;
CR_INVALID_FLAG = $00000004;
CR_INVALID_DEVNODE = $00000005;
CR_INVALID_DEVINST = CR_INVALID_DEVNODE;
CR_INVALID_RES_DES = $00000006;
CR_INVALID_LOG_CONF = $00000007;
CR_INVALID_ARBITRATOR = $00000008;
CR_INVALID_NODELIST = $00000009;
CR_DEVNODE_HAS_REQS = $0000000A;
CR_DEVINST_HAS_REQS = CR_DEVNODE_HAS_REQS;
CR_INVALID_RESOURCEID = $0000000B;
CR_DLVXD_NOT_FOUND = $0000000C; // WIN 95 ONLY
CR_NO_SUCH_DEVNODE = $0000000D;
CR_NO_SUCH_DEVINST = CR_NO_SUCH_DEVNODE;
CR_NO_MORE_LOG_CONF = $0000000E;
CR_NO_MORE_RES_DES = $0000000F;
CR_ALREADY_SUCH_DEVNODE = $00000010;
CR_ALREADY_SUCH_DEVINST = CR_ALREADY_SUCH_DEVNODE;
CR_INVALID_RANGE_LIST = $00000011;
CR_INVALID_RANGE = $00000012;
CR_FAILURE = $00000013;
CR_NO_SUCH_LOGICAL_DEV = $00000014;
CR_CREATE_BLOCKED = $00000015;
CR_NOT_SYSTEM_VM = $00000016; // WIN 95 ONLY
CR_REMOVE_VETOED = $00000017;
CR_APM_VETOED = $00000018;
CR_INVALID_LOAD_TYPE = $00000019;
CR_BUFFER_SMALL = $0000001A;
CR_NO_ARBITRATOR = $0000001B;
CR_NO_REGISTRY_HANDLE = $0000001C;
CR_REGISTRY_ERROR = $0000001D;
CR_INVALID_DEVICE_ID = $0000001E;
CR_INVALID_DATA = $0000001F;
CR_INVALID_API = $00000020;
CR_DEVLOADER_NOT_READY = $00000021;
CR_NEED_RESTART = $00000022;
CR_NO_MORE_HW_PROFILES = $00000023;
CR_DEVICE_NOT_THERE = $00000024;
CR_NO_SUCH_VALUE = $00000025;
CR_WRONG_TYPE = $00000026;
CR_INVALID_PRIORITY = $00000027;
CR_NOT_DISABLEABLE = $00000028;
CR_FREE_RESOURCES = $00000029;
CR_QUERY_VETOED = $0000002A;
CR_CANT_SHARE_IRQ = $0000002B;
CR_NO_DEPENDENT = $0000002C;
CR_SAME_RESOURCES = $0000002D;
CR_NO_SUCH_REGISTRY_KEY = $0000002E;
CR_INVALID_MACHINENAME = $0000002F; // NT ONLY
CR_REMOTE_COMM_FAILURE = $00000030; // NT ONLY
CR_MACHINE_UNAVAILABLE = $00000031; // NT ONLY
CR_NO_CM_SERVICES = $00000032; // NT ONLY
CR_ACCESS_DENIED = $00000033; // NT ONLY
CR_CALL_NOT_IMPLEMENTED = $00000034;
CR_INVALID_PROPERTY = $00000035;
CR_DEVICE_INTERFACE_ACTIVE = $00000036;
CR_NO_SUCH_DEVICE_INTERFACE = $00000037;
CR_INVALID_REFERENCE_STRING = $00000038;
CR_INVALID_CONFLICT_LIST = $00000039;
CR_INVALID_INDEX = $0000003A;
CR_INVALID_STRUCTURE_SIZE = $0000003B;
NUM_CR_RESULTS = $0000003C;

CM_PROB_NOT_CONFIGURED = $00000001; // no config for device
CM_PROB_DEVLOADER_FAILED = $00000002; // service load failed
CM_PROB_OUT_OF_MEMORY = $00000003; // out of memory
CM_PROB_ENTRY_IS_WRONG_TYPE = $00000004; //
CM_PROB_LACKED_ARBITRATOR = $00000005; //
CM_PROB_BOOT_CONFIG_CONFLICT = $00000006; // boot config conflict
CM_PROB_FAILED_FILTER = $00000007; //
CM_PROB_DEVLOADER_NOT_FOUND = $00000008; // Devloader not found
CM_PROB_INVALID_DATA = $00000009; //
CM_PROB_FAILED_START = $0000000A; //
CM_PROB_LIAR = $0000000B; //
CM_PROB_NORMAL_CONFLICT = $0000000C; // config conflict
CM_PROB_NOT_VERIFIED = $0000000D; //
CM_PROB_NEED_RESTART = $0000000E; // requires restart
CM_PROB_REENUMERATION = $0000000F; //
CM_PROB_PARTIAL_LOG_CONF = $00000010; //
CM_PROB_UNKNOWN_RESOURCE = $00000011; // unknown res type
CM_PROB_REINSTALL = $00000012; //
CM_PROB_REGISTRY = $00000013; //
CM_PROB_VXDLDR = $00000014; // WINDOWS 95 ONLY
CM_PROB_WILL_BE_REMOVED = $00000015; // devinst will remove
CM_PROB_DISABLED = $00000016; // devinst is disabled
CM_PROB_DEVLOADER_NOT_READY = $00000017; // Devloader not ready
CM_PROB_DEVICE_NOT_THERE = $00000018; // device doesn't exist
CM_PROB_MOVED = $00000019; //
CM_PROB_TOO_EARLY = $0000001A; //
CM_PROB_NO_VALID_LOG_CONF = $0000001B; // no valid log config
CM_PROB_FAILED_INSTALL = $0000001C; // install failed
CM_PROB_HARDWARE_DISABLED = $0000001D; // device disabled
CM_PROB_CANT_SHARE_IRQ = $0000001E; // can't share IRQ
CM_PROB_FAILED_ADD = $0000001F; // driver failed add
CM_PROB_DISABLED_SERVICE = $00000020; // service's Start = 4
CM_PROB_TRANSLATION_FAILED = $00000021; // resource translation failed
CM_PROB_NO_SOFTCONFIG = $00000022; // no soft config
CM_PROB_BIOS_TABLE = $00000023; // device missing in BIOS table
CM_PROB_IRQ_TRANSLATION_FAILED= $00000024; // IRQ translator failed
NUM_CM_PROB = $00000025;

DN_ROOT_ENUMERATED = $00000001; // Was enumerated by ROOT
DN_DRIVER_LOADED = $00000002; // Has Register_Device_Driver
DN_ENUM_LOADED = $00000004; // Has Register_Enumerator
DN_STARTED = $00000008; // Is currently configured
DN_MANUAL = $00000010; // Manually installed
DN_NEED_TO_ENUM = $00000020; // May need reenumeration
DN_NOT_FIRST_TIME = $00000040; // Has received a config
DN_HARDWARE_ENUM = $00000080; // Enum generates hardware ID
DN_LIAR = $00000100; // Lied about can reconfig once
DN_HAS_MARK = $00000200; // Not CM_Create_DevInst lately
DN_HAS_PROBLEM = $00000400; // Need device installer
DN_FILTERED = $00000800; // Is filtered
DN_MOVED = $00001000; // Has been moved
DN_DISABLEABLE = $00002000; // Can be rebalanced
DN_REMOVABLE = $00004000; // Can be removed
DN_PRIVATE_PROBLEM = $00008000; // Has a private problem
DN_MF_PARENT = $00010000; // Multi function parent
DN_MF_CHILD = $00020000; // Multi function child
DN_WILL_BE_REMOVED = $00040000; // DevInst is being removed

{****************************************************************************
*
* DEVNODE STATUS
*
****************************************************************************
*
* These are the bits in the devnode's status that someone can query
* with a CM_Get_DevNode_Status. The A/S column tells wheter the flag
* can be change asynchronously or not.
*
***************************************************************************}
// 4.0 OPK2 Flags
DN_NOT_FIRST_TIMEE = $00080000; // S: Has received a config enumerate
DN_STOP_FREE_RES = $00100000; // S: When child is stopped, free resources
DN_REBAL_CANDIDATE = $00200000; // S: Don't skip during rebalance
DN_BAD_PARTIAL = $00400000; // S: This devnode's log_confs do not have same resources
DN_NT_ENUMERATOR = $00800000; // S: This devnode's is an NT enumerator
DN_NT_DRIVER = $01000000; // S: This devnode's is an NT driver

// 4.1 Flags
DN_NEEDS_LOCKING = $02000000; // S: Devnode need lock resume processing
DN_ARM_WAKEUP = $04000000; // S: Devnode can be the wakeup device
DN_APM_ENUMERATOR = $08000000; // S: APM aware enumerator
DN_APM_DRIVER = $10000000; // S: APM aware driver
DN_SILENT_INSTALL = $20000000; // S: Silent install
DN_NO_SHOW_IN_DM = $40000000; // S: No show in device manager
DN_BOOT_LOG_PROB = $80000000; // S: Had a problem during preassignment of boot log conf

DN_CHANGEABLE_FLAGS = $79BB62E0;

// 4.11 Flags
DN2_EJECT_ON_REMOVE = $00000001; // S: to be check on CONFIG_REMOVE to see if ejection is wanted
DN2_LOCK_CAPABLE = $00000002; // S: Locking capable
DN2_EJECT_CAPABLE = $00000004; // S: Ejecting capable
DN2_EJECT_LOCKED = $00000008; // S: Currently locked from ejecting
DN2_EJECTED_ON_REMOVE = $00000010; // S: to be set on CONFIG_REMOVE on successful ejection
DN2_POWER_NOOP = $00000020; // S: does not care about power
DN2_STARTING = $00000040; // S: devnode is currently starting
DN2_NO_EXIT_REMOVE = $00000080; // S: devnode will not be remove at exit
DN2_NO_HIBER_DISABLE = $00000100; // S: devnode will not be disable at pageable resume

DN2_CHANGEABLE_FLAGS = $000001B6;

NUM_DN_FLAG = $00000020; // DEBUG: maximum flag (number)
NUM_DN2_FLAG = $00000009; // DEBUG: maximum flag (number)
DN_FLAG_LEN = $00000002; // DEBUG: flag length

function CM_Get_DevNode_Status(pulStatus: PULONG; pulProblemNumber: PULONG;
dnDevInst: DEVINST; ulFlags: ULONG): Integer;
stdcall external 'cfgmgr32.dll' name 'CM_Get_DevNode_Status';

implementation

end.
 
非常感谢GUANYUEGUAN,你的代码我试过了可以用。
但可能是我说的不够明白,你的代码实现的是物理段网,而我想要的不是说直接把网卡给停用了,而且是在上层把所有的包都给拦截了,我现在看到的只有驱动可以,但我一直用的NETFILTER一直运行不是稳定,有时候可以,有时候不行,所以才求救与各位,希望各位很朋友多多发言,给点思路也好,最后还是很感谢GUANYUEGUAN,你的答案我会计分的,最好散分的时候给你。^^
 
请参考以下这篇文章,台湾KTOP的。
http://delphi.ktop.com.tw/board.php?cid=168&amp;fid=920&amp;tid=33608

發表這篇文章的靈感來自於以下這個問題
【BCB】【問題】如何停止/啟動網路卡
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=33605
小弟我記得以前好像也有會員問過,所以曾經花時間研究過,目前比較清楚的,
也只知道如何停止/啟動網際網路連線的方法,參考看看吧

1.此篇文章的目的為何??
提供一個停止/啟動網際網路連線的可行方法

2.主要的程式碼為何??
void __fastcall TForm1::StopClick(TObject *Sender)
{
DWORD dwSize=0;
MIB_IFTABLE *mit=NULL;
GetIfTable(NULL, &amp;dwSize, true);
mit=(MIB_IFTABLE*)new BYTE[dwSize];
if(NO_ERROR==GetIfTable(mit, &amp;dwSize, true))
{
for(int i=0; i<(int)mit->dwNumEntries; i++)
{
mit->table.dwAdminStatus=MIB_IF_ADMIN_STATUS_DOWN;
if(NO_ERROR==SetIfEntry(&amp;mit->table))
{
ShowMessage(&quot;Stop Adapter(&quot;+IntToStr(mit->table.dwIndex)+&quot;) &quot;
+(AnsiString)(char*)mit->table.bDescr+&quot; succeed.&quot;);
}
}
}
delete mit;
}
void __fastcall TForm1::StartClick(TObject *Sender)
{
DWORD dwSize=0;
MIB_IFTABLE *mit=NULL;
GetIfTable(NULL, &amp;dwSize, true);
mit=(MIB_IFTABLE*)new BYTE[dwSize];
if(NO_ERROR==GetIfTable(mit, &amp;dwSize, true))
{
for(int i=0; i<(int)mit->dwNumEntries; i++)
{
mit->table.dwAdminStatus=MIB_IF_ADMIN_STATUS_UP;
if(NO_ERROR==SetIfEntry(&amp;mit->table))
{
ShowMessage(&quot;Start Adapter(&quot;+IntToStr(mit->table.dwIndex)+&quot;) &quot;
+(AnsiString)(char*)mit->table.bDescr+&quot; succeed.&quot;);
}
}
}
delete mit;
}

3.是否有完整的範例 Project 可以下載(包含原始碼)??
http://delphi.ktop.com.tw/loadfile.php?TOPICID=10518691&amp;CC=235249

4.該如何使用這個範例 Project??
(1)下載及解壓縮檔案 StartStopNetwork.zip,執行檔案
StartStopNetwork.exe
(2)在「Start/Stop Adapter Demo」Form 中,按「Stop All Adapter」
Button
(3)如果 (2) 執行成功的話,開啟 Internet Explorer (IE) 後,應該是連不
上任何網頁的
(4)在「Start/Stop Adapter Demo」Form 中,按「Start All Adapter」
Button
(5)如果 (4) 執行成功的話,開啟 Internet Explorer (IE) 後,應該就可以
連上網頁了

5.此種做法是否有其他的限制??
是的,有限制,目前測試的結果,並不能停止「網路芳鄰」的連線

6.在哪一個作業系統中,可以使用這個方法??
根據 M$DN 中的說明,以上程式碼用到的所有 Win32 API,在 Windows 9X/ME
/NT/2000/XP 都是適用的,因此,理論上來說,在這些作業系統應該都可以使
用;不過,目前只在 Windows 2000 測試過,有興趣的會員,也請幫忙在其他
作業系統試試看
7.如果寫得不好,或是有不對的地方,請大家多多指教
 
过去写过一个监视、控制网卡的程序,还算灵光吧,您可以试试:
const
MAX_INTERFACE_NAME_LEN = 256;
MAXLEN_PHYSADDR = 8;
MAXLEN_IFDESCR = 256;
MIB_IF_ADMIN_STATUS_UP = 1;
MIB_IF_ADMIN_STATUS_DOWN = 2;
MIB_IF_ADMIN_STATUS_TESTING = 3;

type
MIB_PHYSADDR = array[0..MAXLEN_PHYSADDR - 1] of Byte;
MIB_IFDESCR = array[0..MAXLEN_IFDESCR - 1] of Char;

PMIB_IFROW = ^MIB_IFROW;
MIB_IFROW = packed record
wszName: array[0..MAX_INTERFACE_NAME_LEN - 1] of WCHAR;
dwIndex,
dwType,
dwMtu,
dwSpeed,
dwPhysAddrLen: DWORD;
bPhysAddr: MIB_PHYSADDR;
dwAdminStatus,
dwOperStatus,
dwLastChange,
dwInOctets,
dwInUcastPkts,
dwInNUcastPkts,
dwInDiscards,
dwInErrors,
dwInUnknownProtos,
dwOutOctets,
dwOutUcastPkts,
dwOutNUcastPkts,
dwOutDiscards,
dwOutErrors,
dwOutQLen,
dwDescrLen: DWORD;
bDescr: MIB_IFDESCR;
end;

PMIB_IFTABLE = ^MIB_IFTABLE;
MIB_IFTABLE = packed record
dwNumEntries: DWORD;
table: array[0..0] of MIB_IFROW;
end;

function GetIfTable(pIfTable: PMIB_IFTABLE; var pdwSize: ULONG; bOrder: BOOL): DWORD;
stdcall; external 'iphlpapi.dll';
function SetIfEntry(const pIfRow: MIB_IFROW): DWORD;
stdcall; external 'iphlpapi.dll';

//Off 为 True 关闭,为 False 开启
procedure SetAdaptersStatus(const Off: Boolean);
var
dwSize: DWORD;
pMibIfTable: PMIB_IFTABLE;
i: Longint;
begin
GetIfTable(nil, dwSize, True);
GetMem(pMibIfTable, dwSize);
if GetIfTable(pMibIfTable, dwSize, True) = NO_ERROR then
for i := 0 to pMibIfTable^.dwNumEntries - 1 do
begin
if Off then
pMibIfTable^.table.dwAdminStatus := MIB_IF_ADMIN_STATUS_DOWN
else
pMibIfTable^.table.dwAdminStatus := MIB_IF_ADMIN_STATUS_UP;
SetIfEntry(pMibIfTable^.table);
end;
FreeMem(pMibIfTable);
end;
 
虽然vvyang也是物理断网,但是还是非常感谢guanyueguan,vvyang,稍后放分[:D].
 
不斷網只能用包攔截了,最好用驅動,效能好些。
 
guanyueguan有什么好的用驱动拦截的方法吗?我只用过NETFILTER,但效果不是很好
 
To _Murray:
我给你写的 GetIfTable、SetIfEntry 就是操作的驱动层,你可以安装一个虚拟网卡试一下,照样可以进行查询、操作...
所谓的“物理断网”,指的是拔掉网线或网卡。请您先搞清虾米是“驱动断网”和“物理断网”...
如果谁能用程序把网卡拔掉,我倒是想学习学习...[:D]
 
驅動這塊我就不熟悉了,只是了解一點而已,幫不上忙了。
不過下面這個東東不知道對你有沒有,它是可以包過濾的,變相包攔截,基於WinPcap。http://www.ccrun.com/view.asp?id=197
 
const
RAS_DLL = 'RASAPI32';

function RasDial; external RAS_DLL name 'RasDialA';
function RasEnumConnections; external RAS_DLL name 'RasEnumConnectionsA';
function RasEnumEntries; external RAS_DLL name 'RasEnumEntriesA';
function RasGetConnectStatus; external RAS_DLL name 'RasGetConnectStatusA';
function RasGetErrorString; external RAS_DLL name 'RasGetErrorStringA';
function RasHangUp; external RAS_DLL name 'RasHangUpA';
function RasGetEntryDialParams; external RAS_DLL name 'RasGetEntryDialParamsA';
 
谢谢大家了
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部