On Jan 21, 2011, at 1:45 PM, Richard Hipp wrote:

> But I did not accept that branch onto the trunk.  I want to do the "hooks" 
> via a different mechanism.  But in order to implement this different 
> mechanism, I need example C code for launching a background process in 
> windows that is not associated with an console.  Doing that is easy in Unix, 
> but I'm not sure how to do it in windows.
> 
> This background process would be responsible for waiting until the push/pull 
> finished and then running whatever hooks are required.

I haven't programmed on Windows for ages, and maybe it's not what you want, but 
here's some code I tried.
Basically, there are two interesting creation flags for CreateProcess: 
CREATE_NEW_CONSOLE and DETACHED_PROCESS. The former opens a new DOS console for 
a child process, the latter doesn't. DETACHED_PROCESS seems like what you want, 
but I cannot make it write into the same console from where the parent process 
launched.

The following is an example with modified win32_create_child_process from 
popen.c. When you launch it, parent process prints "Hello from parent", creates 
a child process and exits. Child continues running even if you close "DOS" 
window. It will write "Hello, I'm a child process, writing #[number of write]" 
to file C:\process-test.txt every second until you close it with Task manager.


#include <windows.h>
#include <fcntl.h>
#include <stdio.h>

static int win32_create_child_process(
 char *zCmd,          /* The command that the child process will run */
 HANDLE hIn,          /* Standard input */
 HANDLE hOut,         /* Standard output */
 HANDLE hErr,         /* Standard error */
 DWORD *pChildPid     /* OUT: Child process handle */
){
 STARTUPINFO si;
 PROCESS_INFORMATION pi;
 BOOL rc;

 memset(&si, 0, sizeof(si));
 si.cb = sizeof(si);
 si.dwFlags = STARTF_USESTDHANDLES;
 SetHandleInformation(hIn, HANDLE_FLAG_INHERIT, TRUE);
 si.hStdInput  = hIn;
 SetHandleInformation(hOut, HANDLE_FLAG_INHERIT, TRUE);
 si.hStdOutput = hOut;
 SetHandleInformation(hErr, HANDLE_FLAG_INHERIT, TRUE);
 si.hStdError  = hErr;
 rc = CreateProcess(
    NULL,  /* Application Name */
    zCmd,  /* Command-line */
    NULL,  /* Process attributes */
    NULL,  /* Thread attributes */
    TRUE,  /* Inherit Handles */
    DETACHED_PROCESS,     /* Create flags  */
    NULL,  /* Environment */
    NULL,  /* Current directory */
    &si,   /* Startup Info */
    &pi    /* Process Info */
 );
 if( rc ){
   CloseHandle( pi.hProcess );
   CloseHandle( pi.hThread );
   *pChildPid = pi.dwProcessId;
 }else{
   //win32_fatal_error("cannot create child process");
        printf("cannot create child process: %d", rc);
        exit(0);
 }
 return rc!=0;
}



int main(int argc, const char *argv) {
        if( argc > 1 ){
                int i = 0;
                for(;;){
                        FILE *f = fopen("C:\\process-test.txt", "a");
                        fprintf(f, "Hello, I'm a child process, writing #%d\n", 
i++);
                        fclose(f);
                        Sleep(1000);
                }
        }else{
           DWORD pChildPid;
           printf("Hello from parent\n");
           win32_create_child_process("a.exe child", 
GetStdHandle(STD_INPUT_HANDLE),
             GetStdHandle(STD_OUTPUT_HANDLE), GetStdHandle(STD_ERROR_HANDLE),
&pChildPid);
        }
}


--
Dmitry Chestnykh

_______________________________________________
fossil-users mailing list
fossil-users@lists.fossil-scm.org
http://lists.fossil-scm.org:8080/cgi-bin/mailman/listinfo/fossil-users

Reply via email to