实现程序的重新启动 请问高人怎么实现在程序中实现让程序关闭并重新启动 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://community.csdn.net/Expert/topic/4876/4876501.xml?temp=.3394434 以下是 liangqingzhi(老之) 提供的代码:这只是正常关机代码,不是一秒关定义全局变量 send:integer;定义过程如下:procedure tform1.ShutDown; procedure AdjustToken(); //获取关机控制权 var hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal; tmpLuid: Int64; //tkpPrivilegeCount: Int64; tkp, tkpNewButIgnored: TOKEN_PRIVILEGES; Privilege: array[0..0] of _LUID_AND_ATTRIBUTES; begin hdlProcessHandle := GetCurrentProcess; OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY), hdlTokenHandle); // Get the LUID for shutdown privilege. LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid); Privilege[0].Luid := tmpLuid; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED; tkp.PrivilegeCount := 1; // One privilege to set tkp.Privileges[0] := Privilege[0]; // Enable the shutdown privilege in the access token of this process. AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Sizeof(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded); end;begin AdjustToken; case send of 1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ; 2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ; 3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。 4: if application.MessageBox('是否重新启动', '重启计算机提示框', 1+48)=1 then //idok begin showmessage('reboot'); ExitWindowsEx(EWX_REBOOT, $FFFF); //重启操作系统 end else exit; 5: if application.MessageBox('是否关闭', '关闭计算机提示框', 1+48)=1 then //idok begin showmessage('close'); ExitWindowsEx(EWX_SHUTDOWN, $FFFF); //关机; end else exit; else EXIT; end;end;调用方法如下:procedure TForm1.Button1Click(Sender: TObject);beginsend:=3;ShutDown;end; 要重启时,生成一个bat文件,来调用主程序:procedure TForm1.Button1Click(Sender: TObject);var f:TextFile;begin AssignFile(f,'temp.bat'); Rewrite(f); Writeln(f,ExtractFileName(Application.ExeName)); Writeln(f,'del %0'); CloseFile(f); Winexec('temp.bat',sw_hide); Application.Terminate; sleep(100);end; application.Terminate;//直接关,或者用close;看情况用 ShellExecute(Application.Handle, 'OPEN', PChar(paramstr(0)), '', '', SW_SHOWNORMAL); UDP端口受限问题 [北京]诚聘Delphi程序员 将数据从sqlserver中导出到access数据库中,报错 从毕业到现在的一年中! dbgrid的问题 如何处理级联的Query的问题。 事件触发问题不明白,以前没这么做过。。 我提的问题那里去了 我想在FORM1表单起动完成后,最小化表单,问我的最小化表单代码要加在FORM1的那个过程中! 十万火鸡,如何捕获任意进程中的windows 控件的句柄,有赏 delphi 语言怎么实现输入法图标的隐藏啊???急!!! 能不能去掉TWebBrowser的弹出菜单?
这只是正常关机代码,不是一秒关定义全局变量 send:integer;
定义过程如下:procedure tform1.ShutDown;
procedure AdjustToken(); //获取关机控制权
var
hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal;
tmpLuid: Int64;
//tkpPrivilegeCount: Int64;
tkp, tkpNewButIgnored: TOKEN_PRIVILEGES;
Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),
hdlTokenHandle);
// Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
// Enable the shutdown privilege in the access token of this process.
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded);
end;
begin
AdjustToken;
case send of
1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ;
2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ;
3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。
4: if application.MessageBox('是否重新启动', '重启计算机提示框', 1+48)=1 then //idok
begin
showmessage('reboot');
ExitWindowsEx(EWX_REBOOT, $FFFF); //重启操作系统
end
else
exit;
5: if application.MessageBox('是否关闭', '关闭计算机提示框', 1+48)=1 then //idok
begin
showmessage('close');
ExitWindowsEx(EWX_SHUTDOWN, $FFFF); //关机;
end
else
exit;
else
EXIT;
end;
end;调用方法如下:procedure TForm1.Button1Click(Sender: TObject);
begin
send:=3;
ShutDown;
end;
var f:TextFile;
begin
AssignFile(f,'temp.bat');
Rewrite(f);
Writeln(f,ExtractFileName(Application.ExeName));
Writeln(f,'del %0');
CloseFile(f);
Winexec('temp.bat',sw_hide);
Application.Terminate;
sleep(100);
end;
ShellExecute(Application.Handle, 'OPEN', PChar(paramstr(0)),
'', '', SW_SHOWNORMAL);