我在使用ExitWindowsEx(EWX_SHUTDOWN,0)时,发现它并不能关机.EWX_LOGOFF倒是可以注销.请教高手为什么?同时详细解释一下第二个参数的意思.谢谢!
解决方案 »
- VC6编译的对话框,在vista下对话框字显示不出来?
- 显示远程控制得到的图片控件
- shbrowseforfolderA 与shbrowseforfolder的区别?
- 这种情况应如何处理
- 如何将一个文件夹里的文件组织成xml描述信息
- ===> 是否可以为 MSFlexGrid 每 Cell 中的串设置各自的字体大小?
- 有什么winapi可以查找某个进程,(最好同时支持9x,me,nt,2000,xp)
- Win2K 下的kernel32.dll 中是否没有API RegisterServiceProcess()了?
- 急问: DrawDibDraw 编译通过,运行出错问题
- 怎么可以将DLL加入到网页实现Winsock的功能
- DOC改变后,退出程序,弹出是否保存对话框。为何对话框上面空空如也?只有图标和按钮.
- 请问:visual studio 6.0 sp5 解压缩到哪个目录去啊?
The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.BOOL MySystemShutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return( FALSE );
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&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.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE; return TRUE;
}
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section. From re
Windows NT: To shut down or restart the system, the calling process must use theAdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, seePrivileges. it means void CShutdownDlg::OnButton1()
{
HANDLE hdl;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hdl);
PTOKEN_PRIVILEGES ptoken = (PTOKEN_PRIVILEGES) new BYTE[sizeof(DWORD) +
sizeof(LUID_AND_ATTRIBUTES)];
ptoken->PrivilegeCount = 1;
LUID uid;
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &uid); ptoken->Privileges[0].Luid = uid;
ptoken->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hdl, FALSE, ptoken, 0, NULL, NULL); //向进程表内写入数据
CloseHandle(hdl);
delete [](BYTE *)ptoken;
ExitWindowsEx(EWX_FORCE|EWX_POWEROFF,0);
}
The ExitWindowsEx function either logs off, shuts down, or shuts down and restarts the system. BOOL ExitWindowsEx( UINT uFlags, // shutdown operation
DWORD dwReserved // reserved
);第二个参数的意思:DWORD dwReserved // reserved:保留字,不用
因为2000属于多用户操作系统,要想关机就要获得权限。在查阅了MSDN后整理出了这个关机程序。 TOKEN_PRIVILEGES tkp;
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
MessageBox("OpenProcessToken failed!");
} LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //获得本地机唯一的标识
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限
if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("AdjustTokenPrivileges enable failed!");
} fResult =InitiateSystemShutdown(
NULL, // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE,
TRUE); //设为TRUE为重起,设为FALSE为关机 if(!fResult)
{
MessageBox("InitiateSystemShutdown failed.");
} tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("AdjustTokenPrivileges disable failed.");
} ExitWindowsEx(EWX_SHUTDOWN,0); //开始关机