Subversion Repositories aysalia

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 daniel-mar 1
program AyDos;
2
 
16 daniel-mar 3
// Aysalia DOS Launcher
31 daniel-mar 4
// Revision 2018-12-08
16 daniel-mar 5
// (C) 2018 Daniel Marschall, ViaThinkSoft
6
 
7
// This launcher does launch DOSBox with the correct *.conf file,
8
// centers the window and changes the window title and icon at runtime.
9
 
9 daniel-mar 10
uses
11
  SysUtils,
12
  ShellAPI,
16 daniel-mar 13
  Windows,
14
  Messages;
9 daniel-mar 15
 
16
{$R *.RES}
17
 
16 daniel-mar 18
const
19
  DOSBOX_EXE = 'DOSBox.exe';
27 daniel-mar 20
  AYDOS_MNU = 'AyDos.mnu';
30 daniel-mar 21
  AYDOS_COM = 'AyDos.com';
16 daniel-mar 22
 
23
var
24 daniel-mar 24
  hPsApiDll: Cardinal = 0;
25
  hIcon: THandle = 0;
26
  bCeneredOnce: boolean = false;
16 daniel-mar 27
 
28
(*
29
function GetModuleFileNameEx(inProcess: THandle; inModule: THandle;
30
      Filename: PChar; size: DWord): DWord; stdcall;
31
      external 'psapi.dll' name 'GetModuleFileNameExA';
32
*)
33
{$IFDEF UNICODE}
19 daniel-mar 34
function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; Filename: PWideChar; size: DWord): Integer;
16 daniel-mar 35
type
36
  TGetModuleFileNameExFunc = function (inProcess: THandle; inModule: THandle; Filename: PWideChar; size: DWord): DWord; stdcall;
37
var
38
  funcGetModuleFileNameEx: TGetModuleFileNameExFunc;
39
begin
20 daniel-mar 40
  if hPsApiDll <> 0 then
16 daniel-mar 41
  begin
20 daniel-mar 42
    @funcGetModuleFileNameEx := GetProcAddress(hPsApiDll, 'GetModuleFileNameExW');
19 daniel-mar 43
    if Assigned(funcGetModuleFileNameEx) then
16 daniel-mar 44
      result := funcGetModuleFileNameEx(inProcess, inModule, Filename, size)
45
    else
19 daniel-mar 46
      result := -1;
16 daniel-mar 47
  end
19 daniel-mar 48
  else result := -2;
16 daniel-mar 49
end;
50
{$ELSE}
19 daniel-mar 51
function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; Filename: PAnsiChar; size: DWord): Integer;
16 daniel-mar 52
type
53
  TGetModuleFileNameExFunc = function (inProcess: THandle; inModule: THandle; Filename: PAnsiChar; size: DWord): DWord; stdcall;
54
var
19 daniel-mar 55
  funcGetModuleFileNameEx: TGetModuleFileNameExFunc;
16 daniel-mar 56
begin
57
  if hPsApiDll <> 0 then
58
  begin
19 daniel-mar 59
    @funcGetModuleFileNameEx := GetProcAddress(hPsApiDll, 'GetModuleFileNameExA');
60
    if Assigned(funcGetModuleFileNameEx) then
16 daniel-mar 61
      result := funcGetModuleFileNameEx(inProcess, inModule, Filename, size)
62
    else
19 daniel-mar 63
      result := -1;
16 daniel-mar 64
  end
19 daniel-mar 65
  else result := -2;
16 daniel-mar 66
end;
67
{$ENDIF}
68
 
69
procedure ChangeTitleAndIcon(hWnd: Thandle);
70
var
71
  Title: array[0..255] of Char;
72
const
73
  TargetWinWidth = 640;
74
  TargetWinHeight = 480;
20 daniel-mar 75
resourcestring
76
  AyDosTitle = 'Aysalia DOS';
26 daniel-mar 77
  AyDos1Title = 'Aysalia DOS 1';
78
  AyDos2Title = 'Aysalia DOS 2';
16 daniel-mar 79
begin
80
  ZeroMemory(@Title, sizeof(Title));
81
  GetWindowText(hWnd, @Title, sizeof(Title)-1);
82
 
83
  // Center window (once)
