Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8610301
  • 博文数量: 1413
  • 博客积分: 11128
  • 博客等级: 上将
  • 技术积分: 14685
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 10:03
个人简介

follow my heart...

文章分类

全部博文(1413)

文章存档

2013年(1)

2012年(5)

2011年(45)

2010年(176)

2009年(148)

2008年(190)

2007年(293)

2006年(555)

分类:

2006-11-08 14:35:17

[点评:这里是针对网卡进行网络的启用和断开.]

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.

阅读(2557) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-07-14 10:59:06

非常好.现在有一个问题,我是机房管理员,上课时,学生们经常偷偷上网(不用通过教师机).我现在想通过教师机把机房里的电脑全部与外网断开但又要保持内网的畅通,怎么办呢.