ExitWindowsEx函数的问题

small_wei 2005-07-25 11:41:15
在MSDN中是这样说的
BOOL ExitWindowsEx(
UINT uFlags,
DWORD dwReason
);

Parameters
uFlags
[in] Shutdown type. This parameter must include one of the following values. Value Meaning
EWX_LOGOFF Shuts down all processes running in the logon session of the process that called the ExitWindowsEx function. Then it logs the user off.
This flag can be used only by processes running in an interactive user's logon session.

EWX_POWEROFF Shuts down the system and turns off the power. The system must support the power-off feature.
The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

EWX_REBOOT Shuts down the system and then restarts the system.
The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped.
The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

Specifying this flag will not turn off the power even if the system supports the power-off feature. You must specify EWX_POWEROFF to do this.

Windows XP SP1: If the system supports the power-off feature, specifying this flag turns off the power.



This parameter can optionally include the following values.


Value Meaning
EWX_FORCE Forces processes to terminate. When this flag is set, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.

Windows XP: If the computer is locked and this flag is not specified, the shutdown process will fail.
EWX_FORCEIFHUNG Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message within the timeout interval. For more information, see the Remarks.
This flag is ignored if EWX_FORCE is used.

Windows NT and Windows Me/98/95: This value is not supported.

dwReason
[in] Reason for initiating the shutdown. This parameter must be one of the system shutdown reason codes.
If this parameter is zero, the SHTDN_REASON_FLAG_PLANNED reason code will not be set and therefore the default action is an undefined shutdown that is logged as "No title for this reason could be found". By default, it is also an unplanned shutdown. Depending on how the system is configured, an unplanned shutdown triggers the creation of a file that contains the system state information, which can delay shutdown. Therefore, do not use zero for this parameter.

Windows 2000, Windows NT, and Windows Me/98/95: This parameter is ignored.
Return Values
If the function succeeds, the return value is nonzero. Because the function executes asynchronously, a nonzero return value indicates that the shutdown has been initiated. It does not indicate whether the shutdown will succeed. It is possible that the system, the user, or another application will abort the shutdown.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
The ExitWindowsEx function returns as soon as it has initiated the shutdown process. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the caller's logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.

A non-zero return value does not mean the logoff was or will be successful. The shutdown is an asynchronous process, and it can occur long after the API call has returned, or not at all. Even if the timeout value is zero, the shutdown can still be aborted by applications, services, or even the system. The non-zero return value indicates that the validation of the rights and parameters was successful and that the system accepted the shutdown request.

When this function is called, the caller must specify whether or not applications with unsaved changes should be forcibly closed. If the caller chooses not to force these applications to close and an application with unsaved changes is running on the console session, the shutdown will remain in progress until the user logged into the console session aborts the shutdown, saves changes, closes the application, or forces the application to close. During this period, the shutdown may not be aborted except by the console user, and another shutdown may not be initiated.

Calling this function with the value of the uFlags parameter set to EWX_FORCE avoids this situation. Remember that doing this may result in loss of data.

To set a shutdown priority for an application relative to other applications in the system, use the SetProcessShutdownParameters function.

During a shutdown or log-off operation, applications that are shut down are allowed a specific amount of time to respond to the shutdown request. If the time expires, the system displays a dialog box that allows the user to forcibly shut down the application, to retry the shutdown, or to cancel the shutdown request. If the EWX_FORCE value is specified, the system always forces applications to close and does not display the dialog box.

If the EWX_FORCEIFHUNG value is specified, the system forces hung applications to close and does not display the dialog box.

Windows Me/98/95: Because of the design of the shell, calling ExitWindowsEx with EWX_FORCE fails to completely log off the user (the system terminates the applications and displays the Enter Windows Password dialog box, however, the user's desktop remains.) To log off the user forcibly, terminate the Explorer process before calling ExitWindowsEx with EWX_LOGOFF and EWX_FORCE.
Console processes receive a separate notification message, CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT, as the situation warrants. A console process routes these messages to its HandlerRoutine function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns.

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, see Running with Special Privileges.


Windows Me/98/95: ExitWindowsEx does not work from a console application.

uFlags参数中只有关机和注销、重起,
EWX_POWEROFF,EWX_SHUTDOWN这两个我都试过了,都是关机,现在我想进入休眠和待机怎么做呢?
...全文
194 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fu0212 2005-07-25
  • 打赏
  • 举报
回复
void CPage1::Onsss()
{
if(MessageBox("确实要休眠吗?","关机程序",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES)
{
static HANDLE hToken;
static TOKEN_PRIVILEGES tp;
static LUID luid;
if(::OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
&hToken))
{
::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
tp.PrivilegeCount=1;
tp.Privileges[0].Luid =luid;
tp.Privileges[0].Attributes =SE_PRIVILEGE_ENABLED;
::AdjustTokenPrivileges(hToken,false,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
}
::SetSystemPowerState(false,true);
}
}
fu0212 2005-07-25
  • 打赏
  • 举报
回复
耶!!!
全是红星的啊?
帮会开会?谈判?
各位老大 走错门了 呵呵
DentistryDoctor 2005-07-25
  • 打赏
  • 举报
回复
来晚了。
small_wei 2005-07-25
  • 打赏
  • 举报
回复
OK,谢谢,我试试,前面得加些罗哩八嗦的东东我已经有了
快乐鹦鹉 2005-07-25
  • 打赏
  • 举报
回复
http://www.enet.com.cn/article/2005/0401/A20050401404388.shtml
快乐鹦鹉 2005-07-25
  • 打赏
  • 举报
回复
http://www.goo6.com/abc-fx/ReadNews.asp?NewsID=425&BigClassName=%BF%C6%BC%BC%D7%CA%D1%B6&SmallClassName=%B5%E7%C4%D4%C5%E0%D1%B5&SpecialID=0
kugou123 2005-07-25
  • 打赏
  • 举报
回复
函数很简单,但直接用不好使,前面得加些罗哩八嗦的东东,所以可以进行一下封装。

void PERR(LPTSTR szAPI, DWORD dwLastError);
#define RTN_ERROR 13

INT SetPower()
{

// TODO: Add your control notificationhandler code here
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;

LPTSTR MachineName=NULL; // pointer to machine name

if(!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken ))
{
PERR("OpenProcessToken", GetLastError() );
return RTN_ERROR;
}

if(!LookupPrivilegeValue(MachineName, SE_SHUTDOWN_NAME, &luid))

{
PERR("LookupPrivilegeValue", GetLastError() );
return RTN_ERROR;
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
NULL, NULL );

SetSystemPowerState(FALSE,TRUE);
return 0;

}
void PERR(
LPTSTR szAPI, // pointer to failed API name
DWORD dwLastError // last error value associated with API
)
{
LPTSTR MessageBuffer;
DWORD dwBufferLength;

//
// TODO get this fprintf out of here!
//
fprintf(stderr,"%s error! (rc=%lu)\n", szAPI, dwLastError);

if(dwBufferLength=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwLastError,
LANG_NEUTRAL,
(LPTSTR) &MessageBuffer,
0,
NULL))
{

DWORD dwBytesWritten;

//
// Output message string on stderr
//
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
MessageBuffer,
dwBufferLength,
&dwBytesWritten,
NULL);

//
// free the buffer allocated by the system
//
LocalFree(MessageBuffer);
}
}

注意:此函数只运行于Windows 2000/XP,并且打开了高级电源管理的休眠支持。
kugou123 2005-07-25
  • 打赏
  • 举报
回复
BOOL SetSystemPowerState(
BOOL fSuspend, // system state
BOOL fForce // forced suspension option
);
kugou123 2005-07-25
  • 打赏
  • 举报
回复
BOOL SetSystemPowerState(
BOOL fSuspend, // system state
BOOL fForce // forced suspension option
);
bobob 2005-07-25
  • 打赏
  • 举报
回复
不是ExitWindowsEx函数的问题吧?~!!@~!##@%#$%@%#@!$@
AntonlioX 2005-07-25
  • 打赏
  • 举报
回复
mark


up

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