]>
Commit | Line | Data |
---|---|---|
32592631 | 1 | ///////////////////////////////////////////////////////////////////////////// |
b568d04f | 2 | // Name: msw/utilsexec.cpp |
32592631 GL |
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 | ||
b568d04f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
32592631 | 20 | #ifdef __GNUG__ |
b568d04f | 21 | #pragma implementation |
32592631 GL |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
b568d04f | 28 | #pragma hdrstop |
32592631 GL |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
b568d04f VZ |
32 | #include "wx/utils.h" |
33 | #include "wx/app.h" | |
34 | #include "wx/intl.h" | |
32592631 GL |
35 | #endif |
36 | ||
3e64d4e1 | 37 | #include "wx/log.h" |
1044a386 JS |
38 | |
39 | #ifdef __WIN32__ | |
8b33ae2d | 40 | #include "wx/stream.h" |
b568d04f | 41 | #include "wx/process.h" |
1044a386 | 42 | #endif |
dbeac4bd | 43 | |
32592631 | 44 | #include "wx/msw/private.h" |
dbeac4bd | 45 | |
32592631 GL |
46 | #include <ctype.h> |
47 | ||
04ef50df | 48 | #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) |
b568d04f | 49 | #include <direct.h> |
17dff81c | 50 | #ifndef __MWERKS__ |
b568d04f | 51 | #include <dos.h> |
32592631 | 52 | #endif |
17dff81c | 53 | #endif |
32592631 | 54 | |
b568d04f VZ |
55 | #if defined(__GNUWIN32__) && !defined(__TWIN32__) |
56 | #include <sys/unistd.h> | |
57 | #include <sys/stat.h> | |
57c208c5 | 58 | #endif |
32592631 | 59 | |
04ef50df | 60 | #if defined(__WIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__) |
32592631 GL |
61 | #include <io.h> |
62 | ||
63 | #ifndef __GNUWIN32__ | |
64 | #include <shellapi.h> | |
65 | #endif | |
66 | #endif | |
67 | ||
68 | #include <stdio.h> | |
69 | #include <stdlib.h> | |
70 | #include <string.h> | |
71 | #ifndef __WATCOMC__ | |
3f4a0c5b VZ |
72 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
73 | #include <errno.h> | |
74 | #endif | |
32592631 GL |
75 | #endif |
76 | #include <stdarg.h> | |
77 | ||
731dd422 VZ |
78 | #if wxUSE_IPC |
79 | #include "wx/dde.h" // for WX_DDE hack in wxExecute | |
80 | #endif // wxUSE_IPC | |
5bd3a2da | 81 | |
b568d04f VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // constants | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
cb6827a8 VZ |
86 | // this message is sent when the process we're waiting for terminates |
87 | #define wxWM_PROC_TERMINATED (WM_USER + 10000) | |
32592631 | 88 | |
b568d04f VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // this module globals | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // we need to create a hidden window to receive the process termination | |
94 | // notifications and for this we need a (Win) class name for it which we will | |
95 | // register the first time it's needed | |
96 | static const wxChar *gs_classForHiddenWindow = NULL; | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // private types | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
cb6827a8 VZ |
102 | // structure describing the process we're being waiting for |
103 | struct wxExecuteData | |
104 | { | |
105 | public: | |
106 | ~wxExecuteData() | |
107 | { | |
750b78ba | 108 | #ifndef __WIN16__ |
cb6827a8 VZ |
109 | if ( !::CloseHandle(hProcess) ) |
110 | { | |
f6bcfd97 | 111 | wxLogLastError(wxT("CloseHandle(hProcess)")); |
cb6827a8 | 112 | } |
750b78ba | 113 | #endif |
cb6827a8 VZ |
114 | } |
115 | ||
116 | HWND hWnd; // window to send wxWM_PROC_TERMINATED to | |
117 | HANDLE hProcess; // handle of the process | |
118 | DWORD dwProcessId; // pid of the process | |
119 | wxProcess *handler; | |
120 | DWORD dwExitCode; // the exit code of the process | |
121 | bool state; // set to FALSE when the process finishes | |
32592631 GL |
122 | }; |
123 | ||
f6bcfd97 | 124 | #if defined(__WIN32__) && wxUSE_STREAMS |
8b33ae2d | 125 | |
8b33ae2d GL |
126 | // ---------------------------------------------------------------------------- |
127 | // wxPipeStreams | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
f6bcfd97 BP |
130 | class wxPipeInputStream: public wxInputStream |
131 | { | |
8b33ae2d GL |
132 | public: |
133 | wxPipeInputStream(HANDLE hInput); | |
134 | ~wxPipeInputStream(); | |
135 | ||
f6bcfd97 BP |
136 | virtual bool Eof() const; |
137 | ||
8b33ae2d GL |
138 | protected: |
139 | size_t OnSysRead(void *buffer, size_t len); | |
140 | ||
141 | protected: | |
142 | HANDLE m_hInput; | |
143 | }; | |
144 | ||
f6bcfd97 BP |
145 | class wxPipeOutputStream: public wxOutputStream |
146 | { | |
8b33ae2d GL |
147 | public: |
148 | wxPipeOutputStream(HANDLE hOutput); | |
149 | ~wxPipeOutputStream(); | |
150 | ||
151 | protected: | |
152 | size_t OnSysWrite(const void *buffer, size_t len); | |
153 | ||
154 | protected: | |
155 | HANDLE m_hOutput; | |
156 | }; | |
157 | ||
158 | // ================== | |
159 | // wxPipeInputStream | |
160 | // ================== | |
161 | ||
162 | wxPipeInputStream::wxPipeInputStream(HANDLE hInput) | |
163 | { | |
164 | m_hInput = hInput; | |
cd6ce4a9 | 165 | } |
8b33ae2d GL |
166 | |
167 | wxPipeInputStream::~wxPipeInputStream() | |
168 | { | |
169 | ::CloseHandle(m_hInput); | |
170 | } | |
171 | ||
f6bcfd97 | 172 | bool wxPipeInputStream::Eof() const |
8b33ae2d | 173 | { |
f6bcfd97 BP |
174 | DWORD nAvailable; |
175 | ||
176 | // function name is misleading, it works with anon pipes as well | |
177 | DWORD rc = ::PeekNamedPipe | |
178 | ( | |
179 | m_hInput, // handle | |
180 | NULL, 0, // ptr to buffer and its size | |
181 | NULL, // [out] bytes read | |
182 | &nAvailable, // [out] bytes available | |
183 | NULL // [out] bytes left | |
184 | ); | |
185 | ||
186 | if ( !rc ) | |
187 | { | |
188 | if ( ::GetLastError() != ERROR_BROKEN_PIPE ) | |
189 | { | |
190 | // unexpected error | |
191 | wxLogLastError(_T("PeekNamedPipe")); | |
192 | } | |
8b33ae2d | 193 | |
f6bcfd97 BP |
194 | // don't try to continue reading from a pipe if an error occured or if |
195 | // it had been closed | |
196 | return TRUE; | |
197 | } | |
198 | else | |
199 | { | |
200 | return nAvailable == 0; | |
201 | } | |
202 | } | |
203 | ||
204 | size_t wxPipeInputStream::OnSysRead(void *buffer, size_t len) | |
205 | { | |
206 | // reading from a pipe may block if there is no more data, always check for | |
207 | // EOF first | |
8b33ae2d | 208 | m_lasterror = wxSTREAM_NOERROR; |
f6bcfd97 BP |
209 | if ( Eof() ) |
210 | return 0; | |
211 | ||
212 | DWORD bytesRead; | |
213 | if ( !::ReadFile(m_hInput, buffer, len, &bytesRead, NULL) ) | |
214 | { | |
215 | if ( ::GetLastError() == ERROR_BROKEN_PIPE ) | |
8b33ae2d GL |
216 | m_lasterror = wxSTREAM_EOF; |
217 | else | |
218 | m_lasterror = wxSTREAM_READ_ERROR; | |
219 | } | |
f6bcfd97 | 220 | |
8b33ae2d GL |
221 | return bytesRead; |
222 | } | |
223 | ||
224 | // ================== | |
225 | // wxPipeOutputStream | |
226 | // ================== | |
227 | ||
228 | wxPipeOutputStream::wxPipeOutputStream(HANDLE hOutput) | |
229 | { | |
230 | m_hOutput = hOutput; | |
cd6ce4a9 | 231 | } |
8b33ae2d GL |
232 | |
233 | wxPipeOutputStream::~wxPipeOutputStream() | |
234 | { | |
235 | ::CloseHandle(m_hOutput); | |
236 | } | |
237 | ||
238 | size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len) | |
239 | { | |
240 | DWORD bytesRead; | |
241 | ||
242 | m_lasterror = wxSTREAM_NOERROR; | |
f6bcfd97 BP |
243 | if ( !::WriteFile(m_hOutput, buffer, len, &bytesRead, NULL) ) |
244 | { | |
245 | if ( ::GetLastError() == ERROR_BROKEN_PIPE ) | |
8b33ae2d GL |
246 | m_lasterror = wxSTREAM_EOF; |
247 | else | |
248 | m_lasterror = wxSTREAM_READ_ERROR; | |
249 | } | |
f6bcfd97 | 250 | |
8b33ae2d GL |
251 | return bytesRead; |
252 | } | |
253 | ||
254 | #endif // __WIN32__ | |
255 | ||
b568d04f VZ |
256 | // ============================================================================ |
257 | // implementation | |
258 | // ============================================================================ | |
5260b1c5 JS |
259 | |
260 | #ifdef __WIN32__ | |
f6bcfd97 | 261 | |
32592631 GL |
262 | static DWORD wxExecuteThread(wxExecuteData *data) |
263 | { | |
cb6827a8 VZ |
264 | WaitForSingleObject(data->hProcess, INFINITE); |
265 | ||
266 | // get the exit code | |
267 | if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) ) | |
268 | { | |
f6bcfd97 | 269 | wxLogLastError(wxT("GetExitCodeProcess")); |
cb6827a8 | 270 | } |
32592631 | 271 | |
cb6827a8 | 272 | wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE, |
223d09f6 | 273 | wxT("process should have terminated") ); |
32592631 | 274 | |
cb6827a8 VZ |
275 | // send a message indicating process termination to the window |
276 | SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data); | |
e6045e08 | 277 | |
cb6827a8 | 278 | return 0; |
32592631 | 279 | } |
5260b1c5 | 280 | |
cb6827a8 VZ |
281 | // window procedure of a hidden window which is created just to receive |
282 | // the notification message when a process exits | |
32592631 GL |
283 | LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message, |
284 | WPARAM wParam, LPARAM lParam) | |
285 | { | |
cb6827a8 VZ |
286 | if ( message == wxWM_PROC_TERMINATED ) |
287 | { | |
288 | DestroyWindow(hWnd); // we don't need it any more | |
289 | ||
290 | wxExecuteData *data = (wxExecuteData *)lParam; | |
291 | if ( data->handler ) | |
292 | { | |
293 | data->handler->OnTerminate((int)data->dwProcessId, | |
294 | (int)data->dwExitCode); | |
295 | } | |
e6045e08 | 296 | |
cb6827a8 VZ |
297 | if ( data->state ) |
298 | { | |
299 | // we're executing synchronously, tell the waiting thread | |
300 | // that the process finished | |
301 | data->state = 0; | |
302 | } | |
303 | else | |
304 | { | |
305 | // asynchronous execution - we should do the clean up | |
306 | delete data; | |
307 | } | |
cb6827a8 | 308 | |
0d7ea902 VZ |
309 | return 0; |
310 | } | |
311 | else | |
312 | { | |
313 | return DefWindowProc(hWnd, message, wParam, lParam); | |
314 | } | |
32592631 | 315 | } |
731dd422 | 316 | #endif // Win32 |
32592631 | 317 | |
ca289436 VZ |
318 | #if wxUSE_IPC |
319 | ||
320 | // connect to the given server via DDE and ask it to execute the command | |
321 | static bool wxExecuteDDE(const wxString& ddeServer, | |
322 | const wxString& ddeTopic, | |
323 | const wxString& ddeCommand) | |
324 | { | |
325 | bool ok; | |
326 | ||
327 | wxDDEClient client; | |
328 | wxConnectionBase *conn = client.MakeConnection(_T(""), | |
329 | ddeServer, | |
330 | ddeTopic); | |
331 | if ( !conn ) | |
332 | { | |
333 | ok = FALSE; | |
334 | } | |
335 | else // connected to DDE server | |
336 | { | |
337 | // the added complication here is that although most | |
338 | // programs use XTYP_EXECUTE for their DDE API, some | |
339 | // important ones - like IE and other MS stuff - use | |
340 | // XTYP_REQUEST! | |
341 | // | |
342 | // so we try it first and then the other one if it | |
343 | // failed | |
344 | { | |
345 | wxLogNull noErrors; | |
346 | ok = conn->Request(ddeCommand) != NULL; | |
347 | } | |
348 | ||
349 | if ( !ok ) | |
350 | { | |
351 | // now try execute - but show the errors | |
352 | ok = conn->Execute(ddeCommand); | |
353 | } | |
354 | } | |
355 | ||
356 | return ok; | |
357 | } | |
358 | ||
359 | #endif // wxUSE_IPC | |
360 | ||
5bd3a2da | 361 | long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) |
32592631 | 362 | { |
5bd3a2da VZ |
363 | wxCHECK_MSG( !!cmd, 0, wxT("empty command in wxExecute") ); |
364 | ||
039f62f4 | 365 | wxString command; |
6ba63600 | 366 | |
731dd422 | 367 | #if wxUSE_IPC |
5bd3a2da VZ |
368 | // DDE hack: this is really not pretty, but we need to allow this for |
369 | // transparent handling of DDE servers in wxMimeTypesManager. Usually it | |
370 | // returns the command which should be run to view/open/... a file of the | |
371 | // given type. Sometimes, however, this command just launches the server | |
372 | // and an additional DDE request must be made to really open the file. To | |
373 | // keep all this well hidden from the application, we allow a special form | |
6ba63600 | 374 | // of command: WX_DDE#<command>#DDE_SERVER#DDE_TOPIC#DDE_COMMAND in which |
5bd3a2da | 375 | // case we execute just <command> and process the rest below |
039f62f4 | 376 | wxString ddeServer, ddeTopic, ddeCommand; |
5bd3a2da VZ |
377 | static const size_t lenDdePrefix = 7; // strlen("WX_DDE:") |
378 | if ( cmd.Left(lenDdePrefix) == _T("WX_DDE#") ) | |
379 | { | |
ca289436 VZ |
380 | // speed up the concatenations below |
381 | ddeServer.reserve(256); | |
382 | ddeTopic.reserve(256); | |
383 | ddeCommand.reserve(256); | |
384 | ||
5bd3a2da VZ |
385 | const wxChar *p = cmd.c_str() + 7; |
386 | while ( *p && *p != _T('#') ) | |
387 | { | |
388 | command += *p++; | |
389 | } | |
390 | ||
391 | if ( *p ) | |
392 | { | |
393 | // skip '#' | |
394 | p++; | |
395 | } | |
396 | else | |
397 | { | |
398 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
399 | } | |
400 | ||
401 | while ( *p && *p != _T('#') ) | |
402 | { | |
403 | ddeServer += *p++; | |
404 | } | |
405 | ||
406 | if ( *p ) | |
407 | { | |
408 | // skip '#' | |
409 | p++; | |
410 | } | |
411 | else | |
412 | { | |
413 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
414 | } | |
415 | ||
416 | while ( *p && *p != _T('#') ) | |
417 | { | |
418 | ddeTopic += *p++; | |
419 | } | |
420 | ||
421 | if ( *p ) | |
422 | { | |
423 | // skip '#' | |
424 | p++; | |
425 | } | |
426 | else | |
427 | { | |
428 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
429 | } | |
430 | ||
431 | while ( *p ) | |
432 | { | |
433 | ddeCommand += *p++; | |
434 | } | |
6ba63600 | 435 | |
ca289436 VZ |
436 | // if we want to just launch the program and not wait for its |
437 | // termination, try to execute DDE command right now, it can succeed if | |
438 | // the process is already running - but as it fails if it's not | |
439 | // running, suppress any errors it might generate | |
440 | if ( !sync ) | |
6ba63600 | 441 | { |
ca289436 VZ |
442 | wxLogNull noErrors; |
443 | if ( wxExecuteDDE(ddeServer, ddeTopic, ddeCommand) ) | |
444 | { | |
445 | // a dummy PID - this is a hack, of course, but it's well worth | |
446 | // it as we don't open a new server each time we're called | |
447 | // which would be quite bad | |
448 | return -1; | |
449 | } | |
6ba63600 | 450 | } |
5bd3a2da VZ |
451 | } |
452 | else | |
731dd422 | 453 | #endif // wxUSE_IPC |
5bd3a2da VZ |
454 | { |
455 | // no DDE | |
456 | command = cmd; | |
457 | } | |
32592631 | 458 | |
57c208c5 | 459 | #if defined(__WIN32__) && !defined(__TWIN32__) |
cb6827a8 | 460 | |
f6bcfd97 BP |
461 | // the IO redirection is only supported with wxUSE_STREAMS |
462 | BOOL redirect = FALSE; | |
463 | #if wxUSE_STREAMS | |
464 | // the first elements are reading ends, the second are the writing ones | |
465 | HANDLE hpipeStdin[2], | |
f6bcfd97 BP |
466 | hpipeStdout[2], |
467 | hpipeStderr[2]; | |
33ac7e6f | 468 | HANDLE hpipeStdinWrite = INVALID_HANDLE_VALUE; |
8b33ae2d | 469 | |
cd6ce4a9 | 470 | // open the pipes to which child process IO will be redirected if needed |
cd6ce4a9 VZ |
471 | if ( handler && handler->IsRedirected() ) |
472 | { | |
f6bcfd97 | 473 | // default secutiry attributes |
8b33ae2d GL |
474 | SECURITY_ATTRIBUTES security; |
475 | ||
476 | security.nLength = sizeof(security); | |
477 | security.lpSecurityDescriptor = NULL; | |
478 | security.bInheritHandle = TRUE; | |
479 | ||
f6bcfd97 BP |
480 | // create stdin pipe |
481 | if ( !::CreatePipe(&hpipeStdin[0], &hpipeStdin[1], &security, 0) ) | |
cd6ce4a9 VZ |
482 | { |
483 | wxLogSysError(_("Can't create the inter-process read pipe")); | |
8b33ae2d | 484 | |
f6bcfd97 BP |
485 | // indicate failure in both cases |
486 | return sync ? -1 : 0; | |
8b33ae2d GL |
487 | } |
488 | ||
f6bcfd97 BP |
489 | // and a stdout one |
490 | if ( !::CreatePipe(&hpipeStdout[0], &hpipeStdout[1], &security, 0) ) | |
cd6ce4a9 | 491 | { |
f6bcfd97 BP |
492 | ::CloseHandle(hpipeStdin[0]); |
493 | ::CloseHandle(hpipeStdin[1]); | |
cd6ce4a9 VZ |
494 | |
495 | wxLogSysError(_("Can't create the inter-process write pipe")); | |
8b33ae2d | 496 | |
f6bcfd97 | 497 | return sync ? -1 : 0; |
8b33ae2d GL |
498 | } |
499 | ||
f6bcfd97 | 500 | (void)::CreatePipe(&hpipeStderr[0], &hpipeStderr[1], &security, 0); |
8b33ae2d | 501 | |
f6bcfd97 | 502 | redirect = TRUE; |
8b33ae2d | 503 | } |
f6bcfd97 | 504 | #endif // wxUSE_STREAMS |
5bd3a2da | 505 | |
cb6827a8 VZ |
506 | // create the process |
507 | STARTUPINFO si; | |
11c7d5b6 | 508 | wxZeroMemory(si); |
cb6827a8 VZ |
509 | si.cb = sizeof(si); |
510 | ||
f6bcfd97 BP |
511 | #if wxUSE_STREAMS |
512 | if ( redirect ) | |
cb6827a8 | 513 | { |
f6bcfd97 BP |
514 | // when the std IO is redirected, we don't show the (console) process |
515 | // window | |
516 | si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; | |
517 | ||
518 | si.hStdInput = hpipeStdin[0]; | |
519 | si.hStdOutput = hpipeStdout[1]; | |
520 | si.hStdError = hpipeStderr[1]; | |
521 | ||
522 | si.wShowWindow = SW_HIDE; | |
523 | ||
524 | // we must duplicate the handle to the write side of stdin pipe to make | |
525 | // it non inheritable: indeed, we must close hpipeStdin[1] before | |
526 | // launching the child process as otherwise this handle will be | |
527 | // inherited by the child which will never close it and so the pipe | |
528 | // will never be closed and the child will be left stuck in ReadFile() | |
529 | if ( !::DuplicateHandle | |
530 | ( | |
531 | GetCurrentProcess(), | |
532 | hpipeStdin[1], | |
533 | GetCurrentProcess(), | |
534 | &hpipeStdinWrite, | |
535 | 0, // desired access: unused here | |
536 | FALSE, // not inherited | |
537 | DUPLICATE_SAME_ACCESS // same access as for src handle | |
538 | ) ) | |
cd6ce4a9 | 539 | { |
f6bcfd97 | 540 | wxLogLastError(_T("DuplicateHandle")); |
8b33ae2d | 541 | } |
cd6ce4a9 | 542 | |
f6bcfd97 BP |
543 | ::CloseHandle(hpipeStdin[1]); |
544 | } | |
545 | #endif // wxUSE_STREAMS | |
cb6827a8 | 546 | |
f6bcfd97 BP |
547 | PROCESS_INFORMATION pi; |
548 | DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED; | |
549 | ||
550 | bool ok = ::CreateProcess | |
551 | ( | |
552 | NULL, // application name (use only cmd line) | |
553 | (wxChar *) | |
554 | command.c_str(), // full command line | |
555 | NULL, // security attributes: defaults for both | |
556 | NULL, // the process and its main thread | |
557 | redirect, // inherit handles if we use pipes | |
558 | dwFlags, // process creation flags | |
559 | NULL, // environment (use the same) | |
560 | NULL, // current directory (use the same) | |
561 | &si, // startup info (unused here) | |
562 | &pi // process info | |
563 | ) != 0; | |
564 | ||
565 | #if wxUSE_STREAMS | |
566 | // we can close the pipe ends used by child anyhow | |
567 | if ( redirect ) | |
568 | { | |
569 | ::CloseHandle(hpipeStdin[0]); | |
570 | ::CloseHandle(hpipeStdout[1]); | |
571 | ::CloseHandle(hpipeStderr[1]); | |
cb6827a8 | 572 | } |
f6bcfd97 | 573 | #endif // wxUSE_STREAMS |
cb6827a8 | 574 | |
f6bcfd97 | 575 | if ( !ok ) |
5e1febfa | 576 | { |
f6bcfd97 BP |
577 | #if wxUSE_STREAMS |
578 | // close the other handles too | |
579 | if ( redirect ) | |
5e1febfa | 580 | { |
f6bcfd97 BP |
581 | ::CloseHandle(hpipeStdout[0]); |
582 | ::CloseHandle(hpipeStderr[0]); | |
5e1febfa | 583 | } |
f6bcfd97 | 584 | #endif // wxUSE_STREAMS |
5e1febfa | 585 | |
f6bcfd97 BP |
586 | wxLogSysError(_("Execution of command '%s' failed"), command.c_str()); |
587 | ||
588 | return sync ? -1 : 0; | |
589 | } | |
8b33ae2d | 590 | |
f6bcfd97 BP |
591 | #if wxUSE_STREAMS |
592 | if ( redirect ) | |
593 | { | |
8b33ae2d | 594 | // We can now initialize the wxStreams |
f6bcfd97 BP |
595 | wxInputStream *inStream = new wxPipeInputStream(hpipeStdout[0]), |
596 | *errStream = new wxPipeInputStream(hpipeStderr[0]); | |
597 | wxOutputStream *outStream = new wxPipeOutputStream(hpipeStdinWrite); | |
8b33ae2d | 598 | |
f6bcfd97 | 599 | handler->SetPipeStreams(inStream, outStream, errStream); |
8b33ae2d | 600 | } |
f6bcfd97 | 601 | #endif // wxUSE_STREAMS |
8b33ae2d | 602 | |
0d7ea902 | 603 | // register the class for the hidden window used for the notifications |
b568d04f VZ |
604 | if ( !gs_classForHiddenWindow ) |
605 | { | |
606 | gs_classForHiddenWindow = _T("wxHiddenWindow"); | |
607 | ||
608 | WNDCLASS wndclass; | |
609 | wxZeroMemory(wndclass); | |
610 | wndclass.lpfnWndProc = (WNDPROC)wxExecuteWindowCbk; | |
611 | wndclass.hInstance = wxGetInstance(); | |
612 | wndclass.lpszClassName = gs_classForHiddenWindow; | |
613 | ||
614 | if ( !::RegisterClass(&wndclass) ) | |
615 | { | |
f6bcfd97 | 616 | wxLogLastError(wxT("RegisterClass(hidden window)")); |
b568d04f VZ |
617 | } |
618 | } | |
619 | ||
cb6827a8 VZ |
620 | // create a hidden window to receive notification about process |
621 | // termination | |
b568d04f | 622 | HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL, |
0d7ea902 VZ |
623 | WS_OVERLAPPEDWINDOW, |
624 | 0, 0, 0, 0, NULL, | |
cb6827a8 | 625 | (HMENU)NULL, wxGetInstance(), 0); |
223d09f6 | 626 | wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") ); |
e6045e08 | 627 | |
cb6827a8 VZ |
628 | // Alloc data |
629 | wxExecuteData *data = new wxExecuteData; | |
630 | data->hProcess = pi.hProcess; | |
631 | data->dwProcessId = pi.dwProcessId; | |
632 | data->hWnd = hwnd; | |
633 | data->state = sync; | |
e6045e08 VZ |
634 | if ( sync ) |
635 | { | |
5e1febfa VZ |
636 | // handler may be !NULL for capturing program output, but we don't use |
637 | // it wxExecuteData struct in this case | |
e6045e08 VZ |
638 | data->handler = NULL; |
639 | } | |
640 | else | |
641 | { | |
642 | // may be NULL or not | |
643 | data->handler = handler; | |
644 | } | |
cb6827a8 VZ |
645 | |
646 | DWORD tid; | |
647 | HANDLE hThread = ::CreateThread(NULL, | |
648 | 0, | |
649 | (LPTHREAD_START_ROUTINE)wxExecuteThread, | |
650 | (void *)data, | |
651 | 0, | |
652 | &tid); | |
653 | ||
0d7ea902 VZ |
654 | // resume process we created now - whether the thread creation succeeded or |
655 | // not | |
656 | if ( ::ResumeThread(pi.hThread) == (DWORD)-1 ) | |
657 | { | |
658 | // ignore it - what can we do? | |
f6bcfd97 | 659 | wxLogLastError(wxT("ResumeThread in wxExecute")); |
0d7ea902 VZ |
660 | } |
661 | ||
662 | // close unneeded handle | |
663 | if ( !::CloseHandle(pi.hThread) ) | |
f6bcfd97 | 664 | wxLogLastError(wxT("CloseHandle(hThread)")); |
0d7ea902 | 665 | |
cb6827a8 VZ |
666 | if ( !hThread ) |
667 | { | |
f6bcfd97 | 668 | wxLogLastError(wxT("CreateThread in wxExecute")); |
cb6827a8 VZ |
669 | |
670 | DestroyWindow(hwnd); | |
671 | delete data; | |
672 | ||
673 | // the process still started up successfully... | |
674 | return pi.dwProcessId; | |
675 | } | |
e6045e08 | 676 | |
f6bcfd97 BP |
677 | ::CloseHandle(hThread); |
678 | ||
731dd422 | 679 | #if wxUSE_IPC |
5bd3a2da VZ |
680 | // second part of DDE hack: now establish the DDE conversation with the |
681 | // just launched process | |
ca289436 | 682 | if ( !ddeServer.empty() ) |
5bd3a2da | 683 | { |
ca289436 VZ |
684 | bool ok; |
685 | ||
686 | // give the process the time to init itself | |
687 | // | |
688 | // we use a very big timeout hoping that WaitForInputIdle() will return | |
689 | // much sooner, but not INFINITE just in case the process hangs | |
690 | // completely - like this we will regain control sooner or later | |
691 | switch ( ::WaitForInputIdle(pi.hProcess, 10000 /* 10 seconds */) ) | |
6ba63600 | 692 | { |
ca289436 VZ |
693 | default: |
694 | wxFAIL_MSG( _T("unexpected WaitForInputIdle() return code") ); | |
695 | // fall through | |
6ba63600 | 696 | |
ca289436 VZ |
697 | case -1: |
698 | wxLogLastError(_T("WaitForInputIdle() in wxExecute")); | |
6ba63600 | 699 | |
ca289436 VZ |
700 | case WAIT_TIMEOUT: |
701 | wxLogDebug(_T("Timeout too small in WaitForInputIdle")); | |
702 | ||
703 | ok = FALSE; | |
704 | break; | |
705 | ||
706 | case 0: | |
707 | // ok, process ready to accept DDE requests | |
708 | ok = wxExecuteDDE(ddeServer, ddeTopic, ddeCommand); | |
6ba63600 | 709 | } |
5bd3a2da | 710 | } |
731dd422 | 711 | #endif // wxUSE_IPC |
5bd3a2da | 712 | |
cb6827a8 VZ |
713 | if ( !sync ) |
714 | { | |
715 | // clean up will be done when the process terminates | |
e6045e08 VZ |
716 | |
717 | // return the pid | |
cb6827a8 VZ |
718 | return pi.dwProcessId; |
719 | } | |
e6045e08 | 720 | |
0d7ea902 VZ |
721 | // waiting until command executed (disable everything while doing it) |
722 | #if wxUSE_GUI | |
cd6ce4a9 VZ |
723 | { |
724 | wxBusyCursor bc; | |
725 | ||
726 | wxWindowDisabler wd; | |
0d7ea902 VZ |
727 | #endif // wxUSE_GUI |
728 | ||
cb6827a8 VZ |
729 | while ( data->state ) |
730 | wxYield(); | |
e6045e08 | 731 | |
0d7ea902 | 732 | #if wxUSE_GUI |
cd6ce4a9 | 733 | } |
0d7ea902 VZ |
734 | #endif // wxUSE_GUI |
735 | ||
e6045e08 | 736 | DWORD dwExitCode = data->dwExitCode; |
cb6827a8 VZ |
737 | delete data; |
738 | ||
e6045e08 VZ |
739 | // return the exit code |
740 | return dwExitCode; | |
cb6827a8 VZ |
741 | #else // Win16 |
742 | long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW); | |
743 | if (instanceID < 32) return(0); | |
e6045e08 | 744 | |
cb6827a8 VZ |
745 | if (sync) { |
746 | int running; | |
747 | do { | |
748 | wxYield(); | |
27a9bd48 | 749 | running = GetModuleUsage((HINSTANCE)instanceID); |
cb6827a8 VZ |
750 | } while (running); |
751 | } | |
752 | ||
753 | return(instanceID); | |
754 | #endif // Win16/32 | |
32592631 | 755 | } |
c740f496 GL |
756 | |
757 | long wxExecute(char **argv, bool sync, wxProcess *handler) | |
758 | { | |
cb6827a8 | 759 | wxString command; |
e6045e08 | 760 | |
cb6827a8 VZ |
761 | while ( *argv != NULL ) |
762 | { | |
763 | command << *argv++ << ' '; | |
764 | } | |
765 | ||
766 | command.RemoveLast(); | |
767 | ||
768 | return wxExecute(command, sync, handler); | |
c740f496 | 769 | } |
006162a9 | 770 |