Windows Service C#

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hello!

I am writing an Windows service in C#. This service will run under the
system account. What I don't know how to do it is to log to the
eventlog which user stopped the service if somebody stops it. Any idea
how to do this?

Thank you
Frank
 
Override the OnStop method of the parent class (ServiceBase).

Hello!

I am writing an Windows service in C#. This service will run under the
system account. What I don't know how to do it is to log to the
eventlog which user stopped the service if somebody stops it. Any idea
how to do this?

Thank you
Frank
 
Thank you very much. But I still have one problem. How do I find out
which user stopped the service?

Thank you
Frank
 
Services are controlled by the SCM. So in reality it's the SYSTEM account
that stops the service, however, the user that issues the stop command from
the service applet, is the interactive user account.


Willy.
 
There is nothing directly included in the FCL to get at the interactive
logon user's ID.
However, when running on XP or W2K3, you can use System.Management classes
and WMI's class Win32_logonSession. Note however that it's possible to have
more than one interactive logon session, in that case you can't know which
one issued the stop.
When running on anything else, it's really hard to get at it, basically
because you run you service in a restricted privilege context and you'll
have to PInvoke some Win32 API's.
You could try to PInvoke GetTokenInformation using explorer.exe process
handle you can obtain with a call to GetProcessByName.

Willy.
 
No, don't rely on Environment to be loaded from the interactive logon
session when called from a service. If you service run's as LocalSystem,
LocalService or NetworkService the default environment is loaded and
"UserName" doesn't contain the IU account name.

Willy.
 
In the Stop method of the Service just use the following code

using System.Diagnostics;
....
EventLog ev=bew EventLog("Application");
ev.Source="Application";
ev.WriteEntry("This user has stopped the service " + currentuser);

You will have to provide some more info on how you are allowing only
roles to access you service.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top