在2000下关不了机????
ExitWindowsEx(EWX_SHUTDOWN, 0);
ExitWindowsEx(EWX_SHUTDOWN, 0);
解决方案 »
- dephi 怎样调用access里带自定义函数的查询
- 怎么调用数据模块定义的方法
- 把BUF中的前recvValue个byte放到一个对应RECORD 类型的变量,请问怎么放?
- ClientDataSet 的保存
- 解释一个程序,高分相送?简单
- 大侠help 关于删除文件的问题:(50分)
- 多线程访问数据库问题
- 我用API函数ftpgetfile下载文件,如何显示下载进度和其它状态---急待
- 做 select 操作内存暴涨,一直没释放
- 使用udp通讯时,一个端口能被两个同时运行的程序共用吗?
- Delphi中关机的函数是什么啊?
- DevExpress QuantumGrid 5.0 安装在D6的问题?
Shut down the windows for any windows type (Win98, WinNt, Win2000, ...)
use the following parameters to indicate the shut down type:
[ LOGOFF, SHUTDOWN, REBOOT, FORCE, POWEROFF ] 2004.05.26 modified from original source
*/
#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize=sizeof OSVERSIONINFO;
GetVersionEx(&osv);
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT){
HANDLE hProcess,hToken;
TOKEN_PRIVILEGES Privileges;
LUID luid;
hProcess=GetCurrentProcess();
OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);
Privileges.PrivilegeCount=1;
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
Privileges.Privileges[0].Luid=luid;
Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
}
UINT shutdowntype;
if (strcmp(lpCmdLine, "LOGOFF") == 0) {
shutdowntype = EWX_LOGOFF;
}
else if (strcmp(lpCmdLine, "SHUTDOWN") == 0) {
shutdowntype = EWX_SHUTDOWN;
}
else if (strcmp(lpCmdLine, "REBOOT") == 0) {
shutdowntype = EWX_REBOOT;
}
else if (strcmp(lpCmdLine, "FORCE") == 0) {
shutdowntype = EWX_FORCE;
}
else if (strcmp(lpCmdLine, "POWEROFF") == 0) {
shutdowntype = EWX_POWEROFF;
}
else {
return 0;
}
ExitWindowsEx(shutdowntype, 0);
return 0;
}
//关闭系统
procedure Shutdown;
var
hToken:THANDLE;
tkp:TOKEN_PRIVILEGES;
ReturnLength,BufferLength: DWORD;
ver:TOSVersionInfo;
begin
ver.dwOSVersionInfoSize:=sizeof(TOSVersionInfo);
if not GetVersionEX(ver) then Exit; if ver.dwPlatformId<VER_PLATFORM_WIN32_NT then
begin
ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);
exit;
end; // Get a token for this process.
if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES OR TOKEN_QUERY, hToken) then
begin
ShowMessage('OpenProcessToken');
Exit;
end;
// Get the LUID for the shutdown privilege.
// SE_SHUTDOWN_NAME :="SeShutdownPrivilege";
// Dest:=@Buf[1]; LookupPrivilegeValue(nil,'SeShutdownPrivilege' , tkp.Privileges[0].Luid); tkp.PrivilegeCount := 1; // one privilege to set
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;// Get the shutdown privilege for this process.
BufferLength:=0;
ReturnLength:=0;
AdjustTokenPrivileges(hToken, LongBool(false), tkp, BufferLength,nil, ReturnLength);// Cannot test the return value of AdjustTokenPrivileges. if GetLastError() <> ERROR_SUCCESS then
begin
ShowMessage('AdjustTokenPrivileges');
Exit;
end;
// Shut down the system and force all applications to close. if not ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0) then
begin
ShowMessage('ExitWindowsEx');
Exit;
end;
end;
procedure ShutDown;
var
hdlProcessHandle: Cardinal;
hdlTokenHandle: Cardinal;
tmpLuid: 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); //取得关机特权的LUID
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
//设置一个特权
tkp.PrivilegeCount := 1;
tkp.Privileges[0] := Privilege[0];
//在当前进程存取标识中加入关机特权
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded); if ForceShutdown then //强制关机
ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, $FFFF)
else
ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, $FFFF);
end;