Commit | Line | Data |
---|---|---|
32592631 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utilsexec.cpp | |
3 | // Purpose: Various utilities | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3f4a0c5b | 9 | // Licence: wxWindows license |
32592631 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
cfe780fb | 13 | #pragma implementation |
32592631 GL |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/setup.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/app.h" | |
2432b92d | 27 | #include "wx/intl.h" |
32592631 GL |
28 | #endif |
29 | ||
3e64d4e1 | 30 | #include "wx/log.h" |
dbeac4bd VZ |
31 | #include "wx/process.h" |
32 | ||
32592631 | 33 | #include "wx/msw/private.h" |
dbeac4bd | 34 | |
32592631 GL |
35 | #include <windows.h> |
36 | ||
37 | #include <ctype.h> | |
38 | ||
ce3ed50d | 39 | #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) |
32592631 | 40 | #include <direct.h> |
17dff81c | 41 | #ifndef __MWERKS__ |
32592631 GL |
42 | #include <dos.h> |
43 | #endif | |
17dff81c | 44 | #endif |
32592631 GL |
45 | |
46 | #ifdef __GNUWIN32__ | |
57c208c5 | 47 | #ifndef __TWIN32__ |
32592631 GL |
48 | #include <sys/unistd.h> |
49 | #include <sys/stat.h> | |
32592631 | 50 | #endif |
57c208c5 | 51 | #endif |
32592631 GL |
52 | |
53 | #ifdef __WIN32__ | |
54 | #include <io.h> | |
55 | ||
56 | #ifndef __GNUWIN32__ | |
57 | #include <shellapi.h> | |
58 | #endif | |
59 | #endif | |
60 | ||
61 | #include <stdio.h> | |
62 | #include <stdlib.h> | |
63 | #include <string.h> | |
64 | #ifndef __WATCOMC__ | |
3f4a0c5b VZ |
65 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
66 | #include <errno.h> | |
67 | #endif | |
32592631 GL |
68 | #endif |
69 | #include <stdarg.h> | |
70 | ||
cb6827a8 VZ |
71 | // this message is sent when the process we're waiting for terminates |
72 | #define wxWM_PROC_TERMINATED (WM_USER + 10000) | |
32592631 | 73 | |
cb6827a8 VZ |
74 | // structure describing the process we're being waiting for |
75 | struct wxExecuteData | |
76 | { | |
77 | public: | |
78 | ~wxExecuteData() | |
79 | { | |
750b78ba | 80 | #ifndef __WIN16__ |
cb6827a8 VZ |
81 | if ( !::CloseHandle(hProcess) ) |
82 | { | |
83 | wxLogLastError("CloseHandle(hProcess)"); | |
84 | } | |
750b78ba | 85 | #endif |
cb6827a8 VZ |
86 | } |
87 | ||
88 | HWND hWnd; // window to send wxWM_PROC_TERMINATED to | |
89 | HANDLE hProcess; // handle of the process | |
90 | DWORD dwProcessId; // pid of the process | |
91 | wxProcess *handler; | |
92 | DWORD dwExitCode; // the exit code of the process | |
93 | bool state; // set to FALSE when the process finishes | |
32592631 GL |
94 | }; |
95 | ||
5260b1c5 JS |
96 | |
97 | #ifdef __WIN32__ | |
32592631 GL |
98 | static DWORD wxExecuteThread(wxExecuteData *data) |
99 | { | |
cb6827a8 VZ |
100 | WaitForSingleObject(data->hProcess, INFINITE); |
101 | ||
102 | // get the exit code | |
103 | if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) ) | |
104 | { | |
105 | wxLogLastError("GetExitCodeProcess"); | |
106 | } | |
32592631 | 107 | |
cb6827a8 VZ |
108 | wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE, |
109 | "process should have terminated" ); | |
32592631 | 110 | |
cb6827a8 VZ |
111 | // send a message indicating process termination to the window |
112 | SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data); | |
113 | ||
114 | return 0; | |
32592631 | 115 | } |
5260b1c5 JS |
116 | #endif |
117 | ||
cb6827a8 VZ |
118 | // window procedure of a hidden window which is created just to receive |
119 | // the notification message when a process exits | |
32592631 GL |
120 | LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message, |
121 | WPARAM wParam, LPARAM lParam) | |
122 | { | |
cb6827a8 VZ |
123 | if ( message == wxWM_PROC_TERMINATED ) |
124 | { | |
125 | DestroyWindow(hWnd); // we don't need it any more | |
126 | ||
127 | wxExecuteData *data = (wxExecuteData *)lParam; | |
128 | if ( data->handler ) | |
129 | { | |
130 | data->handler->OnTerminate((int)data->dwProcessId, | |
131 | (int)data->dwExitCode); | |
132 | } | |
133 | ||
134 | if ( data->state ) | |
135 | { | |
136 | // we're executing synchronously, tell the waiting thread | |
137 | // that the process finished | |
138 | data->state = 0; | |
139 | } | |
140 | else | |
141 | { | |
142 | // asynchronous execution - we should do the clean up | |
143 | delete data; | |
144 | } | |
145 | } | |
146 | ||
147 | return 0; | |
32592631 GL |
148 | } |
149 | ||
150 | extern char wxPanelClassName[]; | |
151 | ||
152 | long wxExecute(const wxString& command, bool sync, wxProcess *handler) | |
153 | { | |
cb6827a8 | 154 | wxCHECK_MSG( !!command, 0, "empty command in wxExecute" ); |
32592631 | 155 | |
57c208c5 | 156 | #if defined(__WIN32__) && !defined(__TWIN32__) |
cb6827a8 VZ |
157 | // the old code is disabled because we really need a process handle |
158 | // if we want to execute it asynchronously or even just get its | |
159 | // return code and for this we must use CreateProcess() and not | |
160 | // ShellExecute() | |
161 | #if 0 | |
162 | // isolate command and arguments | |
163 | wxString commandName; | |
164 | bool insideQuotes = FALSE; | |
165 | const char *pc; | |
166 | for ( pc = command.c_str(); *pc != '\0'; pc++ ) | |
167 | { | |
168 | switch ( *pc ) | |
169 | { | |
170 | case ' ': | |
171 | case '\t': | |
172 | if ( !insideQuotes ) | |
173 | break; | |
174 | // fall through | |
175 | ||
176 | case '"': | |
177 | insideQuotes = !insideQuotes; | |
178 | // fall through | |
179 | ||
180 | default: | |
181 | commandName += *pc; | |
182 | continue; // skip the next break | |
183 | } | |
184 | ||
185 | // only reached for space not inside quotes | |
186 | break; | |
187 | } | |
188 | ||
189 | wxString commandArgs = pc; | |
190 | ||
191 | wxWindow *winTop = wxTheApp->GetTopWindow(); | |
192 | HWND hwndTop = (HWND)(winTop ? winTop->GetHWND() : 0); | |
193 | ||
194 | HANDLE result; | |
32592631 | 195 | #ifdef __GNUWIN32__ |
cb6827a8 VZ |
196 | result = ShellExecute(hwndTop, |
197 | (const wchar_t)"open", | |
198 | (const wchar_t)commandName, | |
199 | (const wchar_t)commandArgs, | |
200 | (const wchar_t)NULL, | |
201 | SW_SHOWNORMAL); | |
202 | #else // !GNUWIN32 | |
203 | result = ShellExecute(hwndTop, "open", commandName, | |
204 | commandArgs, NULL, SW_SHOWNORMAL); | |
205 | #endif // GNUWIN32 | |
206 | ||
207 | if ( ((long)result) <= 32 ) | |
208 | wxLogSysError(_("Can't execute command '%s'"), command.c_str()); | |
209 | ||
210 | return result; | |
211 | #else // 1 | |
212 | // create the process | |
213 | STARTUPINFO si; | |
214 | ::ZeroMemory(&si, sizeof(si)); | |
215 | si.cb = sizeof(si); | |
216 | ||
217 | PROCESS_INFORMATION pi; | |
218 | ||
219 | if ( ::CreateProcess( | |
220 | NULL, // application name (use only cmd line) | |
221 | (char *)command.c_str(), // full command line | |
222 | NULL, // security attributes: defaults for both | |
223 | NULL, // the process and its main thread | |
224 | FALSE, // don't inherit handles | |
225 | CREATE_DEFAULT_ERROR_MODE, // flags | |
226 | NULL, // environment (use the same) | |
227 | NULL, // current directory (use the same) | |
228 | &si, // startup info (unused here) | |
229 | &pi // process info | |
230 | ) == 0 ) | |
231 | { | |
232 | wxLogSysError(_("Execution of command '%s' failed"), command.c_str()); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | // close unneeded handle | |
238 | if ( !::CloseHandle(pi.hThread) ) | |
239 | wxLogLastError("CloseHandle(hThread)"); | |
240 | ||
241 | // create a hidden window to receive notification about process | |
242 | // termination | |
243 | HWND hwnd = ::CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL, | |
244 | (HMENU)NULL, wxGetInstance(), 0); | |
245 | wxASSERT_MSG( hwnd, "can't create a hidden window for wxExecute" ); | |
246 | ||
247 | FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC)wxExecuteWindowCbk, | |
248 | wxGetInstance()); | |
249 | ||
250 | ::SetWindowLong(hwnd, GWL_WNDPROC, (LONG) ExecuteWindowInstance); | |
251 | ||
252 | // Alloc data | |
253 | wxExecuteData *data = new wxExecuteData; | |
254 | data->hProcess = pi.hProcess; | |
255 | data->dwProcessId = pi.dwProcessId; | |
256 | data->hWnd = hwnd; | |
257 | data->state = sync; | |
258 | data->handler = handler; | |
259 | ||
260 | DWORD tid; | |
261 | HANDLE hThread = ::CreateThread(NULL, | |
262 | 0, | |
263 | (LPTHREAD_START_ROUTINE)wxExecuteThread, | |
264 | (void *)data, | |
265 | 0, | |
266 | &tid); | |
267 | ||
268 | if ( !hThread ) | |
269 | { | |
270 | wxLogLastError("CreateThread in wxExecute"); | |
271 | ||
272 | DestroyWindow(hwnd); | |
273 | delete data; | |
274 | ||
275 | // the process still started up successfully... | |
276 | return pi.dwProcessId; | |
277 | } | |
278 | ||
279 | if ( !sync ) | |
280 | { | |
281 | // clean up will be done when the process terminates | |
282 | return pi.dwProcessId; | |
283 | } | |
284 | ||
285 | // waiting until command executed | |
286 | while ( data->state ) | |
287 | wxYield(); | |
288 | ||
289 | delete data; | |
290 | ||
291 | return pi.dwProcessId; | |
292 | #endif // 0/1 | |
293 | #else // Win16 | |
294 | long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW); | |
295 | if (instanceID < 32) return(0); | |
296 | ||
297 | if (sync) { | |
298 | int running; | |
299 | do { | |
300 | wxYield(); | |
301 | running = GetModuleUsage((HANDLE)instanceID); | |
302 | } while (running); | |
303 | } | |
304 | ||
305 | return(instanceID); | |
306 | #endif // Win16/32 | |
32592631 | 307 | } |
c740f496 GL |
308 | |
309 | long wxExecute(char **argv, bool sync, wxProcess *handler) | |
310 | { | |
cb6827a8 VZ |
311 | wxString command; |
312 | ||
313 | while ( *argv != NULL ) | |
314 | { | |
315 | command << *argv++ << ' '; | |
316 | } | |
317 | ||
318 | command.RemoveLast(); | |
319 | ||
320 | return wxExecute(command, sync, handler); | |
c740f496 | 321 | } |