ShellExecute from a Windows ser vice

A quick question about ShellExecute. Is it possible to open up a GUI
program using ShellExecute from within a Windows service? I’ve tried this
under 2K using the return value from GetDesktopWindow() as the HWND param
to ShellExecute. I see in the TaskManager that the program (in my test
case, notepad.exe) has started, but there is no window for it, nor can I
terminate it from TaskManager. I tried using CreateProcess with
essentially the same result.

I know that service programs have no Windows message “pump”, nor a concept
of windows at all, but is there a way to do what I am trying to do? I
noticed that, in sample console program that successfully fires a visible
instance of NotePad via ShellExecute, the return value from
GetDesktopWindow is different than when called from my service program.

In the ultimate case of XP Fast User Switching, I’d like the GUI program
thus opened to appear on the currently active desktop.

TIA,
Bill

Make sure your service has ‘allow service to interact with the desktop’
checked. If you go to the services list, right click on your service and go
to the ‘Log On’ tab you should see the check box.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of xxxxx@endwell.net
Sent: Tuesday, July 29, 2003 12:13 PM
To: Windows System Software Developers Interest List
Subject: [ntdev] ShellExecute from a Windows ser vice

A quick question about ShellExecute. Is it possible to open up a GUI
program using ShellExecute from within a Windows service? I’ve tried this
under 2K using the return value from GetDesktopWindow() as the HWND param
to ShellExecute. I see in the TaskManager that the program (in my test
case, notepad.exe) has started, but there is no window for it, nor can I
terminate it from TaskManager. I tried using CreateProcess with
essentially the same result.

I know that service programs have no Windows message “pump”, nor a concept
of windows at all, but is there a way to do what I am trying to do? I
noticed that, in sample console program that successfully fires a visible
instance of NotePad via ShellExecute, the return value from
GetDesktopWindow is different than when called from my service program.

In the ultimate case of XP Fast User Switching, I’d like the GUI program
thus opened to appear on the currently active desktop.

TIA,
Bill


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@positivenetworks.net
To unsubscribe send a blank email to xxxxx@lists.osr.com

> I know that service programs have no Windows message “pump”, nor a concept

of windows at all, but is there a way to do what I am trying to do? I

A bit more complex. Services can call windowing APIs, but they have the
invisible virtual window station which is not connected to any video hardware
and to any input.

There was a sample on how to switch to other WinSta\Desktop from the service,
and then start the GUI app.

Anyway I would suggest replacing the service with the app registered in Run
registry or Startup folder. It will surely run on the correct winsta.

Max

Almost always with a answer Max, thanks!

There was a sample on how to switch to other WinSta\Desktop from the service,
and then start the GUI app.

Where is that sample found? I haven’t run across it.

Anyway I would suggest replacing the service with the app registered in Run
registry or Startup folder. It will surely run on the correct winsta.

The system I am working on requires the configuration as I’ve described
it, so I really need the service to be able to access the current, active,
“real” desktop.

Bill

Why not have both? Have the service running and have an agent EXE (a
copy of it is running in each session) that communicates with the
service (through RPC for instance). The agent displays all UI and
handles all of the windowing.

This way you don’t open yourself up to an elevation of privilege attack
on the service (which is what the shatter attack is).

D

This posting is provided “AS IS” with no warranties, and confers no
rights

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@endwell.net
Sent: Tuesday, July 29, 2003 10:46 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] Re: ShellExecute from a Windows ser vice

Almost always with a answer Max, thanks!

There was a sample on how to switch to other WinSta\Desktop from the
service,
and then start the GUI app.

Where is that sample found? I haven’t run across it.

Anyway I would suggest replacing the service with the app registered
in Run
registry or Startup folder. It will surely run on the correct winsta.

The system I am working on requires the configuration as I’ve described
it, so I really need the service to be able to access the current,
active,
“real” desktop.

Bill


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@windows.microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

> Make sure your service has ‘allow service to interact with the desktop’

checked. If you go to the services list, right click on your service and go
to the ‘Log On’ tab you should see the check box.

Thanks for the clue… I missed the SERVICE_INTERACTIVE_PROCESS flag when
setting up my service. That did the trick…

> Where is that sample found? I haven’t run across it.

"
Interacting with the User in a Service
" in MSDN Library. Replace MessageBox with CreateProcess.

The system I am working on requires the configuration as I’ve described
it, so I really need the service to be able to access the current, active,
“real” desktop.

What if there is no logged on user?

Max

This is a terrible flag to use. There are numerous side-effects of
allowing a service to interact with the desktop. If anyone wants more
information on this, let me know and I will dig through some of my
notes.

And for God’s sake, do not use MFC in the service!

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@endwell.net
Sent: Tuesday, July 29, 2003 11:06 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] RE: ShellExecute from a Windows ser vice

Make sure your service has ‘allow service to interact with the
desktop’
checked. If you go to the services list, right click on your service
and go
to the ‘Log On’ tab you should see the check box.

Thanks for the clue… I missed the SERVICE_INTERACTIVE_PROCESS flag
when
setting up my service. That did the trick…


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The point where it all comes crashing down (GUI in a system service) in
when your app has to start working with Terminal Services. Can we say
three-month rewrite anybody?

Jamey Kirby wrote:

This is a terrible flag to use. There are numerous side-effects of
allowing a service to interact with the desktop. If anyone wants more
information on this, let me know and I will dig through some of my
notes.

