• 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.
  • 相关阅读:
    “持咒”到底是个什么东西?再论语言和思维关系
    传说中的噪声
    电源噪声(EMI)滤波器的基本原理与应用方法
    Like a rock,like the weather man【转】
    allegro笔记
    Taxicab geometry和Euclidean geometry
    hql和sql的区别
    SpringMVC与Struts2的区别
    ExtJS 学习专题如何应用ExtJS
    编程式事务和声明式事务
  • 原文地址:https://www.cnblogs.com/yzryc/p/6482708.html
Copyright © 2020-2023  润新知