如何获取所有网卡的“本地连接”字符串? ( 积分: 100 )

  • 主题发起人 主题发起人 gdrfds
  • 开始时间 开始时间
G

gdrfds

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,常规中我们的机器中只有一个网卡,所以只有一个“本地连接”,但有多网卡的会接着出现“本地连接 2”、“本地连接 3”...还有就是如果有的机器重装的网卡的,原来的“本地连接”也会自动变为“本地连接 2”。
我想总怎样的代码可获取该信息?
 
--------转载
1、查注册表:

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Network/{4D36E972-E325-11CE-BFC1-08002BE10318}/xxxx/Connection/
这个位置下面有你所有的网络连接,其中xxxx代表你的网络连接的ID,如果只是想获得名字不用去管他
每个xxxx项里的connection/ 里的name键存的就是网络连接的名称
你可以循环查找HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Network/{4D36E972-E325-11CE-BFC1-08002BE10318}里面的项来列出所有网络连接名称

注意Descriptions不是


-----------------------------------
2、
unit UPublicDef;

interface

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


const
DiscVerb = '禁用(&B)';
ConnVerb = '启用(&A)';

function GetNetLinkList(var list:TStrings):BOOLEAN;
function ExcNetLinkMenu(const AdapterName,MenuName:String):BOOLEAN;
//function CheckNetLink
implementation

{------------------------------------------------
/*列举本地连接*/
------------------------------------------------}
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
begin
ControlPanel:=Folder(Item.GetFolder);
break;
end;
end;

for i:=0 to ControlPanel.items.count-1 do
begin
Item:= ControlPanel.items.Item(i);
if Pos('本地连接',Item.Name)>0 then
List.Add(Item.Name);
end;
FreeAndNil(shell);

Result:= TRUE;
end;

{------------------------------------------------
/*操作本地连接*/
------------------------------------------------}
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
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
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;

end.



unit UHookFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, ToolWin, ImgList,UPublicDef;

type
TInstallHook=function :Boolean;Stdcall;
TUnHook=function :Boolean;Stdcall;

TFireWallFrm = class(TForm)
StatusBar1: TStatusBar;
ControlBar1: TControlBar;
Panel1: TPanel;
Panel2: TPanel;
Button1: TButton;
Button2: TButton;
ToolBar1: TToolBar;
ControlBar2: TControlBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ImageList1: TImageList;
Panel3: TPanel;
NetLinkList: TComboBox;
DisconNetLink: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure DisconNetLinkClick(Sender: TObject);
private
{ Private declarations }
InstallHook:TInstallHook;
UnHook:TUnHook;
Procedure GetNetLink;
public
{ Public declarations }
end;

var
FireWallFrm: TFireWallFrm;

implementation

{$R *.dfm}

procedure TFireWallFrm.Button1Click(Sender: TObject);
var
DllHandle:LongWord;
begin
DllHandle:=LoadLibrary('Hook.dll');
if DllHandle>0 then
begin
@InstallHook:=GetProcAddress(DllHandle,'InstallHook');
@UnHook:=GetProcAddress(DllHandle,'UnHook');
if (@InstallHook=Nil) or (@UnHook=nil) then
begin
showmessage('No Add');
Exit;
end else
begin
if InstallHook then showmessage('y')
else showmessage('n');
end;
end else
begin
showmessage('no Dll');
Exit;
end;
end;

procedure TFireWallFrm.Button2Click(Sender: TObject);
begin
UnHook;
end;

procedure TFireWallFrm.FormCreate(Sender: TObject);
begin
GetNetLink;
end;

procedure TFireWallFrm.DisconNetLinkClick(Sender: TObject);
begin
if Trim(NetLinkList.Text)='' then
begin
MessageBox(Handle,'没有选择网络连接!','注意!',Mb_Ok+MB_ICONERROR);
Exit;
end;
if DisconNetLink.Checked then ExcNetLinkMenu(Trim(NetLinkList.Text),DiscVerb)
else ExcNetLinkMenu(Trim(NetLinkList.Text),ConnVerb);
end;

procedure TFireWallFrm.GetNetLink;
var
NetLink:TStrings;
begin
NetLink:=TStringList.Create ;
GetNetLinkList(NetLink);
NetLinkList.Items:=NetLink;
FreeAndNil(NetLink);
end;

end.
 
好长的代码,我试试先!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部