Microsoft Windows services enable you to create long-running executable applications that run in their own Windows sessions. Windows services basically do not have any user interface. So you cannot directly pop up any message or launch any form through your windows service. However, if at all there is any need for the UI like notify icon or any messages, you need to go to the “Service Control Manager” (just type “services.msc” in the Run window to see the SCM), go to properties of your service, and check the “Allow service to interact with desktop”. |
But what if you want to do the same programmatically at the time of installation of your service.
To know the answer you first need to know how the service is installed in windows. Whenever a service is installed, it is added to the SCM and a registry entry is made for the service. To see the entry go to the pathHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services in the registry editor. Now for each service installed in the system, you have an entry over here. The entry “Type”contains the attributes of the service. It is in HEX. The third bit here represents the “Allow service to interact with desktop”. To make your service interactive you just have to modify the entry “Type”. Change its third bit to 1 and your service interacts with desktop.
‘Open the registry key of Your service Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey(“SYSTEM\CurrentControlSet\Services\YourServiceName”, True)
If Not regKey Is Nothing Then If ckey.GetValue(“Type”) IsNot Nothing Then regKey.SetValue(“Type”, (CType(regkey.GetValue(“Type”),Integer) Or 256)) So, this will make your service desktop interactive. However, you must try not to make your service desktop interactive because this is not what they are meant to do. Instead you can make a win form application and make it interact with your service.
NOTE: When dealing with registry proceed with caution as it may result in serious issues related to windows or application. Its better to create a system restore point before making any changes to registry.
|