Sorry for the email necromancy, but since there are talks about post
commit hooks again and as it seems it would generally be a useful
feature, I remembered about the roadblock that Richard encountered
that hadn't received a good anwser as far as I remember:

On Mon, Jan 3, 2011 at 12:58 PM, Richard Hipp <[email protected]> wrote:
>
> I've got a  plan for a better way to do various "hooks", but in order for it
> to work cross-platform, I need code that will start a background process on
> windows.  By "background" process, I mean a process that will continue
> running even after its parent process exits or is killed.  If any windows
> experts out there can explain to me how to do that, or provide sample code,
> that will expedite getting the "hook" logic into the Fossil trunk.

So I looked into the win32 process creation APIs and played with them
on a windows box. It turns out that CreateProcess
(http://msdn.microsoft.com/en-us/library/ms682425%28v=vs.85%29.aspx)
offers a range of options to run a process in a different console
window, without a console, or with its stdin / stdout bound to
arbitrary handles.

If you look in the creation flags you can pass to CreateProcess
(http://msdn.microsoft.com/en-us/library/ms684863(v=vs.85).aspx ), you
can give CREATE_NEW_CONSOLE to create a new console window for the new
process, CREATE_NO_WINDOW to have no window created for it, or
DETACH_PROCESS to just detach the process (which seems different than
CREATE_NO_WINDOW, I think it's for the case where you want to provide
your own file handle for stdin and stdout, which I didn't try).

Those three flags are mutually exclusive.

Below is a quick test program I made that launch the file you give it
on the command line. I used a small .bat file echoing things in a loop
in both the console and a file for testing that I launched with it.

With flags set at zero, predictably the script stopped running when I
closed the console (but kept running after the program ended).

With "CREATE_NEW_CONSOLE", it ran in a new, separate console window
that kept running even when the original console was closed.

With "CREATE_NO_WINDOW", it ran silently (the file kept growing) and
kept running after closing the console (I had to kill it through the
task manager).

With "DETACHED_PROCESS", nothing happened. Obviously the script simply
couldn't run without any form of input/output, but I suspect this is
the option to use when you want to provide your own file handles to
redirect stdin and stdout for the new process (in the STARTUPINFO
structure).


#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
        //ShellExecute( NULL, "open", argv[1], NULL, NULL, 0 );
            STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

        CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        CREATE_NO_WINDOW,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory
        &si,            // Pointer to STARTUPINFO structure
        &pi );           // Pointer to PROCESS_INFORMATION structure

        return 0;
}
_______________________________________________
fossil-users mailing list
[email protected]
http://lists.fossil-scm.org:8080/cgi-bin/mailman/listinfo/fossil-users

Reply via email to