Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Application behaves differently when launched from a service

Posted on 2004-07-31
Medium Priority
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 http://www.microsoft.com/msj/0398/service2.aspx ("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 1600 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 http://support.microsoft.com/default.aspx?scid=kb;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
// http://support.microsoft.com/support/kb/articles/Q175/0/30.ASP

  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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


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 400 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

580 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