bool setupProcess()

in src/main/cpp/bootstrap/utilsfuncs.cpp [292:360]


bool setupProcess(int &argc, char *argv[], DWORD &parentProcID, const char *attachMsg) {
#define CHECK_ARG \
    if (i+1 == argc) {\
        logErr(false, true, "Argument is missing for \"%s\" option.", argv[i]);\
        return false;\
    }

    parentProcID = 0;
    DWORD cmdLineArgPPID = 0;
    for (int i = 0; i < argc; i++) {
        if (strcmp(ARG_NAME_CONSOLE, argv[i]) == 0) {
            CHECK_ARG;
            if (strcmp("new", argv[i + 1]) == 0){
                AllocConsole();
                setConsoleCodepage();
            } else if (strcmp("suppress", argv[i + 1]) == 0) {
                // nothing, no console should be attached
            } else {
                logErr(false, true, "Invalid argument for \"%s\" option.", argv[i]);
                return false;
            }
            // remove options
            for (int k = i + 2; k < argc; k++) {
                argv[k-2] = argv[k];
            }
            argc -= 2;
            return true;
        } else if (strcmp(ARG_NAME_LA_PPID, argv[i]) == 0) {
            CHECK_ARG;
            char *end = 0;
            cmdLineArgPPID = strtoul(argv[++i], &end, 10);
            if (cmdLineArgPPID == 0 && *end != '\0') {
                logErr(false, true, "Invalid parameter for option %s", ARG_NAME_LA_PPID);
                return false;
            }
            logMsg("Command line arg PPID: %u", cmdLineArgPPID);
            break;
        }
    }
#undef CHECK_ARG

    // default, attach to parent process console if exists
    // AttachConsole exists since WinXP, so be nice and do it dynamically
    typedef BOOL (WINAPI *LPFAC)(DWORD  dwProcessId);
    HINSTANCE hKernel32 = GetModuleHandle("kernel32");
    if (hKernel32) {
        LPFAC attachConsole = (LPFAC) GetProcAddress(hKernel32, "AttachConsole");
        if (attachConsole) {
            if (cmdLineArgPPID) {
                if (!attachConsole(cmdLineArgPPID)) {
                    logErr(true, false, "AttachConsole of PPID: %u failed.", cmdLineArgPPID);
                }
            } else {
                if (!attachConsole((DWORD) -1)) {
                    logErr(true, false, "AttachConsole of PP failed.");
                } else {
                    getParentProcessID(parentProcID);
                    setConsoleCodepage();
                    if (attachMsg) {
                        printToConsole(attachMsg);
                    }
                }
            }
        } else {
            logErr(true, false, "GetProcAddress() for AttachConsole failed.");
        }
    }
    return true;
}