类似各种个人防火墙里的 开启网络和断开网络的功能 ,求实现方法? ( 积分: 200 )

  • 主题发起人 主题发起人 kinsunc
  • 开始时间 开始时间
K

kinsunc

Unregistered / Unconfirmed
GUEST, unregistred user!
类似各种个人防火墙里的 开启网络和断开网络的功能 ,求实现方法?
在学习过程中,发现瑞星个人防火墙里的开启网络和断开网络的功能,觉得挺好用的,因自己的水平有限,一直找实现方法,那位大侠有做过类似的程序,给个实现的方法。
 
类似各种个人防火墙里的 开启网络和断开网络的功能 ,求实现方法?
在学习过程中,发现瑞星个人防火墙里的开启网络和断开网络的功能,觉得挺好用的,因自己的水平有限,一直找实现方法,那位大侠有做过类似的程序,给个实现的方法。
 
不知道有没有人做过类似的。VC的也可以。
 
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.
 
谢谢 jingtao
 
后退
顶部