我调用ExitWindowsEx(EWX_SHUTDOWN,0)关闭WIN98可以,但是关闭WIN2000确不能起作用,这是为什么呢?我看了一下帮助,发现如下内容:
Windows NT: To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, see Privileges.好象前提是要调用AdjustTokenPrivileges获取权限,但是这个函数该怎么用呀?多谢帮忙了!
Windows NT: To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, see Privileges.好象前提是要调用AdjustTokenPrivileges获取权限,但是这个函数该怎么用呀?多谢帮忙了!
解决方案 »
- tidhttp访问https的问题
- 再一次发贴求助DLL窗体的问题 主程序进行大量工作时,DLL窗体会暂时冻结
- 我想问一问,TButton的标准的height和width是多少?就象windows的按钮的大小一样
- 急:整型如何转换成CHAR类型?多谢
- 关于Access数据库的问题,请各位帮忙......
- 疑惑!!调用windows API KERNEL32.DLL函数的问题。
- 动态创建popmenu怎样得到item.Caption?
- adoQuery记录行下移会触发什么事件??
- 关于创建FORM问题~
- 请教CommandButton中的ModalResult属性是做什么用的?能举个例子讲讲吗?
- 关于radiobutton的问题
- 在TDatasetProvider组件的data属性中是否也传递了表字段的类型、名称等信息,如何取得或设置这些值
Procedure ShutDown2k;
Var
hToken:THandle;
tkp : TOKEN_PRIVILEGES;
ReturnLength : DWord;
Begin
If (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken))then
begin
application.Terminate;
end;
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED;
ReturnLength :=0;
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength);
If (GetLastError() <> ERROR_SUCCESS) then
begin
application.Terminate;
End;
If (not ExitWindowsEx(EWX_POWEROFF, 0)) then
Begin
Application.Terminate;
end;
End;
首先定义常数
const
EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; //重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
ExitWindowsEx(How,0);
不過只有WIN9X下有效,NT和WIN2000不行。
系統時間用Now()
我查了一下下载的大富翁论坛,这方面的太多了阿,所以不准备都给你贴出来了,还是自己去下一个吧!好运!
procedure GetAdmin;
var
st : SYSTEMTIME;
hToken : THANDLE;
tkp : TOKEN_PRIVILEGES;
rr : Dword;
begin
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
// 設定權限為1
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
// 得到系統權限
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,rr);
end;
首先聲明type
......
procedure GetAdmin;
......
implementation{$R *.DFM}
procedure GetAdmin;
var
st : SYSTEMTIME;
hToken : THANDLE;
tkp : TOKEN_PRIVILEGES;
rr : Dword;
begin
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
// 設定權限為1
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
// 得到系統權限
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,rr);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
GetAdmin;//調用獲取權限過程;
ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, 0);//強行結束當前全部進程後關閉系統;
//ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, 0);//關閉系統前詢問用戶;
//ExitWindowsEx(EWX_REBOOT, 0);//重起系統;
//ExitWindowsEx(EWX_REBOOT OR EWX_force, 0)//強行結束全部進程後重起;
//ExitWindowsEx(EWX_logoff, 0);//登出;
//ExitWindowsEx(EWX_logoff OR EWX_force, 0);//強行結束全部進程後登出;
end;
{取得系統權限過程不可缺少,否則在NT內核的系統中不能正常通過驗証的.}
其实WIN2000与WIN98的关机函数是一样的(ExitWindowsEx),只是WIN2000多了个过程,不允许没有足够权限的程序从事这项操作,所以加一个调整权限的操作步骤就可以了,上面代码中:AdjustTokenPrivileges函数就是调整权限的了.