NT中的关闭计算机,重新登陆的实现在NT中要实现以上的功能,首先必须获得SE_SHUTDOWN_NAME权限后,才能调用ExitWindowsEx;这是和Windows95的不同//iFlags:// one of the following must be specified// EWX_LOGOFF// EWX_REBOOT// EWX_SHUTDOWN// following attributes may be combined with above flags// EWX_POWEROFF// EWX_FORCE : terminate processesfunction WinExitInNT( iFlags : integer ) : boolean;beginResult := True;if( SetPrivilege( 'SeShutdownPrivilege'True ) )thenbeginif( not ExitWindowsEx( iFlags0 ) )thenbeginResult := False;end;SetPrivilege( 'SeShutdownPrivilege'False )endelse begin// handle errors...Result := False;end;end;
//Enabled or DisEnabled the SE_SHUTDOWN_NAME privilege//sPrivilegeName:// 'SE_SHUTDOWN_NAME'//bEnabled :// Enabled or DisEnabled 'SE_SHUTDOWN_NAME' privilegefunction SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;varTPPrev
TP : TTokenPrivileges;Token : THandle;dwRetLen : DWord;beginResult := False;
//opens the access token associated with a process.OpenProcessToken(GetCurrentProcess//handle to processTOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.or TOKEN_QUERY//Required to query the contents of an access token.Token);
TP.PrivilegeCount := 1;//retrieves the locally unique identifier (LUID) used on a specified system to//locally represent the specified privilege name.if( LookupPrivilegeValue(Nil//attempts to find the privilege name on the local system.PChar( sPrivilegeName )// address of string specifying the privilegeTP.Privileges[ 0 ].LUID ) // address of locally unique identifier)thenbeginif( bEnabled )then //Give this privilegesbeginTP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;endelse begin //NOT Give this privilegesTP.Privileges[ 0 ].Attributes := 0;end;
dwRetLen := 0;//enables or disables privileges in the specified access token.Result := AdjustTokenPrivileges(Token// handle to token that contains privilegesFalse//modifies privilegesTP// pointer to new privilege informationSizeOf( TPPrev )// sizein bytesof the TPPrev bufferTPPrev// receives original state of changed privilegesdwRetLen // receives required size of the TPPrev buffer);end;
CloseHandle( Token );end;
//Enabled or DisEnabled the SE_SHUTDOWN_NAME privilege//sPrivilegeName:// 'SE_SHUTDOWN_NAME'//bEnabled :// Enabled or DisEnabled 'SE_SHUTDOWN_NAME' privilegefunction SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;varTPPrev
TP : TTokenPrivileges;Token : THandle;dwRetLen : DWord;beginResult := False;
//opens the access token associated with a process.OpenProcessToken(GetCurrentProcess//handle to processTOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.or TOKEN_QUERY//Required to query the contents of an access token.Token);
TP.PrivilegeCount := 1;//retrieves the locally unique identifier (LUID) used on a specified system to//locally represent the specified privilege name.if( LookupPrivilegeValue(Nil//attempts to find the privilege name on the local system.PChar( sPrivilegeName )// address of string specifying the privilegeTP.Privileges[ 0 ].LUID ) // address of locally unique identifier)thenbeginif( bEnabled )then //Give this privilegesbeginTP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;endelse begin //NOT Give this privilegesTP.Privileges[ 0 ].Attributes := 0;end;
dwRetLen := 0;//enables or disables privileges in the specified access token.Result := AdjustTokenPrivileges(Token// handle to token that contains privilegesFalse//modifies privilegesTP// pointer to new privilege informationSizeOf( TPPrev )// sizein bytesof the TPPrev bufferTPPrev// receives original state of changed privilegesdwRetLen // receives required size of the TPPrev buffer);end;
CloseHandle( Token );end;
解决方案 »
- 只知道窗口句柄,如何设置owner?
- maskedit的属性如何设置
- 怎样将JPEG图形文件存在表中并用DBChart来显示?
- WebConnection連接, 出現"could not convert variant of type (Dispatch) into type (Integer)"
- 请问在Object Pascal中@操作符和Pointer有什么区别?
- 如何知道ListBox一共有多少项数据?
- 高手来帮忙看看~ 查询字符串的问题
- 关于程序包装的问题!星期一用,急!!!
- 如何把字符型数据录入到numeric型数据库中
- 打印打印,高手进来指点!!!
- 高手请进,一个与VCL深层工作方式有关的问题
- 多久可以上手
var
hToken :THandle ;
tkp :TOKEN_PRIVILEGES ;
otkp :TOKEN_PRIVILEGES ;
dwLen :Dword ;
begin
if OpenProcessToken(GetCurrentProcess ,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY ,hToken) then
begin
LookupPrivilegeValue(Nil ,'SeShutdownPrivilege' ,tkp.PrivilegeValue[0].Luid) ;
tkp.PrivilegeCount := 1 ;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken ,False ,tkp ,sizeof(tkp) ,otkp,dwLen) ;
if (GetLastError() = ERROR_SUCCESS) then
begin
ExitWindowsEx(EXW_POWEROFF ,0) ; //关机
end ;
end ;
谢谢你,最好给我源代码好吗?
谢谢你,最好给我源代码好吗?
我们经常会遇到在安装驱动程序或应用程序时,对系统配置进行了修改而必须重新启动Windows才能使设置生效,这时往往会弹出一个提示用户是否重新启动Windows的对话框。但有时又不希望关闭Windows,例如:一个程序正在工作,数据尚未保存,而另一个程序执行了“关闭Windows”的操作(如“网络蚂蚁”的定时关机功能),就会影响用户工作甚至造成数据丢失。此时,我们可在程序中设置防止关闭Windows的代码,只要本程序在运行中关闭Windows时,都将弹出对话框要求用户确认。 对于以上功能,我们完全可以通过编程来实现,下面就以Delphi编程来完成。 关闭(或重启)Windows 要关闭Windows,可利用API函数ExitWindowsEx(),它能够实现“注销当前用户”、“关闭Windows”以及“关闭Windows并重启”等功能,具体格式和用法如下: ExitWindowsEx(关闭类型参数,系统保留参数); 其中,系统保留参数无特定意义,一般写0即可;关闭类型可以是以下几种: EWX_FORCE:强制关闭,Windows不会发送任何消息给正运行的程序,这可能导致数据丢失; EWX_LOGOFF:关闭所有正在运行的程序,注销当前用户并重新登录; EWX_POWEROFF:关闭Windows并关机,当然,系统必须支持电源管理; EWX_REBOOT:关闭Windows并重新启动; EWX_SHUTDOWN:关闭Windows,缓冲区内的数据将被写入磁盘。 我们来看一个实例,首先新建一窗体,在上面放置一组单选钮,命名为rgExit,共三个选项:注销当前用户并重新登录;关闭Windows并重新启动;关闭Windows。再放置两个按钮,Botton1用于确认,Botton2用于取消。代码如下: Implementation {$r *.dfm} Procedure tform1.button1click(sender: tobject); Begin case rgexit.itemindex of 0: exitwindowsex(ewx_ logoff,0); //注销当前用户并重新登录 1: exitwindowsex(ewx_reboot,0); //关闭Windows并重新启动 2: exitwindowsex(ewx_shutdown,0); //关闭Windows end; End; Procedure tform1.button2click(sender: tobject); Begin close; End; 防止关闭Windows 要实现“防止关闭Windows”这一功能其实很简单,只要在程序主窗体的OnCloseQuery事件中加入以下代码即可: Procedure tform1.formclosequery(Sender: tobject; var canclose: Boolean); Begin If messagedlg('是否允许关闭?', mtconfirmation, mbokcancel, 0) = mrok then Canclose := True Else Canclose := False; End; 如果把CanClose设为False,表示不允许关闭Windows;如果CanClose设为True,则允许关闭。