Windows Service How To????

Inferno122

Newcomer
Joined
Jul 31, 2003
Messages
6
Location
Philippines
I'm kinda new to vb.net programming and i'm currently getting my feet wet :)

My situation is this: I needed to create a windows service that is user-interactive and I managed to create one. When I start the service, a notify icon appears in the system tray area and the context menus are accessible. Everything works well except for a few problems:

1. The service is run by the system automatically,if I start the service manually, it interacts, but when I log-off and log-on again, it doesn't.
2. The service is supposed to ID the user currently logged and I have tried using all variations of retrieving the current user name but the service always returns "Local System" as the current user :(

There is a forum somewhere that I came across telling that the notify icon should be placed in a WinForms project not belonging the service project. I tried doing this by creating a project group but I can't seem to make the service refference the WinForms project :(

Can anybody help me with this??? I only have 2wks to work on this project so please help!!!
 
You always get LocalSystem as the current user because that's
the account specified to run your service in the Log On tab of the service's property page accessed via the Administrative Tools > Services utility app.

You should follow the reco to make the notify icon (w/c is also your service controller app) a separate windows app from the service app itself because Windows Services are not supposed to have UI code in them. You don't have to set a reference to the service project. Use the ServiceController class to control your service from your notify icon app.
 
JABE said:
You always get LocalSystem as the current user because that's
the account specified to run your service in the Log On tab of the service's property page accessed via the Administrative Tools > Services utility app.
.

Is there anyway to change it? I am working on a file monitor to see which user modify files/directory during his/her session.
 
so you mean that the only way I can do it is by creating a standard app and not creating a service? This will be a standard app placed on the statup folder of the start menu instead of a service, right? The problem is this, if this is just a standard app, then any user can simply terminate this task unlike in service, only an administrator can terminate a service :( Any advice?
 
Worrow said:
Is there anyway to change it? I am working on a file monitor to see which user modify files/directory during his/her session.

You can specify a different account in the Account property of the ServiceProcessInstaller class. Of course, you can always change the account after deployment via the Windows Services management console, given that you have the rights to do so.
 
Inferno122 said:
so you mean that the only way I can do it is by creating a standard app and not creating a service? This will be a standard app placed on the statup folder of the start menu instead of a service, right? The problem is this, if this is just a standard app, then any user can simply terminate this task unlike in service, only an administrator can terminate a service :( Any advice?

You will still have your service but the notify icon project (w/c controls the service) will be a separate Windows Forms project. On the admnistrators-only issue, you can inspect the current user's principals during run-time. This link may get you started http://msdn.microsoft.com/library/d...ncipalwindowsprincipalclassisinroletopic.asp.
 
simple service solution

If you have an app running as a service, then you have to be prepared for it to run while no one is even logged into the system, which is why allowing services to interact with the desktop isn't always the best idea (of course, my wallpaper changer would have to be an exception to that rule).

One way to handle this is by having the service sit around doing nothing (maybe some processing every so often -- whatever your service is intended to do) until it receives a request from a user.

So, you would have to override
OnCustomCommand(command as integer)
in your service, which would do whatever processing the user requests.

Then, you can create a separate application to be used by the user (can even put it in the startup folder if it needs to be run every time) which sends these requests to the service via a ServiceController object.

Now, getting information back from the server is a little trickier, since you have to remember that the service is running in the background of ALL the users logged in. Your options that I can think of now are:

1. Use the Registry to pass information - this is fine if values are not updated often and you can synchronize reads/writes easily (which isn't easy)

2. Create a Pipe between the service and your application. I didn't see anything about Pipes in the .NET quick help, but there's support for them in Win32.

3. If the service is already listening on a port (like if you're writing an ftp server) then you could use a TcpListener / TcpClient architecture. You should only use this if the app will eventually be run from a different system. This is also a way of dealing with pipes (creating a pipe to the localhost). This is a very powerful way of handling the situation, especially if you plan your protocol well.


Hope that helps,
Ben
 
Back
Top