• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1407
  • Last Modified:

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
0
Nosfedra
Asked:
Nosfedra
  • 2
  • 2
1 Solution
 
jkrCommented:
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
 
NosfedraAuthor Commented:
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
 
jkrCommented:
>>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
 
NosfedraAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now