Run exe with UI from installed Window service - c#

I am trying to run an Exe on start of window service . In development mode it triggers exe and UI is visible once triggered . But after windows service installed , i am not seeing the UI but exe is running as background process. How to make UI visible for the tiggered exe from windows service.

Services do not display a user interface directly due to Session 0 isolation. So you'll need to rethink with a client-server-style separation of service processing from user interface.
The user-mode UI process will start when the user logs in and remain largely idle/hidden. It will establish an inter-process communication connection to the service and await orders in client-server fashion. If the server wants to "push" some kind of an alert to the client, it does so by notifying the user-mode application to display the user interface on its behalf.

Related

Communication between service and application And also to trigger the application with GUI

I have developed an application which does periodical jobs. It runs as windows service in background and manages jobs. I want to trigger another application with GUI from service application when any error notification needs to be given to user. So after seeing err message user will give some response. So now service application (running as service) should do some work based on user response in GUI application (with interface). User response should be communicated to the service application.
So my requirement is
Application running as service should be able to trigger another application with GUI (its sufficient if triggering happens only when users are logged in)
Communication between an application running as service and application with GUI.
I saw many links about communication between service and application. Named pipes requires .net 3.5 and above, but I want my application to work in .net 2 alone. Writing to a txt file and reading it is last option for me as it is not the best method.
So how it can be done? Please give me ideas to start with.

How can I develop an agent to notify user

I would like to develop a program (an agent) that will run on the user's desktop. It has to communicate with a web service periodically then if a state changes it will notify the user in a notification balloon.
Can I do a winforms or wpf project? (that will both communicate with the web service and notify the user)
Or should I do separately a windows service that will firstly communicate with the web service and then communicate with another standalone winforms app that will notify the user?
Thanks.
Periodically fetching status through service
You can implement an executable(Console) application which should have the logic to invoke the web service to fetch the status. You can use windows task scheduler to execute this exe periodically.
There are many task scheduler wrapper libraries are available, Use could use any of them based on your need.
Task Scheduler Managed Wrapper
Quartz Job Scheduler
Notifying user
If the status is changed then you can notify the user through any User interface, this can be WinForm or WPF application. When the exe which is initiated by the task scheduler finds a status change, it will execute the UI application that you have created to notify the user.
If you need only a single Form to notify the user then Windows Form should be enough unless you want to show any fancy UI using WPF.
Why Windows Service might not be a good choice
It's actually easy to use a windows service to periodically call the web service instead of using a Task scheduler, but the problem you will face with the windows service is that it can't easily communicate with user's desktop, that means it can't directly show/execute any User interface application on user desktop. You will end up writing a IPC(Inter Process Communication) mechanism to communicate with any UI application from windows service.

Window is not visible when started from a Windows Service

There is a windows service that monitors a given infrastructure.
Under some conditions there is a need to pop up a form where the user see some indicators and able to do some choice.
For this purpose I am staring a WPF Window
var thread = new Thread(() =>
{
var w = new MyWindow();
_uis.Add(w);
w.Info = sb.ToString();
w.Show();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
and unfortunatelly cannot see the window.
When I am hosting my service busines logic in a console application I can see the window.
Where is the trick?
Services do not have access to the same privileges (Network Service/Local Service) and do not have access to your desktop.
Services are meant to be silent in nature by design and simply do not have the ability to launch windows.
A console application does have this ability, console applications are not windows services and of course do have the ability to interact with a user.
You could split the functionality:
service to do the monitoring
client application to display signals from the service
The client app could simply listen to a socket for messages from the service that should be displayed to the user.
The service will start up when the machines starts. The app will start with every user who logs in.
There is no trick. Don't do it
This approach is against well established practices and patterns regarding services and UI. You should not attempt to show a UI (or an interactive UI) directly from a windows service.
The standard practice is to write a separate UI application that communicates with your windows service.
You can consider periodically updating the UI
Webservices can be used to communicate between the service and the UI application
The hard way
However if you really want to do what you originally asked, here you go
http://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-32-and-64-bit-Archite

How can I get the MainWindowHandle of a Windows 7 application running as user <foo> from within a service running as Local System?

I've created a service that runs as the Local System user. This service launches and monitors a Silverlight Out-of-browser application using native interop and the CreateProcessAsUser() method (to run it as the currently logged-in user, rather than Local System). I'm able to get a handle on the spawned Process and do things like Kill() it, however, I've become aware that the service is unable to get a handle to the main window of the child application because the child application is running as a different user. I'm running on Windows 7.
My end goal is to respond properly to when the Process stops responding (i.e. Process.Responding == false) so that I can kill the application and restart it. However, Process.Responding requires a handle to the main window of the process (Process.MainWindowHandle, to be exact), however, in this scenario, Process.MainWindowHandle always returns 0.
I'm stumped here. Is there any way for one user to get a window handle to a process running as another user in Win 7?
Thanks in advance for any and all help.
No, that's not possible. Windows Services are completely isolated from user-mode applications for security reasons. If you could get the handle to a window, you could manipulate and otherwise interact with that window, leaving open a huge security vulnerability.
More information is available here:
How can a Windows Service start a process when a Timer event is raised?
How can I run an EXE program from a Windows Service using C#?
windows service (allow service to interact with desktop)
Need suggestion on replacing Windows Service by invisible WinForm Application
Strictly speaking, what you're using the Windows Service for in the first place is bad design. It shouldn't be creating or launching any user-mode processes, or interacting with the user in any way. Remember that services run even when there is no user logged in! Thus, they shouldn't be starting applications.
A better solution is a simple background application, set to launch automatically when the user logs in. This background application could then launch the Silverlight application, monitor its state, and interact with it as necessary, because both would be running under the context of the same local user account. The effect is similar to a service, but without any of the drawbacks of isolation. The easiest way to do this in Visual Studio is to create a WinForms application (or possibly a WPF application, I have less experience in that area) that simply doesn't show any forms/windows.

Starting process programatically in C# from windows service, UI does not show up i see the process in task manager

I am trying to start a process programatically from windows service written in C#. i see my process having started in the task manager but i dont see its UI.
Any idea whats going on? i am running this on windows xp
clientProcess = new System.Diagnostics.Process();
clientProcess.StartInfo.FileName = system_drive_path + #"\sd\ud\ud.exe";
clientProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
clientProcess.Start();
Services don't have a desktop associated with them, so when you start the GUI application, it will not show up to the user.
You need to configure the service to have interaction with the desktop, then it will work. You can do this either diagrammatically in the service installer class
Or manually in the service properties windows, under the Log On tab.
You are not supposed to interact with the user from a service, including starting a process. This is actually actively disallowed in Vista and later, including services marked as 'interactive', for various reasons primarily security:
Important Services cannot directly
interact with a user as of Windows
Vista.
If you want to interact with the user session then you must have a process in the user session (eg. a tray icon application) that interacts with the service via an IPC protocol (net pipes, shared memory, messages etc).
Create a separate UI application that runs at session start up and that application can start your 'sd.exe' when asked so by the service.

Categories

Resources