]> git.lyx.org Git - features.git/blob - development/Win32/packaging/AltInstaller/specials/Launcher/lyxLauncher.dpr
08317f720b37db308223da8ec593a20a72b65ecf
[features.git] / development / Win32 / packaging / AltInstaller / specials / Launcher / lyxLauncher.dpr
1 program lyxLauncher;
2 // this program opens LyX and hides its console window
3 // author: Uwe Stöhr
4
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.}
9
10 {$APPTYPE CONSOLE}
11
12 uses
13   Windows, SysUtils, ShellApi, Forms, Dialogs;
14
15 var Path : string;
16     hLyX : THandle;
17
18 procedure HideWindow(ProgWin: string);
19 // hides a given program window
20 var
21   Handle : THandle;
22 begin
23
24  // find handle of the program window
25  // Repeat until the handle is available
26  // because Lyx needs some time to start
27  Repeat
28   Handle := FindWindow(nil,Pchar(ProgWin));
29  Until Handle <> 0;
30  // minimize the window
31  // SendMessage(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 1);
32  // hide the window from taskbar
33  ShowWindow(Handle, SW_HIDE);
34
35 end; //end procedure
36
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;
42     hFile : THandle;
43     PInString : PChar;
44
45 begin
46
47  try //try..except for procedure
48   // open the text file
49   hFile:= Windows.CreateFile(PChar(FileName),GENERIC_READ,0,nil,
50                             OPEN_EXISTING,
51                             FILE_FLAG_SEQUENTIAL_SCAN,0);
52   if hFile= INVALID_HANDLE_VALUE then
53   begin
54    MessageDlg('The file "' + FileName + '" could not be found!',
55                mtError,[mbOK],0);
56    exit;
57   end;
58   
59   try //try..finally for hFile
60    FileSize:= FileSeek(hFile,0,2); //get file size
61    if FileSize = -1 then
62     RaiseLastOSError;
63
64    //move file content to InString
65    FileSeek(hFile,0,0);
66    SetLength(InString,FileSize);
67    PInString:= PChar(InString);
68    FileRead(hFile,PInString^,FileSize);
69
70    //search the string backwards for the first appearance of ":"
71    Index:= FileSize;
72    Repeat
73     dec(Index);
74     if InString[Index] = ':' then
75      Break;
76    Until (InString[Index] = #10) or (Index = 1);
77
78    //if the last line of lyx.bat doesn't contain a ":" (a path)
79    if (InString[Index] = #10) or (Index = 1) then
80    begin
81     MessageDlg('The file lyx.bat is corrupted!',mtError,[mbOK],0);
82     exit;
83    end;
84
85    //jump before the ":" to the drive letter
86    dec(Index);
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;
93
94   finally //close the text file
95    Windows.CloseHandle(hFile);
96   end; //end finally
97
98  except //when an error occurred somewhere in the procedure
99   MessageDlg('The file "' + FileName + '" is corrupted!',mtError,[mbOK],0);
100  end; //end except
101
102 end; //end procedure
103
104
105 begin //begin program
106
107  //hide the window of this application
108  ShowWindow(Application.Handle,SW_HIDE);
109
110  // read path of the lyxLauncher.exe from the file lyx.bat 
111  ReadPath('lyx.bat', 'lyxLauncher.exe', 'lyx.exe', Path);
112
113  // start LyX
114  hLyX:= ShellExecute(Application.Handle,PChar('open'),
115                      PChar(Path),nil,nil,SW_SHOWNORMAL);
116  if hLyX = ERROR_FILE_NOT_FOUND  then
117  begin
118   MessageDLG('The file'#13#10 + Path + #13#10
119              + 'could not be found!',mtError,[mbOK],0);
120   exit;
121  end;
122  if hLyX = SE_ERR_ACCESSDENIED  then
123  begin
124   MessageDLG('Windows denied access on the file'#13#10 + Path,
125              mtError,[mbOK],0);
126   exit;
127  end;
128
129  // hide console window of lyx.exe
130  HideWindow(Path);
131
132 end. //end program
133