Rev 16 | Rev 20 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 16 | Rev 19 | ||
---|---|---|---|
Line 25... | Line 25... | ||
25 | function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; |
25 | function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; |
26 | Filename: PChar; size: DWord): DWord; stdcall; |
26 | Filename: PChar; size: DWord): DWord; stdcall; |
27 | external 'psapi.dll' name 'GetModuleFileNameExA'; |
27 | external 'psapi.dll' name 'GetModuleFileNameExA'; |
28 | *) |
28 | *) |
29 | {$IFDEF UNICODE} |
29 | {$IFDEF UNICODE} |
30 | function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; Filename: PWideChar; size: DWord): DWord; |
30 | function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; Filename: PWideChar; size: DWord): Integer; |
31 | type |
31 | type |
32 | TGetModuleFileNameExFunc = function (inProcess: THandle; inModule: THandle; Filename: PWideChar; size: DWord): DWord; stdcall; |
32 | TGetModuleFileNameExFunc = function (inProcess: THandle; inModule: THandle; Filename: PWideChar; size: DWord): DWord; stdcall; |
33 | var |
33 | var |
34 | dllHandle: Cardinal; |
- | |
35 | funcGetModuleFileNameEx: TGetModuleFileNameExFunc; |
34 | funcGetModuleFileNameEx: TGetModuleFileNameExFunc; |
36 | begin |
35 | begin |
37 | if psAPIHandle <> 0 then |
36 | if psAPIHandle <> 0 then |
38 | begin |
37 | begin |
39 | @funcGetModuleFileNameEx := GetProcAddress(psAPIHandle, 'GetModuleFileNameExW') ; |
38 | @funcGetModuleFileNameEx := GetProcAddress(psAPIHandle, 'GetModuleFileNameExW'); |
40 | if Assigned (funcGetModuleFileNameEx) then |
39 | if Assigned(funcGetModuleFileNameEx) then |
41 | result := funcGetModuleFileNameEx(inProcess, inModule, Filename, size) |
40 | result := funcGetModuleFileNameEx(inProcess, inModule, Filename, size) |
42 | else |
41 | else |
43 | result := 0; |
42 | result := -1; |
44 | end |
43 | end |
45 | else result := 0; |
44 | else result := -2; |
46 | end; |
45 | end; |
47 | {$ELSE} |
46 | {$ELSE} |
48 | function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; Filename: PAnsiChar; size: DWord): DWord; |
47 | function GetModuleFileNameEx(inProcess: THandle; inModule: THandle; Filename: PAnsiChar; size: DWord): Integer; |
49 | type |
48 | type |
50 | TGetModuleFileNameExFunc = function (inProcess: THandle; inModule: THandle; Filename: PAnsiChar; size: DWord): DWord; stdcall; |
49 | TGetModuleFileNameExFunc = function (inProcess: THandle; inModule: THandle; Filename: PAnsiChar; size: DWord): DWord; stdcall; |
51 | var |
50 | var |
52 | funcGetModuleFileNameEx : TGetModuleFileNameExFunc; |
51 | funcGetModuleFileNameEx: TGetModuleFileNameExFunc; |
53 | begin |
52 | begin |
Line 55... | Line 54... | ||
55 | begin |
54 | begin |
56 | @funcGetModuleFileNameEx := GetProcAddress(hPsApiDll, 'GetModuleFileNameExA') ; |
55 | @funcGetModuleFileNameEx := GetProcAddress(hPsApiDll, 'GetModuleFileNameExA'); |
57 | if Assigned (funcGetModuleFileNameEx) then |
56 | if Assigned(funcGetModuleFileNameEx) then |
58 | result := funcGetModuleFileNameEx(inProcess, inModule, Filename, size) |
57 | result := funcGetModuleFileNameEx(inProcess, inModule, Filename, size) |
59 | else |
58 | else |
60 | result := 0; |
59 | result := -1; |
61 | end |
60 | end |
62 | else result := 0; |
61 | else result := -2; |
63 | end; |
62 | end; |
64 | {$ENDIF} |
63 | {$ENDIF} |
65 | 64 | ||
66 | var |
65 | var |
67 | hIcon: THandle = 0; |
66 | hIcon: THandle = 0; |
Line 110... | Line 109... | ||
110 | end; |
109 | end; |
111 | 110 | ||
112 | function EnumWindowsProc(Handle: hWnd; dummy: DWORD): BOOL; stdcall; |
111 | function EnumWindowsProc(Handle: hWnd; dummy: DWORD): BOOL; stdcall; |
113 | var |
112 | var |
114 | Title: array[0..255] of Char; |
113 | Title: array[0..255] of Char; |
115 | const |
- | |
116 | C_FileNameLength = 256; |
114 | WinFileName: array[0..MAX_PATH] of Char; |
117 | var |
115 | var |
118 | WinFileName: string; |
116 | PID: DWORD; |
119 | PID, hProcess: DWORD; |
117 | hProcess: THandle; |
120 | Len: Byte; |
118 | Len: Integer; |
121 | begin |
119 | begin |
122 | Result := True; |
120 | Result := True; |
123 | SetLength(WinFileName, C_FileNameLength); |
121 | ZeroMemory(@WinFileName, sizeof(WinFileName)); |
124 | GetWindowThreadProcessId(Handle, PID); |
122 | GetWindowThreadProcessId(Handle, @PID); |
125 | hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, PID); |
123 | hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, PID); |
126 | Len := GetModuleFileNameEx(hProcess, 0, PChar(WinFileName), C_FileNameLength); |
124 | Len := GetModuleFileNameEx(hProcess, 0, WinFileName, sizeof(WinFileName)-1); |
127 | if Len > 0 then |
125 | if Len > 0 then |
128 | begin |
126 | begin |
129 | // GetModuleFileNameEx is available on newer operating systems; |
127 | // GetModuleFileNameEx is available on newer operating systems; |
130 | // it ensures that we find the correct window by checking its EXE filename. |
128 | // it ensures that we find the correct window by checking its EXE filename. |
131 | SetLength(WinFileName, Len); |
- | |
132 | if SameText(WinFileName, ExtractFilePath(ParamStr(0)) + DOSBOX_EXE) then |
129 | if SameText(WinFileName, ExtractFilePath(ParamStr(0)) + DOSBOX_EXE) then |
133 | begin |
130 | begin |
134 | Result := False; // stop enumeration |
131 | Result := False; // stop enumeration |
135 | ChangeTitleAndIcon(Handle); |
132 | ChangeTitleAndIcon(Handle); |
136 | end; |
133 | end; |
137 | end |
134 | end |
138 | else |
135 | else if Len < 0 then |
139 | begin |
136 | begin |
140 | // At Win9x, there is no psapi.dll, so we try it the old fashioned way, |
137 | // At Win9x, there is no psapi.dll, so we try it the old fashioned way, |
141 | // finding the window by parts of its title |
138 | // finding the window by parts of its title |
142 | ZeroMemory(@Title, sizeof(Title)); |
139 | ZeroMemory(@Title, sizeof(Title)); |
143 | GetWindowText(Handle, Title, sizeof(Title)-1); |
140 | GetWindowText(Handle, Title, sizeof(Title)-1); |
Line 156... | Line 153... | ||
156 | function ShellExecuteWait(hWnd: HWND; Operation, FileName, Parameters, |
153 | function ShellExecuteWait(hWnd: HWND; Operation, FileName, Parameters, |
157 | Directory: PAnsiChar; ShowCmd: Integer): DWord; |
154 | Directory: PAnsiChar; ShowCmd: Integer): DWord; |
158 | var |
155 | var |
159 | Info: TShellExecuteInfo; |
156 | Info: TShellExecuteInfo; |
160 | pInfo: PShellExecuteInfo; |
157 | pInfo: PShellExecuteInfo; |
161 | exitCode: DWord; |
- | |
162 | begin |
158 | begin |
163 | pInfo := @Info; |
159 | pInfo := @Info; |
164 | with Info do |
160 | with Info do |
165 | begin |
161 | begin |
166 | cbSize := SizeOf(Info); |
162 | cbSize := SizeOf(Info); |
167 | fMask := SEE_MASK_NOCLOSEPROCESS; |
163 | fMask := SEE_MASK_NOCLOSEPROCESS; |
168 | wnd := hWnd; |
164 | wnd := hWnd; |
169 | lpVerb := Operation; |
165 | lpVerb := Operation; |
170 | lpFile := FileName;; |
166 | lpFile := FileName; |
171 | lpParameters := PChar(Parameters + #0); |
167 | lpParameters := PChar(Parameters + #0); |
172 | lpDirectory := PChar(Directory); |
168 | lpDirectory := PChar(Directory); |
173 | nShow := ShowCmd; |
169 | nShow := ShowCmd; |
174 | hInstApp := 0; |
170 | hInstApp := 0; |
175 | end; |
171 | end; |
176 | ShellExecuteEx(pInfo); |
172 | ShellExecuteEx(pInfo); |
177 | 173 | ||
178 | repeat |
174 | repeat |
179 | exitCode := WaitForSingleObject(Info.hProcess, 10); |
175 | result := WaitForSingleObject(Info.hProcess, 10); |
180 | EnumWindows(@EnumWindowsProc, 0); |
176 | EnumWindows(@EnumWindowsProc, 0); |
181 | until (exitCode <> WAIT_TIMEOUT); |
177 | until (result <> WAIT_TIMEOUT); |
182 | - | ||
183 | result := exitCode; |
- | |
184 | end; |
178 | end; |
185 | 179 | ||
186 | function Main: Integer; |
180 | function Main: Integer; |
187 | var |
181 | var |
188 | sFile: string; |
182 | sFile: string; |
189 | begin |
183 | begin |
190 | ShellExecuteWait(0, 'open', DOSBOX_EXE, '-noconsole -conf DOSBox.conf', |
184 | ShellExecuteWait(0, 'open', DOSBOX_EXE, '-noconsole -conf DOSBox.conf', |
191 | PChar(ExtractFilePath(ParamStr(0))), SW_NORMAL); |
185 | PChar(ExtractFilePath(ParamStr(0))), SW_NORMAL); |
192 | 186 | ||
193 | sFile := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'stdout.txt'; |
187 | sFile := IncludeTrailingBackSlash(ExtractFilePath(ParamStr(0))) + 'stdout.txt'; |
194 | if FileExists(sFile) then DeleteFile(PChar(sFile)); |
188 | if FileExists(sFile) then DeleteFile(PChar(sFile)); |
195 | 189 | ||
196 | sFile := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'stderr.txt'; |
190 | sFile := IncludeTrailingBackSlash(ExtractFilePath(ParamStr(0))) + 'stderr.txt'; |
197 | if FileExists(sFile) then DeleteFile(PChar(sFile)); |
191 | if FileExists(sFile) then DeleteFile(PChar(sFile)); |
198 | 192 | ||
199 | result := 0; |
193 | result := 0; |
200 | end; |
194 | end; |
201 | 195 | ||
202 | begin |
196 | begin |
203 | hPsApiDll := LoadLibrary('psapi.dll') ; |
197 | hPsApiDll := LoadLibrary('psapi.dll'); |
- | 198 | try |
|
204 | hIcon := LoadIcon(hInstance, 'MainIcon'); |
199 | hIcon := LoadIcon(hInstance, 'MainIcon'); |
205 | ExitCode := Main; |
200 | ExitCode := Main; |
- | 201 | finally |
|
206 | FreeLibrary(hPsApiDll); |
202 | FreeLibrary(hPsApiDll); |
- | 203 | end; |
|
207 | end. |
204 | end. |