3

Possible Duplicate:
How can a WPF application be launched before I logon to Windows?

I have written a application in C#. I want to run my application before logging into Windows OS (after Windows OS pre-loading). How can I do that?

Community
  • 1
  • 1
MaxCoder88
  • 2,374
  • 6
  • 41
  • 59

3 Answers3

6

The only way to do this is to create a Windows Service instead of an application. Since services are not user-mode applications, they are allowed to run even when no user is logged in.

However, this has other caveats. Most developers learn only the above and think that they need to write a Windows Service. This is incorrect: in reality, it is quite rare that you ever actually need to write one of these. As mentioned above, they are not user-mode applications and therefore cannot show any type of user interface. They are designed only to run in the background and for instances where the user does not need to interact with them in any way, other than [rarely] stopping and starting them.

If you already have a regular application with a user interface, then creating a Windows Service is not an option for you. Your application will not port to a service, and you'll be back asking half a dozen questions about seemingly unexplained behavior, system crashes, and the inability to do various things. A service is not a replacement for an application: it's an entirely different product that requires a completely different design methodology.

So what are your options? Well, basically nothing. Windows is a multi-user operating system. There is no way to run a user-mode application without a user logged in. The best thing that you can do is to add your application to the "Startup" folder shared by all user accounts, and then configure the machine to automatically log in a particular user when it starts up. That way, there will never be a time that the computer is running without a user logged in, and therefore without your application running as well.

In order to do this, you'll need to configure Group Policies on the computers, which will require you to have administrative access to them and will not work on computers which you do not own (such as machines that belong to customers). That's actually a good thing, because this is extremely poor design for an application intended for general use.

Ask more questions about setting up Group Policies over on our sister site designed for system administrators, Server Fault.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    While it is true that a service cannot have any kind of user interface *directly*, it can communicate with an application that provides a user interface to it. So depending on the application, it would be possible to extract certain parts of functionality from an application that could be turned into a service that could then communicate via pipes, shared memory, sockets, etc. with the application. – Mark Wilkins Jul 22 '11 at 19:56
4

It would have to be a Service to run before login.

Josh
  • 2,955
  • 1
  • 19
  • 28
  • 2
    This is not the whole answer. Windows Services are **not** applications, and they are subject to some important limitations which are quite often deal-breakers. Among them, services cannot show any type of user interface. In the majority of cases where developers think they need to write a service, *they are mistaken*. – Cody Gray - on strike Jul 22 '11 at 17:27
  • Actually, according to Microsoft, Windows Services are considered Applications. Secondly, his only option is to run a Service because those are the only applications that can run while a user is not logged in. However, they cannot run under a user context. – Josh Jul 22 '11 at 17:32
  • Okay, yes, they are applications in a lot of ways, but ways that are quite irrelevant for the majority of developers. They're not **user-mode** applications; this is all detailed in my answer to the question. Yes, I agree that's the only option in this case, and I didn't say that your answer was "wrong". I only said that it was not the whole answer. You'd be surprised how many questions there are here from people who don't know or understand why they can't show a dialog or a message box from their Windows Service. At some point, someone helpfully misled them with this type of answer. – Cody Gray - on strike Jul 22 '11 at 17:34
  • I see your point, so I'll add to my answer. To be able to have a UI with a service is not possible. However, what you can do is port your UI to another application. Take the code you need to run prior to login and make it a service. Then if the two must work together, you would need to provide a pathway of communication. – Josh Jul 22 '11 at 17:40
1

It need to be a Windows service and the service needs to be installed/configured as "automatic startup"

Yahia
  • 69,653
  • 9
  • 115
  • 144