?
Solved

Spawn Process from Service

Posted on 2004-08-11
4
Medium Priority
?
1,402 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 750 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

801 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