红旗下的蛋

网络、计算机相关
posts - 3, comments - 0, trackbacks - 0, articles - 6

远程关机的部分源代码

Posted on 2006-04-29 10:41 红旗下的蛋 阅读(237) 评论(0)  编辑 收藏 引用 所属分类: 计算机程序

//远程关机的部分源代码
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient,
ExtCtrls, Buttons;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Edthost: TEdit;
Label1: TLabel;
ListBox2: TListBox;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
procedure wakeup(macaddr:string);
procedure readnamelist;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.readnamelist;
var namelistfile:textfile;
str,str1,str2:string;
begin
//读出mac地址列表
AssignFile(namelistfile, application.GetNamePath+'namelist.cfg');
reset(namelistfile);

try
begin
readln(namelistfile,str);
edthost.Text:=str;
while not eof(namelistfile) do
begin
readln(namelistfile,str);
str1:=copy(str,0,pos(' ',str)-1);
str2:=copy(str,pos(' ',str)+1,length(str)-length(str1));
listbox1.Items.Append(str1);
listbox2.Items.Append(str2);
end;
end;
finally
closefile(namelistfile);
end;

end;

procedure TForm1.wakeup(macaddr: string);
var fudp:TIdUDPClient;
sendcode:string;
i,j:integer;
bin:string[6];
begin
//唤醒过程
fudp:=TIdUDPClient.create(nil);
try
fudp.Port:=7;
fudp.Host:=edthost.Text;
fudp.Active:=true;
hextobin(pchar(macaddr),@bin,6);
sendcode:=stringofchar(#255,6);

//填充缓冲区
for i:=0 to 15 do
for j:=0 to 5 do
sendcode:=sendcode+bin[j];

for i:=0 to 5 do //连续发送6次保险
fudp.Broadcast(sendcode,7);


finally
fudp.Free;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
readnamelist;
if listbox1.Items.Text='' then
begin
button1.Enabled:=false;
button2.Enabled:=false;
exit;
end;
listbox1.ItemIndex:=0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
//showmessage(inttostr(listbox1.selcount));
//if listbox1.s then
begin
str:=listbox2.items[listbox1.itemindex];
wakeup(trim(str));
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
for i:=0 to listbox1.Items.Count-1 do
begin
//showmessage(trim(listbox2.Items[i]));
wakeup(trim(listbox2.Items[i]));
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var shutdownlistfile,batfile:textfile;
str,str1,str2:string;
begin

AssignFile(shutdownlistfile, application.GetNamePath+'shutdown.cfg');
assignfile(batfile,application.GetNamePath+'myshutdown.bat');
reset(shutdownlistfile);
rewrite(batfile);
try
while not eof(shutdownlistfile) do
begin
readln(shutdownlistfile,str);
str:='shutdown -s -m '+str;
writeln(batfile,str);
end;
writeln(batfile,'pause');
finally
closefile(shutdownlistfile);
closefile(batfile);
end;
// ShellExecute(Application.Handle,'open',’NotePad.exe’,PChar(’C:AutoExec.bat’),nil,SW_SHOWNORMAL);

WinExec(pchar(application.getnamepath+'myshutdown.bat'),SW_MINIMIZE);
end;

end.

只有注册用户登录后才能发表评论。