📄 server.pas
字号:
unit server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, ExtCtrls,winsock, StdCtrls,inifiles, Buttons;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Timer1Timer(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
connectflag:boolean;
str:string;
implementation
{$R *.dfm}
//操作函数:关机、重启、注销。
procedure operatecomputer(statue:longword);
var
hToken:THandle;
tkp : TOKEN_PRIVILEGES;
ReturnLength : DWord;
begin
if (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken))then
begin
application.Terminate;
end;
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED;
ReturnLength :=0;
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength);
if (GetLastError() <> ERROR_SUCCESS) then
begin
application.Terminate;
end;
if (not ExitWindowsEx(statue, 0)) then
begin
application.Terminate;
end;
end;
//获取IP地址
{function LocalIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;}
procedure TForm1.FormCreate(Sender: TObject);
var myinifile:tinifile;
path,ipaddress:string;
begin
path:=extractfilepath(paramstr(0))+'config.ini';
myinifile:=tinifile.Create(path);
ipaddress:=myinifile.ReadString ('server','ip','');
if ipaddress='' then
begin
application.MessageBox('ini文件丢失或损坏!','提示',mb_iconwarning);
application.Terminate;
end
else
clientsocket1.Host:=ipaddress;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
connectflag:=true;
timer1.Enabled:=false;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
connectflag:=false;
timer1.Enabled:=true;
errorcode:=0;
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
connectflag:=false;
timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if connectflag= false then
begin
clientsocket1.Active:=true;
timer1.Enabled:=false;
end
else
timer1.Enabled:=false;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var b:string;
begin
b:=socket.ReceiveText;
case strtoint(b) of
1: begin
if win32platform =ver_platform_win32_windows then
exitwindowsex(ewx_force+ewx_shutdown+ewx_poweroff,32);
if win32platform =ver_platform_win32_NT then
operatecomputer(ewx_poweroff); //ewx_logoff 为关机
end;
2: begin
if win32platform =ver_platform_win32_windows then
exitwindowsex(ewx_force+ewx_shutdown+ewx_poweroff,32);
if win32platform =ver_platform_win32_NT then
operatecomputer(ewx_reboot); //ewx_reboot 为重启
end;
3: begin
if win32platform =ver_platform_win32_windows then
exitwindowsex(ewx_force+ewx_shutdown+ewx_poweroff,32);
if win32platform =ver_platform_win32_NT then
operatecomputer(ewx_logoff); //ewx_logoff 为注销
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -