win7服务程序中如何实现注销
win7服务程序中怎么实现注销?
我在win7服务程序中执行:
ExitWindowsEx(EWX_LOGOFF,0);
结果没有任何反应, 服务程序还继续正常运行.
(在XP下服务程序可以注销成功)
但在正常应用程序中该语句可以正常注销.
win7服务程序中怎么实现注销?
------解决思路----------------------
ExitWindowsEx Function
Logs off the interactive user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated.
BOOL WINAPI ExitWindowsEx(
__in UINT uFlags,
__in DWORD dwReason
);
Parameters
uFlags
The shutdown type. This parameter must include one of the following values.
Value Meaning
EWX_LOGOFF
0
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
0x00000008
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
0x00000002
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_RESTARTAPPS
0x00000040
Shuts down the system and then restarts it, as well as any applications that have been registered for restart using the RegisterApplicationRestart function. These application receive the WM_QUERYENDSESSION message with lParam set to the ENDSESSION_CLOSEAPP value. For more information, see Guidelines for Applications.
EWX_SHUTDOWN
0x00000001
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 one of the following values.
Value Meaning
EWX_FORCE
0x00000004
This flag has no effect if terminal services is enabled. Otherwise, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_FORCEIFHUNG
0x00000010
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.
Windows NT and Windows Me/98/95: This value is not supported.
dwReason
The 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/NT and Windows Me/98/95: This parameter is ignored.
Return Value
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, running applications are allowed a specific amount of time to respond to the shutdown request. If this time expires before all applications have stopped, the system displays a user interface that allows the user to forcibly shut down the system or to cancel the shutdown request. If the EWX_FORCE value is specified, the system forces running applications to stop when the time expires.
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.
Example Code
For an example, see How to Shut Down the System.
Requirements
Client
Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server
Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header
Declared in Winuser.h; include Windows.h.
Library
Use User32.lib.
DLL
Requires User32.dll.
See Also
Logging Off
Shutting Down
System Shutdown Functions
AdjustTokenPrivileges
HandlerRoutine
SetProcessShutdownParameters
Send comments about this topic to Microsoft
Build date: 8/10/2007
所以此服务需要设置“允许服务与桌面交互”
------解决思路----------------------
允许服务与桌面交互! 就是这个,想办法吧。
------解决思路----------------------
楼主的细心程度满格的时候不是100%啊!
dwServiceType
A set of bit flags that specify the type of service. You must specify one of the following service types. Value Meaning
SERVICE_WIN32_OWN_PROCESS Specifies a Win32-based service that runs in its own process.
SERVICE_WIN32_SHARE_PROCESS Specifies a Win32-based service that shares a process with other services.
SERVICE_KERNEL_DRIVER Specifies a driver service.
SERVICE_FILE_SYSTEM_DRIVER Specifies a file system driver service.
If you specify either SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS, you can also specify the following flag. Value Meaning
SERVICE_INTERACTIVE_PROCESS Enables a Win32-based service process to interact with the desktop.
------解决思路----------------------
要么修改service为交互属性,不然就把这个交给一个普通应用程序来处理
------解决思路----------------------
服务与桌面交互在Win7和xp中完全不是一回事,Win7中有完善的会话隔离机制,服务程序无法简单的弹出一个窗口同用户交互了。
要想实现在服务中注销的话:
方案一:研究一下任务管理器怎么做的,任务管理器“用户”标签页中可以右击任意一个会话并注销。相关函数:WTSEnumerateSessions WTSLogoffSession
方案二:创建指定会话上的进程来代替服务程序注销。相关函数:CreateProcessAsUser
方案三:部署时既有服务程序,又有用户态程序,用户态程序同服务程序通信,在需要的时候由用户态程序执行注销
我在win7服务程序中执行:
ExitWindowsEx(EWX_LOGOFF,0);
结果没有任何反应, 服务程序还继续正常运行.
(在XP下服务程序可以注销成功)
但在正常应用程序中该语句可以正常注销.
win7服务程序中怎么实现注销?
------解决思路----------------------
ExitWindowsEx Function
Logs off the interactive user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated.
BOOL WINAPI ExitWindowsEx(
__in UINT uFlags,
__in DWORD dwReason
);
Parameters
uFlags
The shutdown type. This parameter must include one of the following values.
Value Meaning
EWX_LOGOFF
0
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
0x00000008
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
0x00000002
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_RESTARTAPPS
0x00000040
Shuts down the system and then restarts it, as well as any applications that have been registered for restart using the RegisterApplicationRestart function. These application receive the WM_QUERYENDSESSION message with lParam set to the ENDSESSION_CLOSEAPP value. For more information, see Guidelines for Applications.
EWX_SHUTDOWN
0x00000001
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 one of the following values.
Value Meaning
EWX_FORCE
0x00000004
This flag has no effect if terminal services is enabled. Otherwise, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_FORCEIFHUNG
0x00000010
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.
Windows NT and Windows Me/98/95: This value is not supported.
dwReason
The 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/NT and Windows Me/98/95: This parameter is ignored.
Return Value
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, running applications are allowed a specific amount of time to respond to the shutdown request. If this time expires before all applications have stopped, the system displays a user interface that allows the user to forcibly shut down the system or to cancel the shutdown request. If the EWX_FORCE value is specified, the system forces running applications to stop when the time expires.
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.
Example Code
For an example, see How to Shut Down the System.
Requirements
Client
Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server
Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header
Declared in Winuser.h; include Windows.h.
Library
Use User32.lib.
DLL
Requires User32.dll.
See Also
Logging Off
Shutting Down
System Shutdown Functions
AdjustTokenPrivileges
HandlerRoutine
SetProcessShutdownParameters
Send comments about this topic to Microsoft
Build date: 8/10/2007
所以此服务需要设置“允许服务与桌面交互”
------解决思路----------------------
允许服务与桌面交互! 就是这个,想办法吧。
------解决思路----------------------
dwServiceType
A set of bit flags that specify the type of service. You must specify one of the following service types. Value Meaning
SERVICE_WIN32_OWN_PROCESS Specifies a Win32-based service that runs in its own process.
SERVICE_WIN32_SHARE_PROCESS Specifies a Win32-based service that shares a process with other services.
SERVICE_KERNEL_DRIVER Specifies a driver service.
SERVICE_FILE_SYSTEM_DRIVER Specifies a file system driver service.
If you specify either SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS, you can also specify the following flag. Value Meaning
SERVICE_INTERACTIVE_PROCESS Enables a Win32-based service process to interact with the desktop.
------解决思路----------------------
要么修改service为交互属性,不然就把这个交给一个普通应用程序来处理
------解决思路----------------------
服务与桌面交互在Win7和xp中完全不是一回事,Win7中有完善的会话隔离机制,服务程序无法简单的弹出一个窗口同用户交互了。
要想实现在服务中注销的话:
方案一:研究一下任务管理器怎么做的,任务管理器“用户”标签页中可以右击任意一个会话并注销。相关函数:WTSEnumerateSessions WTSLogoffSession
方案二:创建指定会话上的进程来代替服务程序注销。相关函数:CreateProcessAsUser
方案三:部署时既有服务程序,又有用户态程序,用户态程序同服务程序通信,在需要的时候由用户态程序执行注销