Solved

Spawn Process from Service

Posted on 2004-08-11
4
1,398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

738 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