您的位置:首页 > 理论基础 > 计算机网络

用Delphi实现关闭、重启本地网络连接

2008-11-11 16:52 387 查看
unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, OleServer, Shell32_TLB, StdCtrls;

const

connVerb = '启用';

discVerb = '停用';

type

TForm1 = class(TForm)

shl1: TShell;

btn1: TButton;

btn2: TButton;

procedure btn1Click(Sender: TObject);

procedure btn2Click(Sender: TObject);

private

{ Private declarations }

function ControlEthernet(const EthName, FolderItemVerbsName: string): Boolean;

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

{

Component -> Import ActiveX Control -> Microsoft Shell Contolrs And Automation

然后创建单元文件,之后再新建一个安装包,添加该单元文件,最后是安装该控件,就可以用了(Tshell)

}

procedure TForm1.btn1Click(Sender: TObject);

begin

ControlEthernet('本地连接', discVerb); //启用本地连接 '停用&'

end;

procedure TForm1.btn2Click(Sender: TObject);

begin

ControlEthernet('本地连接', connVerb); //启用本地连接 '启用&'

end;

function TForm1.ControlEthernet(const EthName,

FolderItemVerbsName: string): Boolean;

var cpFolder, nwFolder: Folder; //一个外壳文件夹对象

nVerbs: FolderItemVerbs; //获得上下文相关的菜单信息

i, j, k: integer;

Shell1: TShell;

begin

Result := false;

Shell1 := TShell.Create(Application);

cpFolder := Shell1.NameSpace(3); //选择控件面板

if cpFolder <> nil then

begin

for i := 0 to cpFolder.items.Count - 1 do //返回它所包含的外壳对象的集合(文件) 28

begin

if cpFolder.Items.Item(i).Name = '网络连接' then //返回的集合的名称

begin

nwFolder := cpFolder.items.item(i).GetFolder as Folder; //取得该cpFolder下面的外壳对象

if nwFolder <> nil then //内容不为空

begin

for j := 0 to nwFolder.items.Count - 1 do //历遍cpFolder下面的外壳对象

begin

if nwFolder.Items.Item(j).Name = EthName then //若果为'本地连接'

begin

nVerbs := nwFolder.Items.Item(j).Verbs; //取得该对象的上下文菜单信息

for k := 0 to nVerbs.Count - 1 do //历遍所有菜单信息

begin

if Pos(FolderItemVerbsName, nVerbs.Item(k).Name) > 0 then //如果菜单名称为 '禁用&' 时,

// 则执行该菜单命令

begin

nVerbs.Item(k).DoIt; //执行该菜单命令

//nwFolder.Items.Item(j).InvokeVerb(nwFolder.Items.Item(j).Verbs.Item(k).Name);

Result := true; //效果一致

end;

end;

end;

end;

end;

end;

end;

end;

end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: