如何启用和禁用本地连接(200分)

  • 主题发起人 主题发起人 yppxjs
  • 开始时间 开始时间
Y

yppxjs

Unregistered / Unconfirmed
GUEST, unregistred user!
系统为Windows XP,机子上装有两块网卡,如何用代码来切换两个网络?
 
查找IpHlpApi,GetIfTable,SetIfEntry
参考
http://bbs.pediy.com/showthread.php?t=54358
 
不是很明白,能否具体些。最好是能够查询到几个网卡,然后能对其每个进行断开连接或者启用连接。
 
我也想學習下這方面的知識,幫你頂
 
转一个吧,都可以在网上搜索到的,[:(]
unit Unit_FrmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrmMain = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
const
iphlpapilib = 'iphlpapi.dll';
MAX_INTERFACE_NAME_LEN = 256;
// MPRAPI.H
ANY_SIZE = 1;
MAXLEN_IFDESCR = 256;
MAXLEN_PHYSADDR = 8;
MIB_IF_ADMIN_STATUS_UP = 1;
MIB_IF_ADMIN_STATUS_DOWN = 2;
MIB_IF_ADMIN_STATUS_TESTING = 3;
type
_MIB_IFROW = record
wszName: array[0..MAX_INTERFACE_NAME_LEN - 1] of WCHAR;
dwIndex: DWORD;
dwType: DWORD;
dwMtu: DWORD;
dwSpeed: DWORD;
dwPhysAddrLen: DWORD;
bPhysAddr: array[0..MAXLEN_PHYSADDR - 1] of BYTE;
dwAdminStatus: DWORD;
dwOperStatus: DWORD;
dwLastChange: DWORD;
dwInOctets: DWORD;
dwInUcastPkts: DWORD;
dwInNUcastPkts: DWORD;
dwInDiscards: DWORD;
dwInErrors: DWORD;
dwInUnknownProtos: DWORD;
dwOutOctets: DWORD;
dwOutUcastPkts: DWORD;
dwOutNUcastPkts: DWORD;
dwOutDiscards: DWORD;
dwOutErrors: DWORD;
dwOutQLen: DWORD;
dwDescrLen: DWORD;
bDescr: array[0..MAXLEN_IFDESCR - 1] of BYTE;
end;
MIB_IFROW = _MIB_IFROW;
_MIB_IFTABLE = record
dwNumEntries: DWORD;
table: array[0..ANY_SIZE - 1] of MIB_IFROW;
end;
MIB_IFTABLE = _MIB_IFTABLE;
PMIB_IFTABLE = ^MIB_IFTABLE;
function GetIfTable(pIfTable: PMIB_IFTABLE;
var pdwSize: ULONG;
bOrder: BOOL): DWORD;
stdcall;
external iphlpapilib name 'GetIfTable';
function SetIfEntry(const pIfRow: MIB_IFROW): DWORD;
stdcall;
external iphlpapilib name 'SetIfEntry';
procedure TFrmMain.Button1Click(Sender: TObject);
var
dwSize: DWORD;
mit: PMIB_IFTABLE;
i: integer;
begin
//dwSize := 0;
//mit := nil;
GetIfTable(nil, dwSize, TRUE);
mit := GetMemory(dwSize);
if GetIfTable(mit, dwSize, TRUE) = NO_ERROR then
for i := 0 to Pred(mit^.dwNumEntries)do
begin
mit^.table.dwAdminStatus := MIB_IF_ADMIN_STATUS_DOWN;
if SetIfEntry(mit^.table) = NO_ERROR then
Showmessage('Stop Adapter(' + IntToStr(mit^.table.dwIndex) + ')'
+ pchar(@mit^.table.bDescr[0]) + ' succeed.');
end;
FreeMemory(mit);
end;

procedure TFrmMain.Button2Click(Sender: TObject);
var
dwSize: DWORD;
mit: PMIB_IFTABLE;
i: integer;
begin
//dwSize := 0;
//mit := nil;
GetIfTable(nil, dwSize, TRUE);
mit := GetMemory(dwSize);
if GetIfTable(mit, dwSize, TRUE) = NO_ERROR then
for i := 0 to Pred(mit^.dwNumEntries)do
begin
mit^.table.dwAdminStatus := MIB_IF_ADMIN_STATUS_UP;
if SetIfEntry(mit^.table) = NO_ERROR then
Showmessage('Start Adapter(' + IntToStr(mit^.table.dwIndex) + ')'
+ pchar(@mit^.table.bDescr[0]) + ' succeed.');
end;
FreeMemory(mit);
end;

end.
 
谢谢tseug,真是太感谢各位了,不过我现在解决了,也是在网上找的,我觉得这个对我的帮助挺大的。我把代码也贴下:
//要先导入Shell32_Tlb
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Shell32_TLB, StdCtrls, Menus;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
ComboBox1: TComboBox;
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}
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 -1do
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-1do
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 -1do
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-1do
begin
Item:=ControlPanel.items.Item(i);
if (Item.Name = AdapterName) then
//如果是英文的windows则Name也需是英文的
begin
for j:=0 to Item.Verbs.Count -1do
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('本地连接','启用(&A)');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ExcNetLinkMenu('本地连接','停用(&B)');
end;

end.
 
其实有时很简单,用devcon enable *DEV_4222命令一句话就搞定DEV_4222是设备的id
 
后退
顶部