delphi控制本计算机和远程计算机关机等

时间:2022-10-05 17:04:27
unit mainunit;
{远程关机源码}
interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils; type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
RadioButton5: TRadioButton;
RadioButton6: TRadioButton;
GroupBox2: TGroupBox;
Label2: TLabel;
Edit1: TEdit;
Button3: TButton;
Label3: TLabel;
Label4: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Label5: TLabel;
Edit4: TEdit;
Memo1: TMemo;
Label6: TLabel;
Button4: TButton;
Button5: TButton;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function GetWinVer: Byte;//获得当前操作系统的版本
function SetPrivilege(PrivilegeName: string; Enable: Boolean): Boolean;//设置系统权限
procedure ShutDownSystem(EWX_Type: Integer);//根据关机类型,执行操作
function ShutDownRemote(lpMachineName: PChar; lpUsr: PChar; lpPwd: PChar; lpMsg: PChar; dwTimeOut: DWORD): Integer;
end; var
Form1: TForm1; implementation {$R *.dfm}
const
EWX_FORCE= ;
EWX_LOGOFF= ;
EWX_SHUTDOWN= ;
EWX_REBOOT= ;
EWX_POWEROFF= ;
EWX_SLEEP= ; function TForm1.GetWinVer: Byte;
var
OS: TOSVersionInfo;
begin
OS.dwOSVersionInfoSize:= SizeOf(TOSVersionInfo);
GetVersionEx(OS);
case OS.dwPlatformId of
VER_PLATFORM_WIN32s : Result:= ;//Windows 3.1x/32s
VER_PLATFORM_WIN32_WINDOWS : Result:= ;//Windows 95
VER_PLATFORM_WIN32_NT : Result:= ;//Windows NT
else
Result := ;
end;
end; function TForm1.SetPrivilege(PrivilegeName: string; Enable: Boolean): Boolean;
var
NewState, PreviousState: TTokenPrivileges;
Token: THandle;
dwRetLen: DWORD;
begin
Result:= False;
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
NewState.PrivilegeCount:= ;
if(LookupPrivilegeValue(nil, PChar(PrivilegeName), NewState.Privileges[].Luid)) then
begin
if Enable then
NewState.Privileges[].Attributes:= SE_PRIVILEGE_ENABLED
else
NewState.Privileges[].Attributes:= ;
dwRetLen:= ;
Result:= AdjustTokenPrivileges(Token, False, NewState, SizeOf(PreviousState), PreviousState, dwRetLen);
end;
CloseHandle(token);
end; function TForm1.ShutDownRemote(lpMachineName: PChar; lpUsr: PChar; lpPwd: PChar; lpMsg: PChar; dwTimeOut: DWORD): Integer;
var
hToken: THandle;
nRet: Integer;
tp, tpNew: TOKEN_PRIVILEGES;
nr: _NETRESOURCE;
dwRetLen, dwResult: DWORD;
begin
nRet := -;
ZeroMemory(@nr, sizeof(nr));
nr.dwType := RESOURCETYPE_ANY;
nr.lpLocalName := '';
nr.lpProvider := '';
nr.lpRemoteName := lpMachineName;
dwResult := WNetAddConnection2(nr, lpPwd, lpUsr, )- ; if(dwResult = ) then
begin
if(OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)) then
begin
tp.PrivilegeCount := ;
if(not(LookupPrivilegeValue(lpMachineName,
'SeRemoteShutdownPrivilege', tp.Privileges[].Luid))) then
begin
nRet := -; // 查找远程关机权限失败
end
else
begin
tp.Privileges[].Attributes := SE_PRIVILEGE_ENABLED;
if(not(AdjustTokenPrivileges(
hToken, false, tp, SizeOf(tp), tpNew, dwRetLen))) then
begin
nRet := -; // 调整远程关机权限失败
end
else
begin
if(InitiateSystemShutdown(
lpMachineName, lpMsg, dwTimeOut, True, False)) then
nRet :=
else
nRet := -; // 远程关机执行失败
end;
end;
CloseHandle(hToken);
end;
end
else
nRet := -; // 连接到远程主机失败
Result := nRet;
end; procedure TForm1.ShutDownSystem(EWX_Type: Integer);
begin
if GetWinVer= then
begin
SetPrivilege('SeShutdownPrivilege', True);//提升权限到可以关机
if (not ExitWindowsEx(EWX_Type, )) then
SetPrivilege('SeShutdownPrivilege', False);//如果关机不成功,还将权限设置回去
end
else //如果内核是Win9x,或者其他,则直接调用API函数
ExitWindowsEx(EWX_Type, );
end; procedure TForm1.FormShow(Sender: TObject);
begin
RadioButton1.Checked:= True;
Button1.SetFocus;
end; procedure TForm1.FormCreate(Sender: TObject);
begin
if GetWinVer= then
Label1.Caption:= Label1.Caption + 'Windows NT'
else
Label1.Caption:= Label1.Caption + 'Windows 95/98'; Memo1.Text := '';
Edit1.Text := '192.168.135.123';
Edit2.Text := 'Administrator';
Edit4.Text := ''; AnimateWindow(Handle, , AW_CENTER);
end; procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if(Application.MessageBox('Do you decide to Execute?', 'System Warning', MB_OKCANCEL + MB_OK)= IDOK) then
begin
if (RadioButton1.Checked) then
ShutDownSystem(EWX_SHUTDOWN)
else if (RadioButton2.Checked) then
ShutDownSystem(EWX_REBOOT)
else if (RadioButton3.Checked) then
ShutDownSystem(EWX_LOGOFF)
else if (RadioButton4.Checked) then
ShutDownSystem(EWX_POWEROFF)
else if (RadioButton5.Checked) then
ShutDownSystem(EWX_FORCE)
else if (RadioButton6.Checked) then
ShutDownSystem(EWX_FORCEIFHUNG);
end;
end; procedure TForm1.Button3Click(Sender: TObject);
var
strMsg, strCpt: string;
dwTimeOut: DWORD;
nRet: Integer;
unIcon: UINT;
begin
if (Edit1.Text = '') or (Edit2.Text = '') then
MessageBox(Handle, 'Input ComputerIP and UserName', 'System Warning',
MB_OK + MB_ICONWARNING); if Edit1.Text[] <> '\' then
strCpt := '\\' + Edit1.Text; try
begin
dwTimeOut := StrToInt(Edit4.Text);
end;
except
begin
dwTimeOut := ;
end;
end; nRet := ShutDownRemote(PChar(Edit1.Text), PChar(Edit2.Text), PChar(Edit3.Text),
PChar(Memo1.Text), dwTimeOut);
unIcon := MB_ICONWARNING;
case nRet of
:
begin
strMsg := 'Shut' + strCpt + ' Successfully';
unIcon := MB_ICONINFORMATION;
end;
-: strMsg := 'Search' + strCpt + ' Failed';
-: strMsg := 'Adjust' + strCpt + ' Failed';
-: strMsg := 'Shut' + strCpt + ' Failed';
-: strMsg := 'Connect' + strCpt + ' Failed';
else
strMsg := 'Connect to ' + strCpt + 'faild' + # + 'Maybe Can not find the IP';
//SysErrorMessage(nRet);
end;
MessageBox(Handle, PChar(strMsg), 'System FeedBack', MB_OK + unIcon);
end; procedure TForm1.Button4Click(Sender: TObject);
var
strCpt: string;
begin
if Edit1.Text[] <> '\' then
strCpt := '\\' + Edit1.Text;
if AbortSystemShutdown(PChar(strCpt)) then
MessageBox(Handle, 'Abort Shut System successfully', 'System FeedBack', MB_OK+MB_ICONINFORMATION)
else
MessageBox(Handle, 'Abort Shut System failed', 'System FeedBack', MB_OK+MB_ICONWARNING);
end; procedure TForm1.Button5Click(Sender: TObject);
begin
Close;
end; end.