Application behaves differently when launched from a service

Posted on 2004-07-31
Last Modified: 2013-12-03
I have a program that works great when I test the program by running
it as an application, but when I package it as a service, the feature fails.
In particular the feature is a gui part that supports drag-and-drop allowing
interaction with the user's desktop.

I tried having my service launch the gui feature as a separate application,
but the feature still didn't work when the service launched the application.

I tried using ImpersonateLoggedOnUser before launching the application,
but it still failed to work.

What else could be different that causes the application to succeed when I
run it normally, but fail when the service launches it?
Question by:bjornsone
  • 3
  • 3
  • 2
LVL 86

Expert Comment

ID: 11685305
A lot of things can go wrong - see ("Why Do Certain Win32 Technologies Misbehave in Windows NT Services?"). Hsve you tried making your service a SERVICE_INTERACTIVE_PROCESS?

Author Comment

ID: 11686102
Yes, my service is interactive.  I see the user interface and all other gui functionality
works fine with interacting with the logged in user.  I just have problems with drag-
and-drop not working with explorer or other apps.

I already gave up in having my service work just like an application, but I was at least
hoping that I my service could launch an application that worked fine (this method was
suggested in the article that jkr referenced).

I tried using "system( const char *command )" to launch the application after doing
ImpersonateLoggedOnUser.  Is there a better way that I could launch this other app?  
Should I use CreateProcessEx  using the same token that I used with ImpersonateLoggedOnUser?

I can make some guesses, but if anybody knows how a service can launch an application
that behaves just as if the logged on user had launched the app, then I would much
appreciate your help.
LVL 86

Accepted Solution

jkr earned 400 total points
ID: 11686454
That's what I outlined in http:Q_20278299.html



// do stuff



  HANDLE          hToken  =   NULL;

  if  (   !LogonUser  (   ( char*) pszUsername,
                          ( char*) pszPwd,
          return  (   GetLastError    ());

  if  (   !ImpersonateLoggedOnUser    (   hToken)
          return  (   GetLastError    ());

You alternatively could obtain a token from a process the user is running (explorer.exe is a good choice <s>). You'll nedd the PID to call 'OpenProcess()', then 'OpenProcessToken()' with 'TOKEN_IMPERSONATE' access. See;EN-US;q175030 ("HOWTO: Enumerate Applications Using Win32 APIs (Q175030)") on how to get that PID - it comes with sample code.


To obtain a valid token, you could use

HANDLE GetTokenOfLoggedOnUser()
HANDLE hToken;
HANDLE hProcess;
// find PID of 'explorer.exe'
// HOWTO: Enumerate Applications in Win32

  hProcess    =   OpenProcess (   PROCESS_ALL_ACCESS,

   if  (   !OpenProcessToken   (   hProcess,
      )   return  (   INVALID_HANDLE_VALUE);

return ( hToken);

from e.g. http:Q_10223099.html and http:Q_20827986.html

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Author Comment

ID: 11686812
I've already found how to get the proper handle to be used for
Impersonation.  Doing impersonation has worked for my service
doing other tasks.  Impersonation, however, does not seem to solve
the problem when I call "system" to launch my other process after
doing ImpersonateLoggedOnUser.

What is the right way for my code to launch a separate application
so that it behaves as if it had been launched by the logged in user.
My current approach does not work.  Will ShellExecute or CreateProcess
work any better?  Why?
LVL 86

Expert Comment

ID: 11688786
>>What is the right way for my code to launch a separate application

The above worked with 'CreateProcess()'. BTW, what exactly are you using (in terms of technologies) is your application?

Expert Comment

ID: 11758859
Is it because the desktop changes? The desktop is explorer, and explorer is responsible for drag-and-drop of files. If the user logs off, explorer exits. Any handles or interfaces you had might be lost. You need to watch for a certain named-event to be pulsed.

WindowStation0 or something....I can't find it right now but it goes something like this:

hEvtDesktopChanged = CreateEvent(NULL, FALSE, FALSE, "WindowStation0");

Then somewhere in your service, wait for that event. When the desktop changes, windows will pulse the event, causing your waiting thread to wake up. You should then be able to reinitialize your OLE interfaces for drag and drop.


Assisted Solution

adg080898 earned 100 total points
ID: 11758905

Work through that topic and neighbouring topics to get some insight into your problem. It says clearly that you have to "close all handles to the window station and desktop" when the user logs off.

BTW: I think that event name I mentioned in the previous posting is "WinSta0", but still not 100% sure.

Author Comment

ID: 12217534
Sorry for leaving this open for so long.
It looks like I'll just need to run the part that supports drag-and-drop as
an application.   I'll keep the other part that is always needed as a service.
It'll be more work, but it is good separation anyway to not use the
service to directly do the user interface.
Thanks for your help.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question