I want to pop up an application using windows services.
Right now the code that I am using is:
System.Diagnostics.Process.Start(#" ");
That code does not pop up the application when I start running the services.
You can not do that. User session interaction has been removed since Windows Vista. The process would be started in the session that the service runs in, which per definition is not desktop interactive.
What you can do is run windowless console-only tools.
Long story: Before Vista all services ran in the same session as the first logged in user ("session 0"). Since this is a security issue, this was changed in Vista, where a dedicated session ("session 0") is active from the start for services. The first user logs into session 1, the next user into session 2 etc.
Due to the separation of these sessions it is no longer possible to create desktop interactive services - there is no desktop session for services.
This also means you can not display message boxes or run desktop interactive tools in session 0 - there's nobody who can see them.
Is that the exact code that you're using?
If so you're not actually giving the process anything to start, just an empty string. You need to pass it the name of the executable.
For example:
System.Diagnostics.Process.Start(#"C:\Windows\System32\notepad.exe");
If this is not the exact code, please post modify the question with the exact code.
I have written a Windows service that allows me to remotely run and stop applications. These applications are run using CreateProcess, and this works for me because most of them only perform backend processing. Recently, I need to run applications that present GUI to the current log in user. How do I code in C++ to allow my service to locate the currently active desktop and run the GUI on it?
Roger Lipscombe's answer, to use WTSEnumerateSessions to find the right desktop, then CreateProcessAsUser to start the application on that desktop (you pass it the handle of the desktop as part of the STARTUPINFO structure) is correct.
However, I would strongly recommend against doing this. In some environments, such as Terminal Server hosts with many active users, determining which desktop is the 'active' one isn't easy, and may not even be possible.
But most importantly, if an application will suddenly appear on a user's desktop, this may very well occur at a bad time (either because the user simply isn't expecting it, or because you're trying to launch the app when the session isn't quite initialized yet, in the process of shutting down, or whatever).
A more conventional approach would be to put a shortcut to a small client app for your service in the global startup group. This app will then launch along with every user session, and can be used start other apps (if so desired) without any juggling of user credentials, sessions and/or desktops.
Also, this shortcut can be moved/disabled by administrators as desired, which will make deployment of your application much easier, since it doesn't deviate from the standards used by other Windows apps...
The short answer is "You don't", as opening a GUI program running under another user context is a security vulnerability commonly known as a Shatter Attack.
Take a look at this MSDN article: Interactive Services. It gives some options for a service to interact with a user.
In short you have these options:
Display a dialog box in the user's session using the WTSSendMessage function.
Create a separate hidden GUI application and use the CreateProcessAsUser function to run the application within the context of the interactive user. Design the GUI application to communicate with the service through some method of interprocess communication (IPC), for example, named pipes. The service communicates with the GUI application to tell it when to display the GUI. The application communicates the results of the user interaction back to the service so that the service can take the appropriate action. Note that IPC can expose your service interfaces over the network unless you use an appropriate access control list (ACL).
If this service runs on a multiuser system, add the application to the following key so that it is run in each session: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. If the application uses named pipes for IPC, the server can distinguish between multiple user processes by giving each pipe a unique name based on the session ID.
WTSEnumerateSessions and CreateProcessAsUser.
Several people suggested WTSEnumerateSessions and CreateProcessAsUser. I wonder why no one suggested WTSGetActiveConsoleSessionId, since you said you only want to target one logged in user.
Several people sure are right to suggest CreateProcessAsUser though. If you call plain old CreateProcess the way you said, then the application's GUI will run with your service's privileges instead of the user's privileges.
That problems Session 0 , Interactive Services ,
Windows Service Allow Service To Interact With Desktop
on Windows 7 or Windows Vista
You can read this article
http://www.codeproject.com/KB/vista-security/SubvertingVistaUAC.aspx
I try explained here it's working on Windows 7
On Win2K, XP and Win2K3 the console user is logged on in Session 0, the same session the services live in. If a service is configured as interactive, it'll be able to show the UI on the user's desktop.
However, on Vista, no user can be logged on in Session 0. Showing UI from a service there is a bit trickier. You need to enumerate the active sessions using WTSEnumerateSessions API, find the console session and create the process as that user. Of course, you need also a token or user credentials to be able to do that. You can read more details about this process here.
I think as long as you have only one user logged in, it will automatically display on that user's desktop.
Anyway, be very careful when having a service start an exe.
If the write access to the folder with the exe is not restricted, any user can replace that exe with any other program, which will then be run with sytem rights. Take for example cmd.exe (available on all windows sytems). The next time the service tries to start your exe, you get a command shell with system rights...
If you launch a GUI from your service it will show up on the currently active desktop.
But only if you adjusted the service permissions: You need to allow it to interact with the desktop.
Important Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.
This is taken from : http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
I wrote a C# application using RESTful web services. This application should interact periodically with our server application. I want this program to keep running in the background even if the user logs out of the computer. How can I do this in C#?
If you don't want your application to stop when the user logs out, then the application can't be running in the user's session (really a WinStation). That implies your app needs to run in a different session. The sessions that don't logout are service sessions. Run Task Manager and add the Session ID column, and view all processes, and you'll see what I mean.
So your application needs to run as, or be launched by, a service.
In addition to the first answer don't keep the service running under the specific user account. If you do so then also it won't work if you logged off.
We have got a web application and a windows forms application. The web application contains a download link to download this forms application.
This forms application will check the client machine privileges and drive space etc. and will update the values to a global database using some webservice calls.
So the web application will continuously checking the global database for the status ie how much checking has been completed by the forms application.
So as soon as the download popup appears for the forms application to download, the web application will start to check the status. But if a user cancels the download or if a user close the forms application, then in these cases how the web application can stop checking for status.
But in my case the web application will always check for the status change , even if the client cancels the download or closes the forms application. So how to avoid this?
I think a timeout is your only real option here. Basically the web side will only check for a certain amount of time before giving up. Your status table in your database should include a "last update timestamp" field. If the status is not "complete", and the current time is greater than that timestamp by X seconds, the webserver assumes the process has timed out.
You could try to have some thing like a switch as part of a record or some thing in the Database, so the very next time your web app tries to read lets say the bit field it would know that it should stop checking. You could control this bit field as ON & OFF via your forms application. Then your web application you can setup a polling mechnism that checks the db every so often before timing itself off all together based on the bit field or if not been done then aftera period of time.
Having said that your forms application would need to be able to call the outside world where your db is some where located and update it, it can be done many ways, web service call, http, ect...
Update:
I apologies for the delay in replying but did you understand what I said? If your winforms application can call your web service then it can tell it to store a flag field some where like a record in the db for example that your web-application is polling and checking and then by setting that flag field your web-application would know to stop doing any thing with that record, item.
As you mentioned you are worried about:
User clicking cancel on download:
In this scenario you would not set the checking of your record by your web-application unless the user has downloaded and run your win-forms application for the first time, so dont start checking upon dowload of the win-forms application but start checking once for the first time the user has opened it, you can do this upon start of the winforms application by setting the flag field from your winforms app by calling your web-service. You will need a polling mechanism on top of this every so often, like a service.
When the user closes the winforms application
In this case you would upon termination/close of your winforms app call your web service and set the flag field not to check that record, item any more.
You will need polling in any case as I am thinking you will have many users and hence you will need to monitor the db for incoming messages from your winforms app. Also please be aware as some users are behind firewalls, limited security permissions on their machines and on private networks and your winforms app may not always be able to call your web service.
Hope that helped.
I have created a .NET Windows application in C#. I want to execute this application when the system becomes idle for sometime. The application is a user login application, i.e. when system becomes idle for sometime, a login page appears where the user logins with his username and password. Then only the user can continue using the system.
How can I make it work out? Please help as I am new in Windows applications.
I thought to run the application as a Windows service. I managed to run the application on Windows service start. But how can I trigger the app to run only when system goes to idle in Windows service? I also need to start the timer after the user logins to check idle time again. Please provide some code examples.
The system already comes with such functionality. On the screen saver configuration page you simply check the box titled "On resume, display logon screen". It can be configured with group policy.
There's absolutely no point re-implementing this.
How about:
Start your form hidden
Get the system idle time
Show your window.