0

We have a service in our project that runs and we need it to get a command on log off. We could send a command via our console, but the console is a separate program which could not be running. It does not necessarily need to stop, but it does need to get a command of some sort. Is there anything with in the service which can catch a Log Off event?

[10/17/12 10:45] - The service is running as Local System.

[10/17/12 12:07] - I added the following method, but I am not getting any output when I log off or onto the computer even though I am successfully writing to the log with this message I will try again rebuilding my solution, but wanted to post this here in case I am doing something wrong.

protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
        WriteToDebugLog(new string[] { "OnSessionChange()",
                                        DateTime.Now.ToLongTimeString() + " - Session change notice received: " + changeDescription.Reason.ToString() + "  Session ID: " + changeDescription.SessionId.ToString(),
                                       "Information" });
        base.OnSessionChange(changeDescription);
}
Blaze Phoenix
  • 859
  • 2
  • 14
  • 33

3 Answers3

1

Unfortunately Services cannot run per-user, they're system wide. You may modify your service to detect when the user log-off but then it won't be started again when the user log-in again. As alternative you may use a second service to monitor log-in activity (using OnSessionChanged as pointed out by @rene) and to start/stop the first service according.

It's a little bit tricky and you have to install a second service, I prefer a more simple solution using scripting.

PowerShell

You can use two PowerShell scripts, this is to start the service:

Start-Service -name "YourServiceName"

This is to stop the service:

Stop-Service -name "YourServiceName"

To configure this scripts to be executed take a look here. In short you have to change policy configuration in HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System!RunUserPSScriptsFirst to run your scripts when needed.

"Old" shell

You're not forced to use PowerShell, to start/stop a service can be done using shell commands:

sc start YourServiceName

and

sc stop YourServiceName

See this post for how to execute a batch file (or a command) at log-off. In short you have to add an entry to HKCU\Software\Policies\Microsoft\Windows\System\Scripts\Logoff for log-off and HKCU\Software\Policies\Microsoft\Windows\System\Scripts\Logon for log-on (but there are many many different places where you can put your scripts for log-on).

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Would it be possible to run a batch command on Log on? (I am assuming it would be a very similar process, and perhaps a good way for us to finish our execution of the entire product). – Blaze Phoenix Oct 17 '12 at 15:31
  • Yes, for logon there are a lot of different places where you can put your scripts. The most similar one is HKCU\Software\Policies\Microsoft\Windows\System\Scripts\Logon (edited answer). – Adriano Repetti Oct 17 '12 at 16:08
  • Okay, I may have to run a small program on logon/off to handle certain events which seems to be the way to handle it. Thanks for the information. I will see if this works before I accept this answer – Blaze Phoenix Oct 17 '12 at 16:19
  • Hey, will HKCU hit all users or only the user who installs the program? – Blaze Phoenix Oct 17 '12 at 18:33
  • HKCU is only for current user (the one who will install the program). You can install for another user using HKU and user's SID. – Adriano Repetti Oct 17 '12 at 18:42
  • Okay, so what this says to me is that I have to have the Registry Entries as part of the installer, and visual studios _may_ have the installer recognize the difference and let this work how I need it to, the only difficult part will be to make sure that the registry points to the install directory (which I should be able to do with [TARGETDIR]). Hopefully this will work. Thanks so much for the help you have given me thus far :D – Blaze Phoenix Oct 17 '12 at 18:56
  • @BlazePhoenix yes! Of course the alternative is to have a second always running service that acts as "monitor" to control the main service. – Adriano Repetti Oct 17 '12 at 19:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18187/discussion-between-blaze-phoenix-and-adriano) – Blaze Phoenix Oct 17 '12 at 19:51
0

Would adding a group policy help? If you're on Windows 7, click start, write "group policy" and click the "Edit group policy" choice. from there, go to User Configuration > Windows Settings > Scripts (Logon / Logoff)

In here you can add a script that starts or stops your service

default
  • 11,485
  • 9
  • 66
  • 102
0

You can let your service work all the time and check if specific user is logged in, look on this SO answer how to check logged in users

https://stackoverflow.com/a/7065660/351383

Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102