And for God’s sake, do not use MFC in the service!

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@endwell.net
Sent: Tuesday, July 29, 2003 11:06 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] RE: ShellExecute from a Windows ser vice

>Make sure your service has 'allow service to interact with the

desktop’

>checked. If you go to the services list, right click on your service

and go

>to the ‘Log On’ tab you should see the check box.
>

Thanks for the clue… I missed the SERVICE_INTERACTIVE_PROCESS flag
when
setting up my service. That did the trick…


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@nryan.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

  • Nick Ryan (MVP for DDK)

> And for God’s sake, do not use MFC in the service!

Why not? CString and such can be used in service without any problems, so is
MFC’s COM/OA support (which is long ago superceded with ATL).

I only remember that there were problems in the service (and in any code
starting with main() and not WinMain) with MFC’s resource access stuff. I mean
the resources in the binary’s .rsrc sections like string tables.

To bypass it, I used some hack to call some MFC stuff explicitly which is
usually called implicitly, and for now, I do not remember what it was -
AfxSetResourceHandle or something like.

Max

BTW: You can still pop up message boxes from services running in
non-interactive window stations by or’ing the MB_SERVICE_NOTIFICATION
flag with your MB_OK (or whatever other flags you specify).

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Tuesday, July 29, 2003 11:54 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] RE: ShellExecute from a Windows ser vice

And for God’s sake, do not use MFC in the service!

Why not? CString and such can be used in service without any problems,
so is
MFC’s COM/OA support (which is long ago superceded with ATL).

I only remember that there were problems in the service (and in any code
starting with main() and not WinMain) with MFC’s resource access stuff.
I mean
the resources in the binary’s .rsrc sections like string tables.

To bypass it, I used some hack to call some MFC stuff explicitly which
is
usually called implicitly, and for now, I do not remember what it was -
AfxSetResourceHandle or something like.

Max


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

It has been a while since I have done any serious service work, so I
checked with my friend who knows a lot in this area.

Here are his notes:

***** Snip

Here it goes in a fairly short version.

a) Services generally run in a different window station than the
interactive desktop and therefore cannot produce any GUI that is visible
or accept any input (like keyboard, mouse, etc.). If a service needs
some interaction then you can configure it to run in the interactive
window station (WinSta0), but you MUST be aware of some problems with
doing that. Some of them are as follows:

a1. The global atom table in WinSta0 gets re-initialized when users log
off and back on. This causes all sorts of problems for services that
rely on this information staying stable. An example is using named
pipes. If you use named pipes and the global atom table gets
re-initialized you will die a horrible death!

a2. If you run in WinSta0 you will only have Local System permissions
because you cannot specify the user account to run the service under.
This is great on the local machine, but you get NULL permissions when
dealing with remote machines.

a3. I know there are a lot more, but I haven’t worked on a service in a
while and just can’t think of the other issues.

This doesn’t mean that your service should not be designed this way. It
just means that you should keep all of this in mind and only put GUI in
your service if the service will work with these limitations.

BTW: You can still pop up message boxes from services running in
non-interactive window stations by or’ing the MB_SERVICE_NOTIFICATION
flag with your MB_OK (or whatever other flags you specify).

b) Microsoft has fixed the show stoppers for using MFC in your
services. However, I would highly recommend installing at least SP3 of
Visual Studio. Prior to that you would get all kind of exceptions being
thrown when they would dereference your CWinApp object (which you don’t
have!). Unhandled exceptions are really bad in services because you
never see them and can never dismiss them.

Here are some additional tips that might make your life easier (since
you are already familiar with SoftIce). If you are writing a C or C++
service, put ALL of you code in a separate DLL and the only code you
would put in the service exe would be the interface functions to the
SCM. Have them do nothing more than calling into the DLL to do the
work. This makes debugging the service much easier because it doesn’t
start as a standard process. Instead it is started by the SCM, which
means that you can’t run it directly from the debugger. By putting the
main code in a DLL you can load the DLL into SoftIce (using IcePack if
you need to test it at boot time) and then set your breakpoints and tell
the SCM to start the service. You will thank me later for this one!!!

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Tuesday, July 29, 2003 11:54 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] RE: ShellExecute from a Windows ser vice

And for God’s sake, do not use MFC in the service!

Why not? CString and such can be used in service without any problems,
so is
MFC’s COM/OA support (which is long ago superceded with ATL).

I only remember that there were problems in the service (and in any code
starting with main() and not WinMain) with MFC’s resource access stuff.
I mean
the resources in the binary’s .rsrc sections like string tables.

To bypass it, I used some hack to call some MFC stuff explicitly which
is
usually called implicitly, and for now, I do not remember what it was -
AfxSetResourceHandle or something like.

Max


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

> Here are some additional tips that might make your life easier (since

you are already familiar with SoftIce). If you are writing a C or C++
service, put ALL of you code in a separate DLL and the only code you
would put in the service exe would be the interface functions to the
SCM. Have them do nothing more than calling into the DLL to do the

Another way of success:

  • write a service to be launchable both from the command line and from SC.
  • debug as command-line app.
  • in command-line mode, print debug output to stdout, together with
    OutputDebugString.

MSSQLServer was written this way, BTW.

One can use both WinDbg and the MSVC’s debugger to debug the service, both by
attaching to the running process and by starting in a debugger (in command-line
mode).

Max