84
  if (Title = 'DOSBox') and not bCeneredOnce then
85
  begin
86
    MoveWindow(hWnd, GetSystemMetrics(SM_CXSCREEN) div 2 - TargetWinWidth div 2,
87
                     GetSystemMetrics(SM_CYSCREEN) div 2 - TargetWinHeight div 2,
88
                     TargetWinWidth,
89
                     TargetWinHeight,
90
                     true);
91
    bCeneredOnce := true;
92
  end;
93
 
94
  // Change window title
95
  if Pos('AYDOS1', Title) > 0 then
20 daniel-mar 96
    SetWindowText(hWnd, PChar(AyDos1Title))
16 daniel-mar 97
  else if Pos('AYDOS2', Title) > 0 then
20 daniel-mar 98
    SetWindowText(hWnd, PChar(AyDos2Title))
26 daniel-mar 99
  else if Pos('AYDOS', Title) > 0 then
20 daniel-mar 100
    SetWindowText(hWnd, PChar(AyDosTitle));
16 daniel-mar 101
 
102
  // Change window and taskbar icon
103
  if hIcon > 0 then
104
  begin
105
    // Change both icons to the same icon handle.
106
    SendMessage(hWnd, WM_SETICON, ICON_SMALL, hIcon);
107
    SendMessage(hWnd, WM_SETICON, ICON_BIG, hIcon);
108
 
109
    // This will ensure that the application icon gets changed too.
