Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm trying to open an application(with it's GUI) thorough the windows service. It's not throwing any exception or error. Its not opening the application. When I executed the same code with windows Forms app the applicaiton is opening. Why the windows service is not opening any other application ?

In my code I tried to check if "AnyDesk" application is installed or not. If installed I'm trying to open it. But its not opening the app.

When I checked in "task manager" it's showing in "background process" apps list. But the User Interface is not opening. How can I make it to open ?

My code is like below :

What I have tried:

C#
protected override void OnStart(string[] args)
    {
        try
        {
            bool isinstalled = checkInstalled("AnyDesk");
            if (isinstalled)
            {
                Process.Start(@"C:\Program Files (x86)\AnyDesk\AnyDesk.exe");
            }
        }
        catch (Exception ex)
        {
            this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Information);
        }
    }



    public static bool checkInstalled(string c_name)
    {
        string displayName;

        string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
        if (key != null)
        {
            foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
            {
                displayName = subkey.GetValue("DisplayName") as string;
                if (displayName != null && displayName.Contains(c_name))
                {
                    return true;
                }
            }
            key.Close();
        }

        registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
        key = Registry.LocalMachine.OpenSubKey(registryKey);
        if (key != null)
        {
            foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
            {
                displayName = subkey.GetValue("DisplayName") as string;
                if (displayName != null && displayName.Contains(c_name))
                {
                    return true;
                }
            }
            key.Close();
        }
        return false;
    }
Posted
Updated 26-May-20 21:43pm
v4

You can't open a GUI app from a service at all: the whole idea is that services do not have any interaction with the user (directly or indirectly) and they do not run under a "normal user" account for that very reason - that's why they can open without any user being logged in.

Think about it: if no user is logged in and your service opens a GUI window, what user account is that window running under?

Applications talk to services, services don't start the process.
 
Share this answer
 
Comments
Tech Box To Unbox 27-May-20 2:30am    
Don't know am i wrong, "if no user is logged in" means the system will be in "shut down mode" then the service also won't run right ?
OriginalGriff 27-May-20 2:33am    
You are wrong: Turn your system on and by default it goes to the login screen and waits for your password. No apps are running - because there is no user to run them yet - but services are. That's the whole idea of services: they run without a user in the background.
This may help clear your confusion:

https://stackify.com/what-are-windows-services/
Under Windows XP this was allowed, but not in later Windows versions.
Here is a rather complicated article on how to circumvent the Windows UAC control: Subverting Vista UAC in Both 32 and 64 bit Architectures[^]
But my advice would be to not bother, as newer versions of Windows probably will have improved security and will block any attempts.
 
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