unit test;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SetupAPI;
const
GUID_DEVCLASS_NET: TGUID = '
{4D36E972-E325-11CE-BFC1-08002BE10318}';
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function StateChange(NewState, SelectedItem: cardinal; hDevInfo: HDEVINFO): boolean;
var
PropChangeParams: SP_PROPCHANGE_PARAMS;
DeviceInfoData: SP_DEVINFO_DATA;
begin
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA);
// Get handle to selected item
if not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)
then
begin
Result := False;
Exit;
end;
// Set the PropChangeParams structure for this item
PropChangeParams.ClassInstallHeader.InstallFunction := DIF_PROPERTYCHANGE;
PropChangeParams.ClassInstallHeader.cbSize := SizeOf(SP_CLASSINSTALL_HEADER);
PropChangeParams.Scope := DICS_FLAG_GLOBAL;
PropChangeParams.StateChange := NewState;
if not SetupDiSetClassInstallParams(hDevInfo, @DeviceInfoData,
PSPCLASSINSTALLHEADER(@PropChangeParams), SizeOf(PropChangeParams))
then
begin
Result := False;
Exit;
end;
// Call the ClassInstaller and perform the change
if not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, @DeviceInfoData)
then
begin
Result := False;
Exit;
end;
Result := True;
end;
// ------------------------------------------------------------------------
function DisableNetAdapter: boolean;
var
hdi: HDEVINFO;
begin
hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET, 0, 0, DIGCF_PRESENT);
if cardinal(hdi) = INVALID_HANDLE_VALUE
then
begin
Result := False;
end
else
begin
Result := StateChange(DICS_DISABLE, 0, hdi);
SetupDiDestroyDeviceInfoList(hdi);
end;
end;
// ------------------------------------------------------------------------
function EnableNetAdapter: boolean;
var
hdi: HDEVINFO;
begin
hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET,
nil, 0, DIGCF_PRESENT);
if cardinal(hdi) = INVALID_HANDLE_VALUE
then
begin
Result := False;
end
else
begin
Result := StateChange(DICS_ENABLE, 0, hdi);
SetupDiDestroyDeviceInfoList(hdi);
end;
end;
// ------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
EnableNetAdapter;
end;
// ------------------------------------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
begin
DisableNetAdapter;
end;
// ------------------------------------------------------------------------
end.