Delphi实现WINDOWS“本地连接”的启用和禁用
作者: 来自:网络
(*************************************************************
* 这是一个禁用、启用网卡的例子,实际上通过shell可以控制整个界面,参考的网上资料
* 需要先引用 Microsoft Shell Controls And Automation(Shell32.dll)对应delphi声明 Shell32_TLB.pas
* Export:
* GetNetLinkList
* ExcNetLinkMenu
*************************************************************)
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 GetNetLinkList(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 ExcNetLinkMenu(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 ;
GetNetLinkList(List);
ComboBox1.Items:=List;
FreeAndNil(List);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ExcNetLinkMenu(’本地连接 2’,’启用(&A)’);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ExcNetLinkMenu(’本地连接 2’,’禁用(&B)’);
end;
end.
(*************************************************************
* 这是一个禁用、启用网卡的例子,实际上通过shell可以控制整个界面,参考的网上资料
* 需要先引用 Microsoft Shell Controls And Automation(Shell32.dll)对应delphi声明 Shell32_TLB.pas
* Export:
* GetNetLinkList
* ExcNetLinkMenu
*************************************************************)
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 GetNetLinkList(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 ExcNetLinkMenu(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 ;
GetNetLinkList(List);
ComboBox1.Items:=List;
FreeAndNil(List);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ExcNetLinkMenu(’本地连接 2’,’启用(&A)’);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ExcNetLinkMenu(’本地连接 2’,’禁用(&B)’);
end;
end.