tags:

views:

29

answers:

2

Hi.

I have a program that runs as a scheduled task. The program runs on XP as SYSTEM. The idea is that the program will run in the background, while USER is active.

I need the program to logoff USER when specific conditions occur.

I tried using:

[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);

but that appears to not log USER off.

I think maybe it's logging SYSTEM off, as it is running as SYSTEM.

How can i logogg USER?

Thanks, SummerBulb.

A: 

It would help if you showed the flags for ExitWindowsEx, but you may need to impersonate the user, though I think this is unlikely. If I remember logging off the current user was enough, but you may have to force the logoff, as it can be cancelled otherwise, if the user hasn't saved some changes for example.

But to impersonate a user you can look at this: http://www.codeproject.com/KB/system/UserImpersonation.aspx

I would start with looking here, and include both the logoff and forceifhung values: http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx

It would be similar to EWX_FORCEIFHUNG | EWX_LOGOFF as the parameter.

UPDATE:

I expect Mike is correct that impersonation won't help here.

James Black
I don't think that impersonation will do it because you will get another instance of the user window station/desktop/session.
Mike
A: 

I think that you will need to run some code as that user. Create an app that runs when a user logs in and then monitors an event. Have your service set the event and then the code will call the ExitWindowsEx method. You will still need to use the forceifhung and logoff params as James mentioned.

Mike
I had a screensaver I wrote that would be launched by a window service, as System, log the user off, but these types of problems are very, very difficult to debug.
James Black