Link to home
Start Free TrialLog in
Avatar of Nosfedra
Nosfedra

asked on

Spawn Process from Service

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
Avatar of jkr
jkr
Flag of Germany image

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
Avatar of Nosfedra
Nosfedra

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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