110
    SendMessage(GetWindow(hWnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
111
    SendMessage(GetWindow(hWnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);
112
  end;
113
end;
114
 
115
function EnumWindowsProc(Handle: hWnd; dummy: DWORD): BOOL; stdcall;
116
var
117
  Title: array[0..255] of Char;
19 daniel-mar 118
  WinFileName: array[0..MAX_PATH] of Char;
16 daniel-mar 119
var
19 daniel-mar 120
  PID: DWORD;
121
  hProcess: THandle;
122
  Len: Integer;
16 daniel-mar 123
begin
124
  Result := True;
19 daniel-mar 125
  ZeroMemory(@WinFileName, sizeof(WinFileName));
126
  GetWindowThreadProcessId(Handle, @PID);
16 daniel-mar 127
  hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, PID);
19 daniel-mar 128
  Len := GetModuleFileNameEx(hProcess, 0, WinFileName, sizeof(WinFileName)-1);
16 daniel-mar 129
  if Len > 0 then
130
  begin
131
    // GetModuleFileNameEx is available on newer operating systems;
132
    // it ensures that we find the correct window by checking its EXE filename.
133
    if SameText(WinFileName, ExtractFilePath(ParamStr(0)) + DOSBOX_EXE) then
134
    begin
135
      Result := False; // stop enumeration
136
      ChangeTitleAndIcon(Handle);
137
    end;
138
  end
19 daniel-mar 139
  else if Len < 0 then
16 daniel-mar 140
  begin
141
    // At Win9x, there is no psapi.dll, so we try it the old fashioned way,
142
    // finding the window by parts of its title
143
    ZeroMemory(@Title, sizeof(Title));
144
    GetWindowText(Handle, Title, sizeof(Title)-1);
145
    if IsWindowVisible(Handle) then
146
    begin
147
      if (title = 'DOSBox') or ((Pos('DOSBox ',   title) > 0) and
148
                                (Pos('Cpu speed', title) > 0)) then
149
      begin
150
        Result := False; // stop enumeration
151
        ChangeTitleAndIcon(Handle);
152
      end;
153
    end;
154
  end;
155
end;
156
 
9 daniel-mar 157
function ShellExecuteWait(hWnd: HWND; Operation, FileName, Parameters,
31 daniel-mar 158
  Directory: PChar; ShowCmd: Integer; lpEnumFunc: TFNWndEnumProc=nil): DWord;
9 daniel-mar 159
var
160
  Info: TShellExecuteInfo;
161
  pInfo: PShellExecuteInfo;
162
begin
163
  pInfo := @Info;
164
  with Info do
165
  begin
16 daniel-mar 166
    cbSize       := SizeOf(Info);
167
    fMask        := SEE_MASK_NOCLOSEPROCESS;
168
    wnd          := hWnd;
169
    lpVerb       := Operation;
19 daniel-mar 170
    lpFile       := FileName;
9 daniel-mar 171
    lpParameters := PChar(Parameters + #0);
16 daniel-mar 172
    lpDirectory  := PChar(Directory);
173
    nShow        := ShowCmd;
174
    hInstApp     := 0;
9 daniel-mar 175
  end;
176
  ShellExecuteEx(pInfo);
177
 
178
  repeat
19 daniel-mar 179
    result := WaitForSingleObject(Info.hProcess, 10);
31 daniel-mar 180
    if Assigned(lpEnumFunc) then EnumWindows(lpEnumFunc, 0);
19 daniel-mar 181
  until (result <> WAIT_TIMEOUT);
9 daniel-mar 182
end;
183
 
27 daniel-mar 184
function CanRunDosBox: boolean;
185
var
186
  windir: array[0..MAX_PATH] of char;
187
  osVerInfo: TOSVersionInfo;
188
begin
189
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
190
  if GetVersionEx(osVerInfo) then
191
  begin
192
    // DOSBox does not work with Windows 95
193
    // It works on Windows 98 (but the VC++ Runtime must be installed)
194
    if osVerInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
195
    begin
196
      result := (osVerInfo.dwMajorVersion > 4) or
197
               ((osVerInfo.dwMajorVersion = 4) and (osVerInfo.dwMinorVersion >= 10{Win98}));
198
    end
199
    else if osVerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then
200
    begin
201
      result := true;
202
    end
203
    else
204
    begin
205
      // This should not happen
206
      result := false;
207
    end;
208
  end
209
  else
210
  begin
211
    if GetWindowsDirectory(windir, sizeof(windir)) > 0 then
212
    begin
213
      // In case GetVersionEx fails, we are trying to see if command.com exists
214
      result := FileExists(windir + '\command.com');
215
    end
216
    else
217
    begin
218
      // This should never happen
219
      result := false;
220
    end;
221
  end;
222
end;
223
 
16 daniel-mar 224
function Main: Integer;
9 daniel-mar 225
var
226
  sFile: string;
227
begin
27 daniel-mar 228
  if CanRunDosBox then
229
  begin
31 daniel-mar 230
    hPsApiDll := LoadLibrary('psapi.dll');
231
    try
232
      hIcon := LoadIcon(hInstance, 'MainIcon');
233
      bCeneredOnce := false;
9 daniel-mar 234
 
31 daniel-mar 235
      ShellExecuteWait(0, 'open', DOSBOX_EXE, '-noconsole -conf DOSBox.conf',
236
        PChar(ExtractFilePath(ParamStr(0))), SW_NORMAL, @EnumWindowsProc);
9 daniel-mar 237
 
31 daniel-mar 238
      sFile := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) + 'stdout.txt';
239
      if FileExists(sFile) then DeleteFile(PChar(sFile));
240
 
241
      sFile := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) + 'stderr.txt';
242
      if FileExists(sFile) then DeleteFile(PChar(sFile));
243
    finally
244
      FreeLibrary(hPsApiDll);
245
      hPsApiDll := 0;
246
    end;
27 daniel-mar 247
  end
248
  else
249
  begin
250
    // SEE_MASK_CLASSNAME cannot be used with pure MZ files (it does only work for NE/PE files!)
251
    // So we need to do the dirty rename-hack...
31 daniel-mar 252
    if FileExists(AYDOS_MNU) and not FileExists(AYDOS_COM) then RenameFile(AYDOS_MNU, AYDOS_COM);
27 daniel-mar 253
    try
31 daniel-mar 254
      ShellExecuteWait(0, 'open', PChar(AYDOS_COM), '',
255
        PChar(ExtractFilePath(ParamStr(0))), SW_NORMAL, nil);
27 daniel-mar 256
    finally
31 daniel-mar 257
      if FileExists(AYDOS_COM) and not FileExists(AYDOS_MNU) then RenameFile(AYDOS_COM, AYDOS_MNU);
27 daniel-mar 258
    end;
259
  end;
16 daniel-mar 260
 
261
  result := 0;
262
end;
263
 
264
begin
31 daniel-mar 265
  ExitCode := Main;
9 daniel-mar 266
end.