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.

delphi控制本计算机和远程计算机关机等的更多相关文章

  1. c# Start/Stop/Check Status远程计算机的Windows Service

    static void Main(string[] args) { ConnectionOptions op = new ConnectionOptions(); // 登陆远程计算机的远程, op. ...

  2. ubuntu下唤醒或休眠远程计算机

    ubuntu让我明白,没有什么完美的东西,要想完美必须付出代价.要么花时间折腾,要么花时间赚钱买系统. 人生也是一样,所以不要期待什么完美.哪有那么好的人,在合适的时间合适的地点让你遇见,还对你有感觉 ...

  3. [原创]用windows7连接windows2003的终端服务器时,出现"由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的问题

    用windows7连接windows2003的终端服务器时,出现"由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的问题,原因是终端服务器授权方式设置为了"每 ...

  4. ECS Win2008 远程时提示"要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限"的解决方法

    问题描述 ECS Windows 2008 远程登陆时提示"要登录到此远程计算机,您必须被授予允许通过终端登录登录的权限",如下图所示: 问题分析 组策略中做了设置不允许管理员组成 ...

  5. Win7 远程桌面 错误代码:5 异常处理(您的远程桌面会话即将结束 此计算机的虚拟内存可能不足。请关闭其他程序,然后重试连接远程计算机。如果问题仍然存在,请联系网络管理员或技术支持。)

    问题表现: 在用windows7 远程桌面连接其他电脑时,出现错误提示对话框—-标题为“严重错误(错误代码:5)”,内容为“您的远程桌面会话即将结束 此计算机的虚拟内存可能不足.请关闭其他程序,然后重 ...

  6. win 10 远程连接出现 "由于安全设置错误, 客户端无法连接到远程计算机. 确定你已登录到网络后.” 错误

    win 10 远程连接出现 "由于安全设置错误, 客户端无法连接到远程计算机. 确定你已登录到网络后.” 错误 解决方法如下: Step 1:打开"本地安全策略"- Wi ...

  7. PC-如何提高计算机的启动和关机的速度?

    如何提高计算机的启动和关机的速度? 一.bios的优化设置 在bios设置的首页我们进入"advanced bios features"选项,将光标移到"frist bo ...

  8. (转)WS2008远程桌面连接时提示:“要登录到此远程计算机,您必须被授予允许通过终端服务登录的权限”的解决办法

    原文:http://www.chunfengxiyu.com/ws2008-mstsc-privilege.html WS2008远程桌面连接时提示:“要登录到此远程计算机,您必须被授予允许通过终端服 ...

  9. 关于win8.1“连接被远程计算机关闭”的一种解决方案

    我就是连接的时候出现"连接被远程计算机关闭",然后想着可能是win8更新之后网络协议 出问题了,后来无意中发现e信在第一次启动的时候会在网络适配器中会多出很多网卡,其中三个是带感叹 ...

随机推荐

  1. tf.nn.conv_2d

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 结果返回一个Tensor,这个输出,就是 ...

  2. 高通9X07模块QMI架构使用入门

    QMI(Qualcomm Message Interface) 高通用来替代OneRPC/DM的协议,用来与modem通信.本文是摸索高通QMI机制一点经验,重点解读了如果建立拨号连接,仅供参考.qm ...

  3. unhandledException

    处理未捕获的异常是每个应用程序起码有的功能,C#在AppDomain提供了UnhandledException 事件来接收未捕获到的异常的通知.常见的应用如下: static void Main(st ...

  4. Delphi根据方法名调用方法

    type   TForm1 = class(TForm) public published     procedure DoJsCall(str:string);                   ...

  5. selenium在scrapy中的应用

    引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会发现 ...

  6. Java——word分词·自定义词库

    word: https://github.com/ysc/word word-1.3.1.jar 需要JDK8word-1.2.jar c语言给解析成了“语言”,自定义词库必须为UTF-8 程序一旦运 ...

  7. FastDFS+Nginx+fastdfs-nginx-module集群搭建

    一.实验环境说明 操作系统: Centos 6.6 x64 FastDFS 相关版本: fastdfs-5.05 fastdfs-nginx-module-v1.16 libfastcommon-v1 ...

  8. HTML5和CSS3的一些学习记录

    1.引述块级文本的标签(blockquote): <blockquote cite="http://www.marktwainbooks.edu/"> <p> ...

  9. JS静态变量和函数、实例变量和函数以及prototype 说明

    静态变量.函数 当定义一个函数后通过 “.”为其添加的属性和函数,通过对象本身仍然可以访问得到,但是其实例却访问不到,这样的变量和函数分别被称为静态变量和静态函数,用过Java.C#的同学很好理解静态 ...

  10. mysql 存储过程动态拼接sql并执行赋值

    )) BEGIN ## 定义变量 ,) ; ## @表示全局变量 相当于php $ ## 拼接赋值 INTO 必须要用全局变量不然语句会报错 SET @strsql = CONCAT('SELECT ...