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?
Bo
Index: development/Win32/runWithHiddenConsole.c
===================================================================
--- development/Win32/runWithHiddenConsole.c	(revision 0)
+++ development/Win32/runWithHiddenConsole.c	(revision 0)
@@ -0,0 +1,109 @@
+/* -*- C -*- */
+/**
+ * \file runWithHiddenConsole.c
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author hasherfrog
+ * \author Bo Peng
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+/**
+ *  This utility function is used to start lyx under windows, but
+ *  hide the console window. It is adapted from program hidec at
+ *  http://www.msfn.org/board/index.php?showtopic=49184&mode=threaded
+ *
+ *  Usage: create a shortcut with commands like:
+ *    runWithHiddenConsole cmd.exe /c lyx.exe
+ *    
+ *  How to built this program (use msvc):
+ *
+ *  cl.exe runWithHiddenConsole.c /GA /O1 /link /subsystem:windows \
+ *     kernel32.lib advapi32.lib user32.lib libcmt.lib
+ */
+
+#include <process.h>
+#include <windows.h>
+// do not link to default libraries
+#pragma comment(linker,"/NODEFAULTLIB")
+// unite code and data section (make the program smaller)
+#pragma comment(linker,"/MERGE:.rdata=.text")
+// resolve record in section of code (?)
+#pragma comment(linker,"/SECTION:.text,EWR")
+// the new entry point (why?)
+#pragma comment(linker,"/ENTRY:NewWinMain")
+
+char * usage = "This commands hides console window of started program and\n"
+	"wait (optional) for its termination.\n"
+	"Usage: runWithHiddenConsole [/w] <filename>\n"
+	"Where \n"
+	"    /w\twait for program to terminate\n"
+	"    filename\texecutable file name";
+
+
+//int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpszCmd,int nCmd)
+void NewWinMain(void)
+{
+	STARTUPINFO si;
+	PROCESS_INFORMATION pi;
+	int bWait = 0;
+	DWORD exitcode = 0;
+	char stopchar = ' ';
+	char* lpszCmd = GetCommandLine();
+
+	// command name may be quoted "  "
+	if (lpszCmd[0] == '\"') 
+		stopchar = '\"';
+	// go to the end of command
+	do { 
+		lpszCmd++; 
+	} while ((lpszCmd[0] != stopchar) && (lpszCmd[0] != 0));
+	// skip over ' ' or '\t'
+	if (lpszCmd[0] != 0)
+	{
+		do {
+			lpszCmd++; 
+		} while ((lpszCmd[0] != 0) && ((lpszCmd[0] == ' ') || (lpszCmd[0] == '\t')));
+	}
+	// report error if there is no parameter 
+	if (lpszCmd[0] == 0)
+	{
+		MessageBox(0, usage, "Error: Incorrect usage", 0);
+		ExitProcess(0);
+	};
+
+	// /w or /W option
+	if ((lpszCmd[0] == '/') && ((lpszCmd[1] == 'w') || (lpszCmd[1] == 'W')) &&
+		(lpszCmd[2] == ' '))
+	{
+		bWait = 1;
+		lpszCmd += 3;
+	};
+	// skip spaces
+	while ((lpszCmd[0] != 0) && ((lpszCmd[0] == ' ') || (lpszCmd[0] == '\t'))) 
+		lpszCmd++;
+
+	// create process with new console
+	// avoid linkg to libc etc, do not use memset
+	// memset(&si, 0, sizeof(STARTUPINFO);
+	memset(&si, 0, sizeof(si));
+	si.cb = sizeof(si);
+	si.dwFlags = STARTF_USESHOWWINDOW;
+	si.wShowWindow = SW_HIDE;
+	if (CreateProcess( NULL, lpszCmd,
+		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