我已经已经知道利用exitwindowsex(ewx_reboot,0)进行restart!
但是在win2k中,只能起到logoff的作用.而且exitwindowsex(ewx_shutdown,0)
也只是起到了logoff的作用!只对98起作用。
请指教!!
解决方案 »
- delphi主窗体上点击按钮,的另一个窗体上的sheet对应显示
- 请问如何将一个QUERY中的数据增加到另一个QUERY,不用FOR循环一条一条增加,有其他简单办法吗
- 关于oracle数据库中blob数据类型的保存问题????
- 超简单的为题
- 求职!
- winexec或其他api隐藏着打开一个外部程序,如何做?
- 问一个比较弱智的问题,indy控件怎么装的?
- 请问在delphi中如何彻底断开一个ADOConnection的连接
- 怎样让新建的form像OpenDialog一样,总在最前,而且不允许点击其他form,否则beep ?
- 函数的返回值要求是一个类,该怎么做?谢谢
- 请问要怎么办才能避免TPaintBox不闪耀?
- 简单问题!!送分题.
首先定义常数
const
EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; //重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
ExitWindowsEx(How,0);///////////////////////////////////////////
Function ShutDownWindows(Flags: Byte) : Boolean;
begin
Result := ExitWindowsEx(Flage, 0)
// 如果成功返回 True.
end;Flags可用参数:EWX_LOGOFF:
(=0)
安全地关闭所有进程,并关闭用户登录。EWX_SHUTDOWN:
(=1)
关闭系统。所有缓冲区的内容都能被安全的存盘,所有进程都将被停止。
对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
对于Windows 95: 不必有任何特权。EWX_REBOOT:
(=2)
关闭并重新启动系统。
对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
对于Windows 95: 不必有任何特权。EWX_FORCE:
(=4)
强制切断连接,关闭所有应用程序。当使用这个参数时,Windows 将不向正在运行的应用程序发送 WM_QUERYENDSESSION 和 WM_ENDSESSION 消息,有可能造成数据丢失。所以推荐只在紧急时使用这个参数。EWX_POWEROFF:
(=8)
关闭系统并切断电源,需要ATX电源支持。
/////////////////////////////////////
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject); private
{ Private declarations }
procedure AdjustToken;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.AdjustToken();
var
hdlProcessHandle : Cardinal;
hdlTokenHandle : Cardinal;
tmpLuid : Int64;
tkpPrivilegeCount : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
AdjustToken;
ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);
end;end.
begin
if Application.MessageBox('确定關机?','確認關閉電腦',MB_YESNO )=IDYES then
begin
if RadioButton1.Checked=true then
ExitWindowsEx(EWX_LOGOFF,0) //以其他用戶身份登錄
else if RadioButton2.Checked=true then
ExitWindowsEx(EWX_SHUTDOWN,1) //安全關机
else if RadioButton3.Checked=true then
ExitWindowsEx(EWX_REBOOT,2) //重新啟動計算机
else if RadioButton4.Checked=true then
ExitWindowsEx(EWX_FORCE,4) //強行關机
else if RadioButton5.Checked=true then
ExitWindowsEx(EWX_POWEROFF,8); //關閉係統並關閉電源
end;end;
在win2k中只有logoff的作用
不信你试试!
现在的问题就是如何可以拿到shutdown的权限,用那个函数!
谢谢!指教!!
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DsFancyButton;type
TForm1 = class(TForm)
EdtRomateName: TEdit;
BtnShutDown: TButton;
procedure BtnShutDownClick(Sender: TObject);
private
{ Private declarations }
function EnablePrivilege(lpSystemName:PChar; lpName:PChar):Bool;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.EnablePrivilege(lpSystemName, lpName: PChar): Bool;
var
hToken : THANDLE;
fOk : Bool;
fTest : Bool;
tp, tpNew : TOKEN_PRIVILEGES;
ReturnLength : Cardinal;
begin
fOk := FALSE;
if OpenProcessToken(GetCurrentProcess,(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),hToken) then
begin
tp.PrivilegeCount := 1;
if not (LookupPrivilegeValue(lpSystemName,lpName,tp.Privileges[0].Luid)) then
ShowMessage('Can not lookup privilege value!');
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not (AdjustTokenPrivileges(hToken,FALSE,tp,sizeof(tp),tpNew,ReturnLength)) then
ShowMessage('Can not adjust privilege value.');
if GetLastError = ERROR_SUCCESS then
fOk := TRUE
else
fOK := FALSE;
CloseHandle(hToken);
end;
Result := fOk;
end;procedure TForm1.BtnShutDownClick(Sender: TObject);
var
RemoteShutDown : Bool;
ComputerName : PChar;begin
if EdtRomateName.Text <> '' then
begin
ComputerName := PChar(EdtRomateName.Text;
EnablePrivilege(ComputerName,'SeRemoteShutdownPrivilege');
{此句EnablePrivilege(nil,'SeShutdownPrivilege');
下一句改为RemoteShutDown :=InitiateSystemShutdown(nil,nil,0,TRUE,FALSE);将实现本地关机}
RemoteShutDown :=InitiateSystemShutdown(ComputerName ,nil,0,TRUE,FALSE);
{将InitiateSystemShutdown函数的最后一个参数改为TRUE将实现restart}
if RemoteShutDown then
ShowMessage('(远程)关机成功!')
else
ShowMessage('(远程)关机失败!');
end;
end;end.代码已经编译通过。
商榷!
我看了一下,,和上面几位大哥写的一样,,