C# – Service with the ServiceNotification option

.net-4.0cmessageboxwindows-services

In Windows Vista and up, services can no longer access the GUI. However, after writing a simple mechanism to pass messages from my service to a helper GUI application, I found the following overload for System.Windows.Forms.MessageBox.Show, where the "MessageBoxOptions" parameter caught my eye:

public static DialogResult Show(
    string text,
    string caption,
    MessageBoxButtons buttons,
    MessageBoxIcon icon,
    MessageBoxDefaultButton defaultButton,
    MessageBoxOptions options
)

Looking at the enum more closely, one of the valid options is MessageBoxOptions.ServiceNotification, which states on MSDN that:

The message box is displayed on the active desktop. The caller is a
service notifying the user of an event. The function displays a
message box on the current active desktop, even if there is no user
logged on to the computer.

Does this mean that by passing the MessageBox.Show() method this option, my service can actually handle displaying a simple message from the service without having to use a helper application in Vista, Windows 7, and up?

Best Answer

MESSAGEBOXOPTIONS ENUM DEMYSTIFIED - Applicable for XP

Starting with Windows Vista and above, user interfaces generated by Windows services can’t be seen. And even worst, your service could be stuck waiting for some user input that the user cannot give as she does not see anything

From here


To display MessageBox in windows service, you need to send WTSSendMessage message. It will show up DialogBox in Session 0 special desktop.