On Sun, Jul 23, 2006 at 09:29:28AM +0200, Enrico Forestieri wrote:

> On Sat, Jul 22, 2006 at 10:23:55PM -0500, Bo Peng wrote:
> 
> > Dear list,
> > 
> > Using this small utility, we can hide the console window of lyx by
> > starting lyx with
> > 
> >   runWithHiddenConsole cmd.exe /c lyx.exe
> > 
> > I suggest that we remove the hide console code in os_win32.c if this
> > program is used by the windows installer.
> > 
> > Comments?
> 
> We need to set some environment variables. Please have a look at the
> attached program modified from the same source as yours.
> 
> I use it as follows:
> 
> hidecmd /LC_ALL=it_IT /AIK_DATA_DIR="C:\Programmi\LyX14\aiksaurus" lyx.exe
> 
> I don't see the need to use "cmd /c".

Ehm... here is the attachment.

-- 
Enrico
// Hidecmd starts a program hiding its console window, optionally waiting
// for its termination and setting environment variables.
//
// Usage: hidecmd [/w] [/VAR=val] <filename> [<params>]
// where: /w          wait for program termination
//        /VAR=val    set VAR=val
//        <filename>  executable program
//        <params>    program parameters
//
// Compile: gcc -mno-cygwin -mwindows hidecmd.c -o hidecmd

#include <process.h>
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lCmd, int nCmd)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    char *s;
    char *e;
    int i;
    int err = 0;
    int bWait = 0;
    DWORD exitcode = 0;
    char delim = ' ';
    char *cmdline = GetCommandLine();
    char var[100];
    char val[100];

    if (*cmdline == '\"') {
        delim = '\"';
        cmdline++;
    }

    while (*cmdline != delim && *cmdline != 0)
        cmdline++;

    if (*cmdline == delim)
        cmdline++;

    while (*cmdline != 0 && (*cmdline == ' ' || *cmdline == '\t'))
        cmdline++;

    while (*cmdline == '/') {
        if ((cmdline[1] | 0x20) == 'w' && cmdline[2] == ' ') {
            bWait = 1;
            cmdline += 3;
        } else {
            cmdline++;
            s = var;
            e = s + sizeof(var) - 1;
            while (*cmdline != 0 && *cmdline != '=') {
                if (s < e) {
                    *s++ = *cmdline++;
                } else {
                    cmdline++;
                    err = 1;
                }
            }
            *s = 0;
            if (*cmdline == '=')
                cmdline++;

            delim = ' ';
            if (*cmdline == '\"') {
                delim = '\"';
                cmdline++;
            }

            s = val;
            e = s + sizeof(val) - 1;
            while (*cmdline != delim && *cmdline != 0) {
                if (s < e) {
                    *s++ = *cmdline++;
                } else {
                    cmdline++;
                    err = 1;
                }
            }
            *s = 0;
            if (*cmdline == delim)
                cmdline++;

            SetEnvironmentVariable(var, val);
        }
        while (*cmdline != 0 && (*cmdline == ' ' || *cmdline == '\t'))
            cmdline++;
    }

    if (*cmdline == 0) {
        MessageBox(0, "About:\n\nStarts a program hiding its console window, 
optionally waiting\nfor its termination and setting environment 
variables.\n\nUsage:\n\thidecmd [/w] [/VAR=val] <filename> 
[<params>]\n\n\t/w\t\twait for program termination\n\t/VAR=val\t\tset 
VAR=val\n\t<filename>\texecutable program\n\t<params>\tprogram parameters", 
"Error: Incorrect usage", 0);
        ExitProcess(0);
    }

    if (err) {
        MessageBox(0, "One of the specified environment variables or its value 
is too long.", "Error: Variable name or value too long", 0);
        ExitProcess(0);
    }

    /* create process with new console */
    s = (char *) &si;
    for (i = 0; i < sizeof(si); i++)
        s[i] = 0x00;

    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    if (CreateProcess(NULL, cmdline, NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
                      NULL, NULL, &si, &pi)) {
        if (bWait)
            WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }
    else
        exitcode = GetLastError();

    /* terminate this */
    ExitProcess(exitcode);
}

Reply via email to