4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * \author Enrico Forestieri
11 * Full author contact details are available in file CREDITS.
15 * This utility function is used to start lyx under windows, but
16 * hide the console window. It is adapted from program hidec at
17 * http://www.msfn.org/board/index.php?showtopic=49184&mode=threaded
20 * hidecmd [/w] [/VAR=val] <filename> [<params>]
21 * where: /w wait for program termination
22 * /VAR=val set VAR=val
23 * <filename> executable program
24 * <params> program parameters
26 * How to built this program:
28 * cl.exe hidecmd.c /GA /O1 /link /subsystem:windows \
29 * kernel32.lib advapi32.lib user32.lib libcmt.lib
31 * gcc -mno-cygwin -mwindows hidecmd.c -o hidecmd
39 char * usage = "hidecmd [/w] [/VAR=val] <filename> [<params>]\n"
40 " where: /w wait for program termination\n"
41 " /VAR=val set VAR=val\n"
42 " <filename> executable program\n"
43 " <params> program parameters\n";
47 // Using msvc, the following progma can reduce executable size from
48 // 44k to 6k. I am not sure if mingw/gcc can take advantage of them
51 // do not link to default libraries
52 #pragma comment(linker,"/NODEFAULTLIB")
53 // unite code and data section (make the program smaller)
54 #pragma comment(linker,"/MERGE:.rdata=.text")
55 // resolve record in section of code
56 #pragma comment(linker,"/SECTION:.text,EWR")
57 // the new entry point (the WinMain entry point is big)
58 #pragma comment(linker,"/ENTRY:NewWinMain")
61 #else // mingw/gcc uses this entry point
62 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpszCmd, int nCmd)
66 PROCESS_INFORMATION pi;
73 char * cmdLine = GetCommandLine();
75 // start and end of variable/value
79 // use GetCommandLine(), command name is included. Skip it
80 if (*cmdLine == '\"') {
85 while (*cmdLine != delim && *cmdLine != 0)
88 if (*cmdLine == delim)
91 // skip over ' ' or '\t'
92 while ((*cmdLine != 0) && ((*cmdLine == ' ') || (*cmdLine == '\t')))
95 while (*cmdLine == '/') {
97 if (((cmdLine[1] == 'w') || (cmdLine[1] == 'W')) &&
102 // environment variable
108 e = s + sizeof(var) - 1;
110 while (*cmdLine != 0 && *cmdLine != '=') {
126 if (*cmdLine == '\"') {
132 e = s + sizeof(val) - 1;
134 while (*cmdLine != delim && *cmdLine != 0) {
143 if (*cmdLine == delim)
146 SetEnvironmentVariable(var, val);
147 // MessageBox(0, val, var, 0);
151 while ((*cmdLine != 0) && ((*cmdLine == ' ') || (*cmdLine == '\t')))
155 // report error if there is no parameter
158 MessageBox(0, usage, "Error: Incorrect usage", 0);
163 MessageBox(0, "One of the specified environment variables or its value is too long.", "Error: Variable name or value too long", 0);
167 // create process with new console
168 // memset(&si, 0, sizeof(si));
170 for (i = 0; i < sizeof(si); ++i)
173 si.dwFlags = STARTF_USESHOWWINDOW;
174 si.wShowWindow = SW_HIDE;
175 if (CreateProcess(NULL, cmdLine,
176 NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
177 NULL, NULL, &si, &pi))
180 WaitForSingleObject(pi.hProcess, INFINITE);
181 CloseHandle( pi.hProcess );
182 CloseHandle( pi.hThread );
185 exitcode = GetLastError();
188 ExitProcess(exitcode);