如何用Delphi实现关闭、重启本地网络连接?
解决方案 »
- 关于IShellFolder.GetUIObjectOf中传入多个PIDL获取IContextMenu的问题
- 如何在屏幕中 查找图片
- 插入U盘怎样获取U盘盘符?
- 简单问题20分。很容易得。快来啊
- D_Q 来继续探讨 Delphi Path-Finding(A*)算法
- BDE和sql server 的连接问题,急!
- 要参加一个OA+ERP的项目!!给点近期重点学习的方向吧!!
- 在DELPHI中如何提取机器名、IP地址和网卡号(mac)
- 本人是一菜鸟,想学编程,不知是学DEPHI还是VB
- 用Delphi如何取得CPU号.......
- Delphi中关于卸载u盘的问题
- TDBGridEH 如何设置单元格只读。
* CopyRight (c) By 姚佩云 2004
* All Right Reserved
* Email : [email protected] www.jynx.com.cn
* Date :
* New Develop : 2004-4-8
* Description :
* 这是一个禁用、启用网卡的例子,实际上通过shell可以控制整个界面,参考的网上资料
* 需要先引用 Microsoft Shell Controls And Automation(Shell32.dll)对应delphi声明 Shell32_TLB.pas
* Export :
* GetNetLinkList
* ExcNetLinkMenu
* 首发大富翁(www.delphibbs.com)blog,转载请保留
******************************************************************************)unit Unit1;interfaceuses
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.
//syrjzxf add 2007-3-29 可自动检测连接状态,备份时先禁用连接.操作完成之后再启用连接
function TDBAcc.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 nVerbs.Item(k).Name=FolderItemVerbsName 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;ControlEthernet('本地连接',connVerb); //启用本地连接 '启用&'
ControlEthernet('本地连接',discVerb); //禁用本地连接 '禁用&'
这是个什么东西?,引用哪些类可以正常使用?还是是三方控件?
delphi 2007 下Component -> Import ActiveX Control -> Microsoft Shell Contolrs And Automation
然后创建单元文件,之后再新建一个安装包,添加该单元文件,最后是安装该控件,就可以用了(Tshell)在 D7 下不用这么麻烦,直接导入这个 ActiveX 就,引用这个单元就可以使用了
if nVerbs.Item(k).Name=FolderItemVerbsName then//如果菜单名称为 '禁用&' 时,
if cpFolder.Items.Item(i).Name='网络和拨号连接' then //返回的集合的名称
http://www.2ccc.com/article.asp?articleid=3061
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ActiveX;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function 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 nVerbs.Item(k).Name=FolderItemVerbsName 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;procedure TForm1.Button1Click(Sender: TObject);
begin
ControlEthernet('本地连接',discVerb); //禁用本地连接 '禁用&'end;end.var cpFolder,nwFolder:Folder; //一个外壳文件夹对象
nVerbs:FolderItemVerbs; //获得上下文相关的菜单信息
i,j,k:integer;
Shell1:TShell;
这些地方,都不行,需要加什么单元?
能不能发一个在xp下测试过的完整代码?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, Shell32_TLB;
const
connVerb = '启用' ;
discVerb = '停用';
type
TForm1 = class(TForm)
shl1: TShell;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
function ControlEthernet(const EthName,FolderItemVerbsName: String): Boolean;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
begin
ControlEthernet('本地连接',discVerb); //启用本地连接 '启用&'
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.认真看一下8楼的回复,需要导入 Microsoft Shell Contolrs And Automation
这个单元,生成后再引用就可以了
在d7中,根本没有Shell32_TLB;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, ActiveX;
const
connVerb = '启用' ;
discVerb = '停用';
type
TForm1 = class(TForm)
procedure btn1Click(Sender: TObject); private
{ Private declarations }
public
function ControlEthernet(const EthName,FolderItemVerbsName: String): Boolean;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
begin
ControlEthernet('本地连接',discVerb); //启用本地连接 '启用&'
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.
就是不行呢?
原码
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, Common, RegStr;const
DEV_CLASS_NAME = 'Net';
UNKNOWN_DEVICE = '<未知设备>';type
TDevForm = class(TForm)
lbDev: TLabel;
btApply: TButton;
btExit: TButton;
clbDevList: TCheckListBox;
btRefresh: TButton;
procedure FormCreate(Sender: TObject);
procedure btExitClick(Sender: TObject);
procedure btApplyClick(Sender: TObject);
procedure btRefreshClick(Sender: TObject);
private
{ Private declarations }
DevState: Array of Boolean;
procedure RefreshDevState; procedure EnumNetDevice;
function IsClassHidden(const ClassGUID: PGUID): Boolean;
function ConstructDeviceName(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; Buffer: PAnsiChar; Length: ULONG): Boolean;
function GetRegistryProperty(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; AProperty: ULONG; Buffer: PAnsiChar;
Length: ULONG): Boolean;
function IsDevDisable(DevIndex: DWORD; hDevInfo: HDEVINFO): Boolean;
function ChangeDevState(DevIndex, NewState: DWORD): BOOL;
public
{ Public declarations }
end;var
DevForm: TDevForm;implementation{$R *.dfm}procedure TDevForm.EnumNetDevice;
var
DeviceInfoSet: HDEVINFO;
DeviceInfoData: SP_DEVINFO_DATA;
i: Integer;
Status, Problem: DWORD;
ClassName: PChar;
ClassSize, ReqClassSize: DWORD;
DeviceName: PChar;
begin
clbDevList.Clear; DeviceInfoSet:=SetupDiGetClassDevs(Nil,Nil,0,DIGCF_ALLCLASSES or DIGCF_PRESENT);
if DeviceInfoSet = Pointer(INVALID_HANDLE_VALUE) then
Exit; ClassSize:=255;
GetMem(ClassName,256);
try
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA); i:=0;
while SetupDiEnumDeviceInfo(DeviceInfoSet,i,@DeviceInfoData) do
begin
Inc(i); if not SetupDiClassNameFromGuid(@DeviceInfoData.ClassGuid,ClassName,ClassSize,
@ReqClassSize) then
begin
if ReqClassSize>ClassSize then
begin
FreeMem(ClassName);
ClassSize:=ReqClassSize;
GetMem(ClassName,ClassSize+1);
if not SetupDiClassNameFromGuid(@DeviceInfoData.ClassGuid,ClassName,ClassSize,
@ReqClassSize) then
Exit;
end
else
Exit;
end; if not SameText(ClassName,DEV_CLASS_NAME) then
Continue; if CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst,0)
<> CR_SUCCESS then
Exit; if ((Status and DN_NO_SHOW_IN_DM)<>0) or
IsClassHidden(@DeviceInfoData.ClassGuid) then
Continue; GetMem(DeviceName,256);
ZeroMemory(DeviceName,256);
ConstructDeviceName(DeviceInfoSet,@DeviceInfoData,DeviceName,255);
clbDevList.Items.AddObject(StrPas(DeviceName),TObject(i-1));
clbDevList.Checked[clbDevList.Count-1]:=IsDevDisable(i-1,DeviceInfoSet);
FreeMem(DeviceName);
end;
finally
FreeMem(ClassName);
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
end;
end;function TDevForm.ConstructDeviceName(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; Buffer: PAnsiChar;
Length: ULONG): Boolean;
begin
Result:=True; if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_FRIENDLYNAME,
Buffer,Length) then
begin
if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_DEVICEDESC,
Buffer,Length) then
begin
if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_CLASS,
Buffer,Length) then
begin
if not GetRegistryProperty(DeviceInfoSet,DeviceInfoData,SPDRP_CLASSGUID,
Buffer,Length) then
begin
StrCopy(Buffer,UNKNOWN_DEVICE);
end
else
Result:=False;
end
end
end;
end;function TDevForm.GetRegistryProperty(DeviceInfoSet: HDEVINFO;
DeviceInfoData: PSP_DEVINFO_DATA; AProperty: ULONG; Buffer: PAnsiChar;
Length: ULONG): Boolean;
var
ReqLen: DWORD;
begin
Result:=False; while not SetupDiGetDeviceRegistryProperty(DeviceInfoSet,DeviceInfoData,
AProperty,Nil,Buffer,Length,@ReqLen) do
begin
if GetLastError() = ERROR_INVALID_DATA then
break
else if GetLastError() = ERROR_INSUFFICIENT_BUFFER then
begin
if Assigned(Buffer) then
FreeMem(Buffer);
Length:=ReqLen;
GetMem(Buffer,Length+1);
end
else
Exit;
end; Result:=Buffer^<>#0;
end;function TDevForm.IsClassHidden(const ClassGUID: PGUID): Boolean;
var
hKeyClass: HKEY;
begin
Result:=False; hKeyClass := SetupDiOpenClassRegKey(ClassGuid,KEY_READ);
if hKeyClass<>0 then
begin
Result:= RegQueryValueEx(hKeyClass,REGSTR_VAL_NODISPLAYCLASS,Nil,Nil,NIl,Nil) = ERROR_SUCCESS;
RegCloseKey(hKeyClass);
end;
end;function TDevForm.IsDevDisable(DevIndex: DWORD;
hDevInfo: HDEVINFO): Boolean;
var
DeviceInfoData: SP_DEVINFO_DATA;
Status, Problem: DWORD;
begin
Result:=False;
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA); if not SetupDiEnumDeviceInfo(hDevInfo,DevIndex,@DeviceInfoData) then
Exit; if CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS then
Exit; Result:=((Status and DN_DISABLEABLE)<>0) and (CM_PROB_HARDWARE_DISABLED <> Problem);
end;function TDevForm.ChangeDevState(DevIndex, NewState: DWORD): BOOL;
var
DeviceInfoSet: HDEVINFO;
DeviceInfoData: SP_DEVINFO_DATA;
PropChangeParams: SP_PROPCHANGE_PARAMS;
Cursor: HCURSOR;
begin
Result:=False; DeviceInfoSet:=SetupDiGetClassDevs(Nil,Nil,0,DIGCF_ALLCLASSES or DIGCF_PRESENT);
if DeviceInfoSet = Pointer(INVALID_HANDLE_VALUE) then
Exit; try
PropChangeParams.ClassInstallHeader.cbSize:=SizeOf(SP_CLASSINSTALL_HEADER);
DeviceInfoData.cbSize:=SizeOf(SP_DEVINFO_DATA); Cursor := SetCursor(LoadCursor(0, IDC_WAIT)); if not SetupDiEnumDeviceInfo(DeviceInfoSet,DevIndex,@DeviceInfoData) then
Exit; PropChangeParams.ClassInstallHeader.InstallFunction := DIF_PROPERTYCHANGE;
PropChangeParams.Scope := DICS_FLAG_GLOBAL;
PropChangeParams.StateChange := NewState; if not SetupDiSetClassInstallParams(DeviceInfoSet,@DeviceInfoData,
@PropChangeParams,Sizeof(PropChangeParams)) then
Exit; if not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,DeviceInfoSet,
@DeviceInfoData) then
Exit; SetCursor(Cursor);
Result:=True;
finally
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
end;
end;procedure TDevForm.FormCreate(Sender: TObject);
begin
btRefresh.Click;
end;procedure TDevForm.btExitClick(Sender: TObject);
begin
Close;
end;procedure TDevForm.btApplyClick(Sender: TObject);
var
i: Integer;
begin
for i:=0 to clbDevList.Count-1 do
begin
if clbDevList.Checked[i]<>DevState[i] then
begin
if clbDevList.Checked[i] then
ChangeDevState(Cardinal(clbDevList.Items.Objects[i]),DICS_ENABLE)
else
ChangeDevState(Cardinal(clbDevList.Items.Objects[i]),DICS_DISABLE)
end;
end;
RefreshDevState;
end;procedure TDevForm.RefreshDevState;
var
i: Integer;
begin
SetLength(DevState,clbDevList.Count);
for i:=0 to clbDevList.Count-1 do
DevState[i]:=clbDevList.Checked[i];
end;procedure TDevForm.btRefreshClick(Sender: TObject);
begin
EnumNetDevice;
RefreshDevState;
end;end.这个不错,不用加哪些单元,而且测试通过.
谢谢大家
这样D7好像装不了,要用project->Import Type Library->
Common这个单元,在2007下没有?是什么单元。