Disable/Enable Network

Bowers, Michael P. ISD Michael_Bowers at mhhs.org
Tue Jun 28 09:01:30 CDT 2005


I did this just recently as a quick test for a proof of concept.  It
uses the SetupAPI header translation from Project Jedi and the rest is
adapted from source from the Microsoft Win2K3 DDK.  It works in
Win2K/WinXP (the only platforms of interest in my work) but may work in
others.  YMMV.


unit Unit1;

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, nil, 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.

----
Michael Bowers, Sr. Applications Programmer Analyst
Desktop Solutions R&D, Memorial Hermann Healthcare System
Email:  michael_bowers at mhhs.org
Phone:  713/448-6306              Without walls you don't
Pager:  713/605-8989 x21934         need Gates or Windows

-----Original Message-----
From: delphi-bounces at elists.org [mailto:delphi-bounces at elists.org] On
Behalf Of Ken Phipps
Sent: Monday, June 27, 2005 1:24 PM
To: Borland's Delphi Discussion List
Subject: Disable/Enable Network

Is there any way to disable/enable the network card with Delphi 7?

_______________________________________________
Delphi mailing list -> Delphi at elists.org
http://www.elists.org/mailman/listinfo/delphi



More information about the Delphi mailing list