Is there a programmatic way to detect if the process can interact with desktop
I have an executable that can run normally or as service. At the startup it may show an error message if there is an error (using MessageBox
api). This can cause failure if the application is running as service but not allowed to interact with desktop.The process开发者_JS百科 may appear to be hanging. Is there a programmatic way to detect if the application can interact with desktop ? I can then use some other error notification mechanism (log file etc)
You could specify MB_SERVICE_NOTIFICATION
when calling MessageBox()
, which will display the message in the currently active session.
But, displaying a message box from within a service is not what you really want.
Services are supposed to run in the background and not interact with users directly. One of their characteristics is that they can (and often do) run even when no user is logged on. In Windows Vista they even run in a totally different session than all other users'. Displaying a MessageBox could result in your services being blocked, because the message was invoked in your service's own session without the user knowing about it.
Writing to some sort of a log file or using windows' EventLog is the prefered method.
From MSDN:
To determine whether a service is running as an interactive service, call the GetProcessWindowStation function to retrieve a handle to the window station, and the GetUserObjectInformation function to test whether the window station has the WSF_VISIBLE attribute.
In .NET you can use Environment.UserInteractive
The UserInteractive property reports false for a Windows process or a service like IIS that runs without a user interface. If this property is false, do not display modal dialogs or message boxes because there is no graphical user interface for the user to interact with.
From http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx:
To determine whether a service is running as an interactive service, call the GetProcessWindowStation function to retrieve a handle to the window station, and the GetUserObjectInformation function to test whether the window station has the WSF_VISIBLE attribute.
精彩评论