Solved

Spawn Process from Service

Posted on 2004-08-11
4
1,390 Views
Last Modified: 2013-12-03
I have a problem spawning processes from a windows service. The problem is not actually starting the process, but showing the console of the process. I can understand that the windows services do not have consoles and do not take any user input and therefore the spawned processes which inherit their console attributes would do the same.

However, I am spawning a separate process using DETACH_PROCESS flag in the CreateProcess function and even if I am calling AllocConsole right at the start of the child process, nothing shows on the screen (I am calling a console-mode application).

Below is the function I am using for starting a certain process:


int PRAPI ExecuteProgram2(const char *program, const char **args, BOOL wait_for_finish)
{      
      char                        buf[2048];

      char                        tmp[1024];
      DWORD                        err;

      STARTUPINFO                  si;

      PROCESS_INFORMATION pi;


      char      **ptr = (char **)args;

      
      memset(buf, 0, 2048);

      strcat(buf, program);

      while (ptr && *ptr)
      {
             sprintf(tmp," %s", *ptr);

            strcat(buf, tmp);

            ptr++;
      }

      memset(&si, 0, sizeof(si));

      memset(&pi, 0, sizeof(pi));

      si.cb = sizeof(STARTUPINFO);
      si.lpReserved = NULL;
      si.lpReserved2 = NULL;
      si.cbReserved2 = 0;
      si.dwFlags = STARTF_USESHOWWINDOW;
      si.wShowWindow = SW_SHOW;

      //start log console window
      CreateProcess(
            program,
            (LPTSTR) buf,
            NULL,
            NULL,
            FALSE,
            CREATE_DEFAULT_ERROR_MODE | DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP ,
            NULL,
            NULL,
            &si,
            &pi
            );

      err = GetLastError();

      return PRA_SUCCESS;

}

Please don't mind any buffer overflows or any other bugs, this is a test function.

The program starts but it wont show any console.
Any advice is greatly appreciated.

Thank you,
~~Razvan
0
Comment
Question by:Nosfedra
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11776526
You need to execute that process in the context of the logged on user:

All you need is the access token of a process running in the context of the logged-on user, so 'OpenProcessToken()' will do the job for you, e.g.

(Pseudocode ;-)

HANDLE GetTokenOfLoggedOnUser()
{
HANDLE hToken;
HANDLE hProcess;
DWORD dwPID;
// 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,
                                  TRUE,
                                  dwPID
                              );

   if  (   !OpenProcessToken   (   hProcess,
                                  TOKEN_ALL_ACCESS,
                                   &hToken
                              )
      )   return  (   INVALID_HANDLE_VALUE);

return ( hToken);
}

from e.g. http:Q_10223099.html and http:Q_20827986.html
0
 
LVL 3

Author Comment

by:Nosfedra
ID: 11781667
Thank you for your reply.

I did this - found the Process Access token of the explrer.exe process, set it into a SECURITY_ATTRIUBUTES structure and passed it as the third param to CreateProcess - but the CreateProcess returns the mind-numbing 998, ERROR_NOACCESS - Invalid Access to memory Location.


All the memset(0)  are at their place prior to the call to CreateProcess... Any ideas? Isn't the function trying to refer to a memory location (pointed by the hToekn returned by your function which out of its reach?

~~Razvan
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 11784993
>>found the Process Access token of the explrer.exe process, set it into a SECURITY_ATTRIUBUTES

Err, no - you need to either 'ImpersonateLoggedOnUser()' or 'CreateProcessAsUser()' with that token. With 'CreateProcess()', it won't have any effect.
0
 
LVL 3

Author Comment

by:Nosfedra
ID: 11785270
Should I have guessed it?

Thank you anyway. One thing that solved my problem (I really don't need anything else right now) was setting the "Allow Service to Interact with Desktop" checkbox from services manager.

But I will try your sollution as well, thank you. I am awarding you the points.

~~Razvan
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now