2 // this program opens LyX and hides its console window
5 {The problematic is the following:
6 When the lyx.exe is started, also a console window is shown.
7 But closing the console window, also closes LyX,
8 therefore the console will be hidden by this program.}
13 Windows, SysUtils, ShellApi, Forms, Dialogs;
18 procedure HideWindow(ProgWin: string);
19 // hides a given program window
24 // find handle of the program window
25 // Repeat until the handle is available
26 // because Lyx needs some time to start
28 Handle := FindWindow(nil,Pchar(ProgWin));
30 // minimize the window
31 // SendMessage(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 1);
32 // hide the window from taskbar
33 ShowWindow(Handle, SW_HIDE);
37 procedure ReadPath(FileName: string; LaunchName: string;
38 ExecName: string; var PathR: string);
39 // reads the path to the lyx.exe from a given text file
40 var InString : string;
41 FileSize,Index,Last : Int64;
47 try //try..except for procedure
49 hFile:= Windows.CreateFile(PChar(FileName),GENERIC_READ,0,nil,
51 FILE_FLAG_SEQUENTIAL_SCAN,0);
52 if hFile= INVALID_HANDLE_VALUE then
54 MessageDlg('The file "' + FileName + '" could not be found!',
59 try //try..finally for hFile
60 FileSize:= FileSeek(hFile,0,2); //get file size
64 //move file content to InString
66 SetLength(InString,FileSize);
67 PInString:= PChar(InString);
68 FileRead(hFile,PInString^,FileSize);
70 //search the string backwards for the first appearance of ":"
74 if InString[Index] = ':' then
76 Until (InString[Index] = #10) or (Index = 1);
78 //if the last line of lyx.bat doesn't contain a ":" (a path)
79 if (InString[Index] = #10) or (Index = 1) then
81 MessageDlg('The file lyx.bat is corrupted!',mtError,[mbOK],0);
85 //jump before the ":" to the drive letter
87 //search for the LaunchName = end of the path
88 Last:= Pos(LaunchName,InString);
89 //the InString contains between Index and Last the wanted path
90 PathR:= Copy(InString,Index,Last - Index);
91 //attach LyX's executable to the path
92 PathR:= Path + ExecName;
94 finally //close the text file
95 Windows.CloseHandle(hFile);
98 except //when an error occurred somewhere in the procedure
99 MessageDlg('The file "' + FileName + '" is corrupted!',mtError,[mbOK],0);
105 begin //begin program
107 //hide the window of this application
108 ShowWindow(Application.Handle,SW_HIDE);
110 // read path of the lyxLauncher.exe from the file lyx.bat
111 ReadPath('lyx.bat', 'lyxLauncher.exe', 'lyx.exe', Path);
114 hLyX:= ShellExecute(Application.Handle,PChar('open'),
115 PChar(Path),nil,nil,SW_SHOWNORMAL);
116 if hLyX = ERROR_FILE_NOT_FOUND then
118 MessageDLG('The file'#13#10 + Path + #13#10
119 + 'could not be found!',mtError,[mbOK],0);
122 if hLyX = SE_ERR_ACCESSDENIED then
124 MessageDLG('Windows denied access on the file'#13#10 + Path,
129 // hide console window of lyx.exe