views:

79

answers:

3

I am writing a program that can be loaded by another service (under our control), or by the logged-on user. The program needs to know if the window station is interactive in order to display dialogs. I know GetProcessWindowStation function, but this one returns a handle. Is there a way to find out?

+4  A: 

The interactive window station is always winsta0. So you need to get the window station name to determine it. Here is some pseudo code:

wchar_t buffer[256] = {0};
DWORD length = 0;
GetUserObjectInformation(GetProcessWindowStation(), UOI_NAME, buffer, 256, &length);
if (!lstrcmp(buffer, "winsta0")) {
  // Interactive!
}

From http://msdn.microsoft.com/en-us/library/ms687096(VS.85).aspx:

The interactive window station, Winsta0, is the only window station that can display a user interface or receive user input

Marc-Antoine Ruel
Great answer. One caveat - the window station I got is "WinSta0". The string compare function needs to be lstrcmpi.
Sherwood Hu
A: 

I suggest having the service pass command line parameters that let the program know it was launched by the service and not a user.

Thomas Matthews
A: 

Please note that this only works on Windows XP (and then only sometimes) - on Windows Vista and beyond, services run in a separate session from interactive users, so you'll never be able to attach to the console on those OSs.

In addition on Windows XP, your application won't work if there are multiple users on the machine (Fast User Switching) because only the first user is logged onto session 0 (where services run).

You'd be far better off splitting your service into two pieces - the service which does the work and a small piece of code which runs as a task (using the Win32 task scheduler APIs) that runs the UI.

Larry Osterman