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

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= 4;
  EWX_LOGOFF= 0;
  EWX_SHUTDOWN= 1;
  EWX_REBOOT= 2;
  EWX_POWEROFF= 8;
  EWX_SLEEP= 5;

function TForm1.GetWinVer: Byte;
var
  OS: TOSVersionInfo;
begin
  OS.dwOSVersionInfoSize:= SizeOf(TOSVersionInfo);
  GetVersionEx(OS);
  case OS.dwPlatformId of
    VER_PLATFORM_WIN32s : Result:= 0;//Windows 3.1x/32s
    VER_PLATFORM_WIN32_WINDOWS : Result:= 1;//Windows 95
    VER_PLATFORM_WIN32_NT : Result:= 2;//Windows NT
  else
    Result := 3;
  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:= 1;
    if(LookupPrivilegeValue(nil, PChar(PrivilegeName), NewState.Privileges[0].Luid)) then
    begin
      if Enable then
        NewState.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED
      else
        NewState.Privileges[0].Attributes:= 0;
        dwRetLen:= 0;
        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 := -1;
    ZeroMemory(@nr, sizeof(nr));
    nr.dwType := RESOURCETYPE_ANY;
    nr.lpLocalName := '';
    nr.lpProvider := '';
    nr.lpRemoteName := lpMachineName;
    dwResult := WNetAddConnection2(nr, lpPwd, lpUsr, 0)- 67;

    if(dwResult = 0) then
    begin
        if(OpenProcessToken(GetCurrentProcess(),
                TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)) then
        begin
            tp.PrivilegeCount := 1;
            if(not(LookupPrivilegeValue(lpMachineName,
                    'SeRemoteShutdownPrivilege', tp.Privileges[0].Luid))) then
            begin
              nRet := -2; // 查找远程关机权限失败
            end
            else
            begin
                tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
                if(not(AdjustTokenPrivileges(
                        hToken, false, tp, SizeOf(tp), tpNew, dwRetLen))) then
                begin
                    nRet := -3; // 调整远程关机权限失败
                end
                else
                begin
                    if(InitiateSystemShutdown(
                            lpMachineName, lpMsg, dwTimeOut, True, False)) then
                        nRet := 0
                    else
                        nRet := -4; // 远程关机执行失败
                end;
            end;
            CloseHandle(hToken);
        end;
    end
    else
        nRet := -5; // 连接到远程主机失败
    Result := nRet;
end;


procedure TForm1.ShutDownSystem(EWX_Type: Integer);
begin
  if GetWinVer= 2 then
    begin
      SetPrivilege('SeShutdownPrivilege', True);//提升权限到可以关机
      if (not ExitWindowsEx(EWX_Type, 0)) then
      SetPrivilege('SeShutdownPrivilege', False);//如果关机不成功,还将权限设置回去
    end
    else //如果内核是Win9x,或者其他,则直接调用API函数
      ExitWindowsEx(EWX_Type, 0);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  RadioButton1.Checked:= True;
  Button1.SetFocus;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if GetWinVer= 2 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 := '60';
  
  AnimateWindow(Handle, 600, 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[1] <> '\' then
    strCpt := '\\' + Edit1.Text;

  try
    begin
        dwTimeOut := StrToInt(Edit4.Text);
    end;
  except
    begin
        dwTimeOut := 0;
    end;
  end;

  nRet := ShutDownRemote(PChar(Edit1.Text), PChar(Edit2.Text), PChar(Edit3.Text),
                          PChar(Memo1.Text), dwTimeOut);
  unIcon := MB_ICONWARNING;
  case nRet of
    0:
      begin
        strMsg := 'Shut' + strCpt + ' Successfully';
        unIcon := MB_ICONINFORMATION;
      end;
    -2: strMsg := 'Search' + strCpt + ' Failed';
    -3: strMsg := 'Adjust' + strCpt + ' Failed';
    -4: strMsg := 'Shut' + strCpt + ' Failed';
    -5: strMsg := 'Connect' + strCpt + ' Failed';
    else
       strMsg := 'Connect to ' + strCpt + 'faild' + #13 + '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[1] <> '\' 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.

 

转载于:https://www.cnblogs.com/yzryc/p/6482708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值