DELPHI 禁用启用网卡


* 这是一个禁用、启用网卡的例子,实际上通过shell可以控制整个界面,参考的网上资料
* 需要先引用 Microsoft Shell Controls And Automation(Shell32.dll)对应Delphi声明 Shell32_TLB.pas
* Export :
* Ge.NetLinkList
* Ex.NetLinkMenu
* 首发大富翁(www.Delphibbs.com)blog,转载请保留
******************************************************************************)

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Shell32_TLB, StdCtrls, Menus;

type
TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;

implementation

{$R *.dfm}


{++

Routine Description:

取本机所有网络链接列表

Arguments:

OUT list - 取得的网络链接列表

Return Value:

BOOLEAN - 执行是否成功

--}

function Ge.NetLinkList(var list:TStrings):BOOLEAN;
var
Shell:TShell;
ControlPanel:Folder;
Item:FolderItem;
i:integer;
begin

Result:= FALSE;
if list = nil then
exit;

Shell:=TShell.Create(Application);
if Shell = nil then
exit;

ControlPanel:=Shell.NameSpace(ssfCONTROLS);
for i:=0 to ControlPanel.items.Count -1 do
begin
Item:=ControlPanel.items.Item(i);
if (Item.Name = '网络和拨号连接') then //如果是英文的windows则Name也需是英文的
begin
ControlPanel:=Folder(Item.GetFolder);
break;
end;
end;

for i:=0 to ControlPanel.items.count-1 do
begin
Item:= ControlPanel.items.Item(i);
List.Add(Item.Name);
end;
FreeAndNil(shell);

Result:= TRUE;
end;


{++

Routine Description:

执行 本地网络链接 的菜单命令(包括禁用、启用)

Arguments:

IN AdapterName - 网络链接名称
IN MenuName - 菜单名称

Return Value:

BOOLEAN - 执行是否成功

--}

function Ex.NetLinkMenu(const AdapterName,MenuName:String):BOOLEAN;
var
Shell:TShell;
ControlPanel:Folder;
Item:FolderItem;
i,j:integer;
Verb:FolderItemVerb;
begin

Result:= FALSE;

Shell:=TShell.Create(Application);
if Shell = nil then
exit;

ControlPanel:=Shell.NameSpace(ssfCONTROLS);
for i:=0 to ControlPanel.items.Count -1 do
begin
Item:=ControlPanel.items.Item(i);
if (Item.Name = '网络和拨号连接') then //如果是英文的windows则Name也需是英文的
begin
ControlPanel:=Folder(Item.GetFolder);
break;
end;
end;

for i:=0 to ControlPanel.items.count-1 do
begin
Item:=ControlPanel.items.Item(i);
if (Item.Name = AdapterName) then //如果是英文的windows则Name也需是英文的
begin
for j:=0 to Item.Verbs.Count -1 do
begin
Verb:=Item.Verbs.Item(j);
if (Verb.Name = MenuName) then
begin
Verb.DoIt ;
Result:=TRUE;
break;
end;
end;
break;
end;
end;

FreeAndNil(shell);

end;


procedure TForm1.Button1Click(Sender: TObject);
var
list:TStrings;
begin
List:=TStringList.Create ;
Ge.NetLinkList(List);
ComboBox1.Items:=List;
FreeAndNil(List);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Ex.NetLinkMenu('本地连接 2','启用(&A)');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Ex.NetLinkMenu('本地连接 2','禁用(&B)');
end;

end.

联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678