delphi 怎么断网

发布时间:2024-11-08 03:18 发布:上海旅游网

问题描述:

禁用网卡好像不是立马断网的
在我的电脑OK了
调用程序

问题解答:

winexec('rasphone -h "adsl"',sw_hide);
winexec('c:/windows/system32/rasphone -d "adsl"',sw_hide);

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[i].dwAdminStatus := MIB_IF_ADMIN_STATUS_DOWN;
if SetIfEntry(mit^.table[i]) = NO_ERROR then
Showmessage('Stop Adapter(' + IntToStr(mit^.table[i].dwIndex) + ')'
+ pchar(@mit^.table[i].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[i].dwAdminStatus := MIB_IF_ADMIN_STATUS_UP;
if SetIfEntry(mit^.table[i]) = NO_ERROR then
Showmessage('Start Adapter(' + IntToStr(mit^.table[i].dwIndex) + ')'
+ pchar(@mit^.table[i].bDescr[0]) + ' succeed.');
end;
FreeMemory(mit);
end;

end.

热点新闻