We help IT Professionals succeed at work.

Enable windows service to interact with desktop

YZlat
YZlat used Ask the Experts™
on
What's the best way to ebanle windows service to interact with desktop?

I need to do it programmatically. My windows service needs to open a windows application that has an inteface and the interface needs to be visible to the user
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Inside the provperties of a service where you enter the account under which the service runs, is an option "allow service to interact with desktop".
This will hovewer only work with the local system account.

Since this is a registry option that is set for this service, I guess you should be able to set it when installing the service. Use regmon or something to find the specific registry key.

Author

Commented:
do you know which registry key do I need to set?
Commented:
Google is your friend ;-):
Source:  http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/cc4c3a4b-0b62-497c-8d66-60147946e114

There's a registry key that can set this for you.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\(servicename)\Type

It's a bit flags field.  0x100 means "interact with desktop", so you'll have to merge that with the value currently in the field after installation of the service.  The following code should do it for you.  Simply change the serviceName, and make sure the user who installs the app has permissions to edit the registry.

string serviceName = "UwaService";

 

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + serviceName + @"\", true);

 

if (key != null)

{

    // get the value of the Type key.

    object obj = key.GetValue("Type");

 

    if (obj != null && obj is int)

    {

        int value = (int)obj;

 

        // set the flag.

        value |= 0x100;

 

        // set the value back.

        key.SetValue("Type", value);

    }

}