Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Developers,

Can any body will share how to show message box in Windows Service, i heard that we cannot show message box in Windows Service but using "WTSSendMessage" this we can show the message box but how to do,please help me this.

Thanks & regards
Aravidn G
Posted

You cannot and you should not try to do it. The Windows Service is supposed to keep running even if all users log off, so it can work without having a desktop to show anything.

The approach to UI related to the service is different. You should develop a separate application with should communicate with the service through some IPC mechanism, for example through an IPC channel of remoting or WCF (in this case, self-hosted in your service), but it can be a socket or named pipe based communication. One of the approaches is this: you start such application when some user logs on (there is a registry entry "Run" for this purpose) and try to connect to the service. Use this channel for communications between the service and this UI application, in particular, to show service notifications.

—SA
 
Share this answer
 
C#
[DllImport("wtsapi32.dll", SetLastError = true)]
    static extern bool WTSSendMessage(
                IntPtr hServer,
                [MarshalAs(UnmanagedType.I4)] int SessionId,
                String pTitle,
                [MarshalAs(UnmanagedType.U4)] int TitleLength,
                String pMessage,
                [MarshalAs(UnmanagedType.U4)] int MessageLength,
                [MarshalAs(UnmanagedType.U4)] int Style,
                [MarshalAs(UnmanagedType.U4)] int Timeout,
                [MarshalAs(UnmanagedType.U4)] out int pResponse,
                bool bWait);


C#
bool result = false;
                  String title = "Alert";
                  int tlen = title.Length;
                  String msg = sb.ToString();
                  int mlen = msg.Length;
                  int resp = 7;
                  result = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, title, tlen, msg, mlen, 4, 3, out resp, true);
                  int err = Marshal.GetLastWin32Error();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900