]>
Commit | Line | Data |
---|---|---|
32592631 | 1 | ///////////////////////////////////////////////////////////////////////////// |
b568d04f | 2 | // Name: msw/utilsexec.cpp |
79066131 | 3 | // Purpose: wxExecute implementation for MSW |
32592631 GL |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
79066131 | 8 | // Copyright: (c) 1998-2002 wxWindows dev team |
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); | |
79066131 | 134 | virtual ~wxPipeInputStream(); |
8b33ae2d | 135 | |
79066131 VZ |
136 | // returns TRUE if the pipe is still opened |
137 | bool IsOpened() const { return m_hInput != INVALID_HANDLE_VALUE; } | |
138 | ||
139 | // returns TRUE if there is any data to be read from the pipe | |
140 | bool IsAvailable() const; | |
f6bcfd97 | 141 | |
8b33ae2d GL |
142 | protected: |
143 | size_t OnSysRead(void *buffer, size_t len); | |
144 | ||
145 | protected: | |
146 | HANDLE m_hInput; | |
147 | }; | |
148 | ||
f6bcfd97 BP |
149 | class wxPipeOutputStream: public wxOutputStream |
150 | { | |
8b33ae2d GL |
151 | public: |
152 | wxPipeOutputStream(HANDLE hOutput); | |
79066131 | 153 | virtual ~wxPipeOutputStream(); |
8b33ae2d GL |
154 | |
155 | protected: | |
156 | size_t OnSysWrite(const void *buffer, size_t len); | |
157 | ||
158 | protected: | |
159 | HANDLE m_hOutput; | |
160 | }; | |
161 | ||
79066131 VZ |
162 | // define this to let wxexec.cpp know that we know what we're doing |
163 | #define _WX_USED_BY_WXEXECUTE_ | |
164 | #include "../common/execcmn.cpp" | |
165 | ||
166 | // ---------------------------------------------------------------------------- | |
167 | // wxPipe represents a Win32 anonymous pipe | |
168 | // ---------------------------------------------------------------------------- | |
169 | ||
170 | class wxPipe | |
171 | { | |
172 | public: | |
173 | // the symbolic names for the pipe ends | |
174 | enum Direction | |
175 | { | |
176 | Read, | |
177 | Write | |
178 | }; | |
179 | ||
180 | // default ctor doesn't do anything | |
181 | wxPipe() { m_handles[Read] = m_handles[Write] = INVALID_HANDLE_VALUE; } | |
182 | ||
183 | // create the pipe, return TRUE if ok, FALSE on error | |
184 | bool Create() | |
185 | { | |
186 | // default secutiry attributes | |
187 | SECURITY_ATTRIBUTES security; | |
188 | ||
189 | security.nLength = sizeof(security); | |
190 | security.lpSecurityDescriptor = NULL; | |
191 | security.bInheritHandle = TRUE; // to pass it to the child | |
192 | ||
193 | if ( !::CreatePipe(&m_handles[0], &m_handles[1], &security, 0) ) | |
194 | { | |
195 | wxLogSysError(_("Failed to create an anonymous pipe")); | |
196 | ||
197 | return FALSE; | |
198 | } | |
199 | ||
200 | return TRUE; | |
201 | } | |
202 | ||
203 | // return TRUE if we were created successfully | |
204 | bool IsOk() const { return m_handles[Read] != INVALID_HANDLE_VALUE; } | |
205 | ||
206 | // return the descriptor for one of the pipe ends | |
207 | HANDLE operator[](Direction which) const | |
208 | { | |
209 | wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_handles), | |
210 | _T("invalid pipe index") ); | |
211 | ||
212 | return m_handles[which]; | |
213 | } | |
214 | ||
215 | // detach a descriptor, meaning that the pipe dtor won't close it, and | |
216 | // return it | |
217 | HANDLE Detach(Direction which) | |
218 | { | |
219 | wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_handles), | |
220 | _T("invalid pipe index") ); | |
221 | ||
222 | HANDLE handle = m_handles[which]; | |
223 | m_handles[which] = INVALID_HANDLE_VALUE; | |
224 | ||
225 | return handle; | |
226 | } | |
227 | ||
228 | // close the pipe descriptors | |
229 | void Close() | |
230 | { | |
231 | for ( size_t n = 0; n < WXSIZEOF(m_handles); n++ ) | |
232 | { | |
233 | if ( m_handles[n] != INVALID_HANDLE_VALUE ) | |
234 | { | |
235 | ::CloseHandle(m_handles[n]); | |
236 | m_handles[n] = INVALID_HANDLE_VALUE; | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | // dtor closes the pipe descriptors | |
242 | ~wxPipe() { Close(); } | |
243 | ||
244 | private: | |
245 | HANDLE m_handles[2]; | |
246 | }; | |
247 | ||
248 | #endif // wxUSE_STREAMS | |
249 | ||
250 | // ============================================================================ | |
251 | // implementation | |
252 | // ============================================================================ | |
253 | ||
254 | #ifdef __WIN32__ | |
255 | ||
256 | // ---------------------------------------------------------------------------- | |
257 | // process termination detecting support | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
260 | // thread function for the thread monitoring the process termination | |
261 | static DWORD __stdcall wxExecuteThread(void *arg) | |
262 | { | |
263 | wxExecuteData *data = (wxExecuteData*)arg; | |
264 | ||
265 | if ( ::WaitForSingleObject(data->hProcess, INFINITE) != WAIT_OBJECT_0 ) | |
266 | { | |
267 | wxLogDebug(_T("Waiting for the process termination failed!")); | |
268 | } | |
269 | ||
270 | // get the exit code | |
271 | if ( !::GetExitCodeProcess(data->hProcess, &data->dwExitCode) ) | |
272 | { | |
273 | wxLogLastError(wxT("GetExitCodeProcess")); | |
274 | } | |
275 | ||
276 | wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE, | |
277 | wxT("process should have terminated") ); | |
278 | ||
279 | // send a message indicating process termination to the window | |
280 | ::SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data); | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | // window procedure of a hidden window which is created just to receive | |
286 | // the notification message when a process exits | |
287 | LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message, | |
288 | WPARAM wParam, LPARAM lParam) | |
289 | { | |
290 | if ( message == wxWM_PROC_TERMINATED ) | |
291 | { | |
292 | DestroyWindow(hWnd); // we don't need it any more | |
293 | ||
294 | wxExecuteData *data = (wxExecuteData *)lParam; | |
295 | if ( data->handler ) | |
296 | { | |
297 | data->handler->OnTerminate((int)data->dwProcessId, | |
298 | (int)data->dwExitCode); | |
299 | } | |
300 | ||
301 | if ( data->state ) | |
302 | { | |
303 | // we're executing synchronously, tell the waiting thread | |
304 | // that the process finished | |
305 | data->state = 0; | |
306 | } | |
307 | else | |
308 | { | |
309 | // asynchronous execution - we should do the clean up | |
310 | delete data; | |
311 | } | |
312 | ||
313 | return 0; | |
314 | } | |
315 | else | |
316 | { | |
317 | return DefWindowProc(hWnd, message, wParam, lParam); | |
318 | } | |
319 | } | |
320 | ||
321 | // ============================================================================ | |
322 | // implementation of IO redirection support classes | |
323 | // ============================================================================ | |
324 | ||
325 | #if wxUSE_STREAMS | |
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // wxPipeInputStreams | |
329 | // ---------------------------------------------------------------------------- | |
8b33ae2d GL |
330 | |
331 | wxPipeInputStream::wxPipeInputStream(HANDLE hInput) | |
332 | { | |
333 | m_hInput = hInput; | |
cd6ce4a9 | 334 | } |
8b33ae2d GL |
335 | |
336 | wxPipeInputStream::~wxPipeInputStream() | |
337 | { | |
79066131 VZ |
338 | if ( m_hInput != INVALID_HANDLE_VALUE ) |
339 | ::CloseHandle(m_hInput); | |
8b33ae2d GL |
340 | } |
341 | ||
79066131 | 342 | bool wxPipeInputStream::IsAvailable() const |
8b33ae2d | 343 | { |
79066131 VZ |
344 | if ( !IsOpened() ) |
345 | return FALSE; | |
346 | ||
f6bcfd97 BP |
347 | DWORD nAvailable; |
348 | ||
349 | // function name is misleading, it works with anon pipes as well | |
350 | DWORD rc = ::PeekNamedPipe | |
351 | ( | |
352 | m_hInput, // handle | |
353 | NULL, 0, // ptr to buffer and its size | |
354 | NULL, // [out] bytes read | |
355 | &nAvailable, // [out] bytes available | |
356 | NULL // [out] bytes left | |
357 | ); | |
358 | ||
359 | if ( !rc ) | |
360 | { | |
361 | if ( ::GetLastError() != ERROR_BROKEN_PIPE ) | |
362 | { | |
363 | // unexpected error | |
364 | wxLogLastError(_T("PeekNamedPipe")); | |
365 | } | |
8b33ae2d | 366 | |
f6bcfd97 BP |
367 | // don't try to continue reading from a pipe if an error occured or if |
368 | // it had been closed | |
79066131 VZ |
369 | ::CloseHandle(m_hInput); |
370 | ||
371 | wxConstCast(this, wxPipeInputStream)->m_hInput = INVALID_HANDLE_VALUE; | |
372 | ||
373 | return FALSE; | |
f6bcfd97 | 374 | } |
79066131 VZ |
375 | |
376 | return nAvailable != 0; | |
f6bcfd97 BP |
377 | } |
378 | ||
379 | size_t wxPipeInputStream::OnSysRead(void *buffer, size_t len) | |
380 | { | |
381 | // reading from a pipe may block if there is no more data, always check for | |
382 | // EOF first | |
79066131 VZ |
383 | if ( !IsAvailable() ) |
384 | { | |
385 | m_lasterror = wxSTREAM_EOF; | |
386 | ||
f6bcfd97 | 387 | return 0; |
79066131 VZ |
388 | } |
389 | ||
390 | m_lasterror = wxSTREAM_NOERROR; | |
f6bcfd97 BP |
391 | |
392 | DWORD bytesRead; | |
393 | if ( !::ReadFile(m_hInput, buffer, len, &bytesRead, NULL) ) | |
394 | { | |
395 | if ( ::GetLastError() == ERROR_BROKEN_PIPE ) | |
8b33ae2d GL |
396 | m_lasterror = wxSTREAM_EOF; |
397 | else | |
398 | m_lasterror = wxSTREAM_READ_ERROR; | |
399 | } | |
f6bcfd97 | 400 | |
8b33ae2d GL |
401 | return bytesRead; |
402 | } | |
403 | ||
79066131 | 404 | // ---------------------------------------------------------------------------- |
8b33ae2d | 405 | // wxPipeOutputStream |
79066131 | 406 | // ---------------------------------------------------------------------------- |
8b33ae2d GL |
407 | |
408 | wxPipeOutputStream::wxPipeOutputStream(HANDLE hOutput) | |
409 | { | |
410 | m_hOutput = hOutput; | |
cd6ce4a9 | 411 | } |
8b33ae2d GL |
412 | |
413 | wxPipeOutputStream::~wxPipeOutputStream() | |
414 | { | |
415 | ::CloseHandle(m_hOutput); | |
416 | } | |
417 | ||
418 | size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len) | |
419 | { | |
420 | DWORD bytesRead; | |
421 | ||
422 | m_lasterror = wxSTREAM_NOERROR; | |
f6bcfd97 BP |
423 | if ( !::WriteFile(m_hOutput, buffer, len, &bytesRead, NULL) ) |
424 | { | |
425 | if ( ::GetLastError() == ERROR_BROKEN_PIPE ) | |
8b33ae2d GL |
426 | m_lasterror = wxSTREAM_EOF; |
427 | else | |
428 | m_lasterror = wxSTREAM_READ_ERROR; | |
429 | } | |
f6bcfd97 | 430 | |
8b33ae2d GL |
431 | return bytesRead; |
432 | } | |
433 | ||
79066131 VZ |
434 | #endif // wxUSE_STREAMS |
435 | ||
436 | #endif // Win32 | |
8b33ae2d | 437 | |
b568d04f | 438 | // ============================================================================ |
79066131 | 439 | // wxExecute functions family |
b568d04f | 440 | // ============================================================================ |
5260b1c5 | 441 | |
ca289436 VZ |
442 | #if wxUSE_IPC |
443 | ||
444 | // connect to the given server via DDE and ask it to execute the command | |
445 | static bool wxExecuteDDE(const wxString& ddeServer, | |
446 | const wxString& ddeTopic, | |
447 | const wxString& ddeCommand) | |
448 | { | |
449 | bool ok; | |
450 | ||
451 | wxDDEClient client; | |
452 | wxConnectionBase *conn = client.MakeConnection(_T(""), | |
453 | ddeServer, | |
454 | ddeTopic); | |
455 | if ( !conn ) | |
456 | { | |
457 | ok = FALSE; | |
458 | } | |
459 | else // connected to DDE server | |
460 | { | |
461 | // the added complication here is that although most | |
462 | // programs use XTYP_EXECUTE for their DDE API, some | |
463 | // important ones - like IE and other MS stuff - use | |
464 | // XTYP_REQUEST! | |
465 | // | |
466 | // so we try it first and then the other one if it | |
467 | // failed | |
468 | { | |
469 | wxLogNull noErrors; | |
470 | ok = conn->Request(ddeCommand) != NULL; | |
471 | } | |
472 | ||
473 | if ( !ok ) | |
474 | { | |
475 | // now try execute - but show the errors | |
476 | ok = conn->Execute(ddeCommand); | |
477 | } | |
478 | } | |
479 | ||
480 | return ok; | |
481 | } | |
482 | ||
483 | #endif // wxUSE_IPC | |
484 | ||
fbf456aa | 485 | long wxExecute(const wxString& cmd, int flags, wxProcess *handler) |
32592631 | 486 | { |
5bd3a2da VZ |
487 | wxCHECK_MSG( !!cmd, 0, wxT("empty command in wxExecute") ); |
488 | ||
039f62f4 | 489 | wxString command; |
6ba63600 | 490 | |
731dd422 | 491 | #if wxUSE_IPC |
5bd3a2da VZ |
492 | // DDE hack: this is really not pretty, but we need to allow this for |
493 | // transparent handling of DDE servers in wxMimeTypesManager. Usually it | |
494 | // returns the command which should be run to view/open/... a file of the | |
495 | // given type. Sometimes, however, this command just launches the server | |
496 | // and an additional DDE request must be made to really open the file. To | |
497 | // keep all this well hidden from the application, we allow a special form | |
6ba63600 | 498 | // of command: WX_DDE#<command>#DDE_SERVER#DDE_TOPIC#DDE_COMMAND in which |
5bd3a2da | 499 | // case we execute just <command> and process the rest below |
039f62f4 | 500 | wxString ddeServer, ddeTopic, ddeCommand; |
5bd3a2da VZ |
501 | static const size_t lenDdePrefix = 7; // strlen("WX_DDE:") |
502 | if ( cmd.Left(lenDdePrefix) == _T("WX_DDE#") ) | |
503 | { | |
ca289436 VZ |
504 | // speed up the concatenations below |
505 | ddeServer.reserve(256); | |
506 | ddeTopic.reserve(256); | |
507 | ddeCommand.reserve(256); | |
508 | ||
5bd3a2da VZ |
509 | const wxChar *p = cmd.c_str() + 7; |
510 | while ( *p && *p != _T('#') ) | |
511 | { | |
512 | command += *p++; | |
513 | } | |
514 | ||
515 | if ( *p ) | |
516 | { | |
517 | // skip '#' | |
518 | p++; | |
519 | } | |
520 | else | |
521 | { | |
522 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
523 | } | |
524 | ||
525 | while ( *p && *p != _T('#') ) | |
526 | { | |
527 | ddeServer += *p++; | |
528 | } | |
529 | ||
530 | if ( *p ) | |
531 | { | |
532 | // skip '#' | |
533 | p++; | |
534 | } | |
535 | else | |
536 | { | |
537 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
538 | } | |
539 | ||
540 | while ( *p && *p != _T('#') ) | |
541 | { | |
542 | ddeTopic += *p++; | |
543 | } | |
544 | ||
545 | if ( *p ) | |
546 | { | |
547 | // skip '#' | |
548 | p++; | |
549 | } | |
550 | else | |
551 | { | |
552 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
553 | } | |
554 | ||
555 | while ( *p ) | |
556 | { | |
557 | ddeCommand += *p++; | |
558 | } | |
6ba63600 | 559 | |
ca289436 VZ |
560 | // if we want to just launch the program and not wait for its |
561 | // termination, try to execute DDE command right now, it can succeed if | |
562 | // the process is already running - but as it fails if it's not | |
563 | // running, suppress any errors it might generate | |
fbf456aa | 564 | if ( !(flags & wxEXEC_SYNC) ) |
6ba63600 | 565 | { |
ca289436 VZ |
566 | wxLogNull noErrors; |
567 | if ( wxExecuteDDE(ddeServer, ddeTopic, ddeCommand) ) | |
568 | { | |
569 | // a dummy PID - this is a hack, of course, but it's well worth | |
570 | // it as we don't open a new server each time we're called | |
571 | // which would be quite bad | |
572 | return -1; | |
573 | } | |
6ba63600 | 574 | } |
5bd3a2da VZ |
575 | } |
576 | else | |
731dd422 | 577 | #endif // wxUSE_IPC |
5bd3a2da VZ |
578 | { |
579 | // no DDE | |
580 | command = cmd; | |
581 | } | |
32592631 | 582 | |
57c208c5 | 583 | #if defined(__WIN32__) && !defined(__TWIN32__) |
cb6827a8 | 584 | |
f6bcfd97 BP |
585 | // the IO redirection is only supported with wxUSE_STREAMS |
586 | BOOL redirect = FALSE; | |
79066131 | 587 | |
f6bcfd97 | 588 | #if wxUSE_STREAMS |
79066131 VZ |
589 | wxPipe pipeIn, pipeOut, pipeErr; |
590 | ||
591 | // we'll save here the copy of pipeIn[Write] | |
33ac7e6f | 592 | HANDLE hpipeStdinWrite = INVALID_HANDLE_VALUE; |
8b33ae2d | 593 | |
cd6ce4a9 | 594 | // open the pipes to which child process IO will be redirected if needed |
cd6ce4a9 VZ |
595 | if ( handler && handler->IsRedirected() ) |
596 | { | |
79066131 VZ |
597 | // create pipes for redirecting stdin, stdout and stderr |
598 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) | |
cd6ce4a9 | 599 | { |
79066131 | 600 | wxLogSysError(_("Failed to redirect the child process IO")); |
8b33ae2d | 601 | |
fbf456aa VZ |
602 | // indicate failure: we need to return different error code |
603 | // depending on the sync flag | |
604 | return flags & wxEXEC_SYNC ? -1 : 0; | |
8b33ae2d GL |
605 | } |
606 | ||
f6bcfd97 | 607 | redirect = TRUE; |
8b33ae2d | 608 | } |
f6bcfd97 | 609 | #endif // wxUSE_STREAMS |
5bd3a2da | 610 | |
cb6827a8 VZ |
611 | // create the process |
612 | STARTUPINFO si; | |
11c7d5b6 | 613 | wxZeroMemory(si); |
cb6827a8 VZ |
614 | si.cb = sizeof(si); |
615 | ||
f6bcfd97 BP |
616 | #if wxUSE_STREAMS |
617 | if ( redirect ) | |
cb6827a8 | 618 | { |
fbf456aa | 619 | si.dwFlags = STARTF_USESTDHANDLES; |
f6bcfd97 | 620 | |
79066131 VZ |
621 | si.hStdInput = pipeIn[wxPipe::Read]; |
622 | si.hStdOutput = pipeOut[wxPipe::Write]; | |
623 | si.hStdError = pipeErr[wxPipe::Write]; | |
f6bcfd97 | 624 | |
fbf456aa VZ |
625 | // when the std IO is redirected, we don't show the (console) process |
626 | // window by default, but this can be overridden by the caller by | |
627 | // specifying wxEXEC_NOHIDE flag | |
628 | if ( !(flags & wxEXEC_NOHIDE) ) | |
629 | { | |
630 | si.dwFlags |= STARTF_USESHOWWINDOW; | |
631 | si.wShowWindow = SW_HIDE; | |
632 | } | |
f6bcfd97 BP |
633 | |
634 | // we must duplicate the handle to the write side of stdin pipe to make | |
79066131 VZ |
635 | // it non inheritable: indeed, we must close the writing end of pipeIn |
636 | // before launching the child process as otherwise this handle will be | |
f6bcfd97 BP |
637 | // inherited by the child which will never close it and so the pipe |
638 | // will never be closed and the child will be left stuck in ReadFile() | |
79066131 | 639 | HANDLE pipeInWrite = pipeIn.Detach(wxPipe::Write); |
f6bcfd97 BP |
640 | if ( !::DuplicateHandle |
641 | ( | |
79066131 VZ |
642 | ::GetCurrentProcess(), |
643 | pipeInWrite, | |
644 | ::GetCurrentProcess(), | |
f6bcfd97 BP |
645 | &hpipeStdinWrite, |
646 | 0, // desired access: unused here | |
647 | FALSE, // not inherited | |
648 | DUPLICATE_SAME_ACCESS // same access as for src handle | |
649 | ) ) | |
cd6ce4a9 | 650 | { |
f6bcfd97 | 651 | wxLogLastError(_T("DuplicateHandle")); |
8b33ae2d | 652 | } |
cd6ce4a9 | 653 | |
79066131 | 654 | ::CloseHandle(pipeInWrite); |
f6bcfd97 BP |
655 | } |
656 | #endif // wxUSE_STREAMS | |
cb6827a8 | 657 | |
f6bcfd97 BP |
658 | PROCESS_INFORMATION pi; |
659 | DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED; | |
660 | ||
661 | bool ok = ::CreateProcess | |
662 | ( | |
663 | NULL, // application name (use only cmd line) | |
664 | (wxChar *) | |
665 | command.c_str(), // full command line | |
666 | NULL, // security attributes: defaults for both | |
667 | NULL, // the process and its main thread | |
668 | redirect, // inherit handles if we use pipes | |
669 | dwFlags, // process creation flags | |
670 | NULL, // environment (use the same) | |
671 | NULL, // current directory (use the same) | |
672 | &si, // startup info (unused here) | |
673 | &pi // process info | |
674 | ) != 0; | |
675 | ||
676 | #if wxUSE_STREAMS | |
677 | // we can close the pipe ends used by child anyhow | |
678 | if ( redirect ) | |
679 | { | |
79066131 VZ |
680 | ::CloseHandle(pipeIn.Detach(wxPipe::Read)); |
681 | ::CloseHandle(pipeOut.Detach(wxPipe::Write)); | |
682 | ::CloseHandle(pipeErr.Detach(wxPipe::Write)); | |
cb6827a8 | 683 | } |
f6bcfd97 | 684 | #endif // wxUSE_STREAMS |
cb6827a8 | 685 | |
f6bcfd97 | 686 | if ( !ok ) |
5e1febfa | 687 | { |
f6bcfd97 BP |
688 | #if wxUSE_STREAMS |
689 | // close the other handles too | |
690 | if ( redirect ) | |
5e1febfa | 691 | { |
79066131 VZ |
692 | ::CloseHandle(pipeOut.Detach(wxPipe::Read)); |
693 | ::CloseHandle(pipeErr.Detach(wxPipe::Read)); | |
5e1febfa | 694 | } |
f6bcfd97 | 695 | #endif // wxUSE_STREAMS |
5e1febfa | 696 | |
f6bcfd97 BP |
697 | wxLogSysError(_("Execution of command '%s' failed"), command.c_str()); |
698 | ||
fbf456aa | 699 | return flags & wxEXEC_SYNC ? -1 : 0; |
f6bcfd97 | 700 | } |
8b33ae2d | 701 | |
f6bcfd97 | 702 | #if wxUSE_STREAMS |
79066131 VZ |
703 | // the input buffer bufOut is connected to stdout, this is why it is |
704 | // called bufOut and not bufIn | |
705 | wxStreamTempInputBuffer bufOut, | |
706 | bufErr; | |
707 | ||
f6bcfd97 BP |
708 | if ( redirect ) |
709 | { | |
8b33ae2d | 710 | // We can now initialize the wxStreams |
79066131 VZ |
711 | wxPipeInputStream * |
712 | outStream = new wxPipeInputStream(pipeOut.Detach(wxPipe::Read)); | |
713 | wxPipeInputStream * | |
714 | errStream = new wxPipeInputStream(pipeErr.Detach(wxPipe::Read)); | |
715 | wxPipeOutputStream * | |
716 | inStream = new wxPipeOutputStream(hpipeStdinWrite); | |
717 | ||
718 | handler->SetPipeStreams(outStream, inStream, errStream); | |
8b33ae2d | 719 | |
79066131 VZ |
720 | bufOut.Init(outStream); |
721 | bufErr.Init(errStream); | |
8b33ae2d | 722 | } |
f6bcfd97 | 723 | #endif // wxUSE_STREAMS |
8b33ae2d | 724 | |
0d7ea902 | 725 | // register the class for the hidden window used for the notifications |
b568d04f VZ |
726 | if ( !gs_classForHiddenWindow ) |
727 | { | |
728 | gs_classForHiddenWindow = _T("wxHiddenWindow"); | |
729 | ||
730 | WNDCLASS wndclass; | |
731 | wxZeroMemory(wndclass); | |
732 | wndclass.lpfnWndProc = (WNDPROC)wxExecuteWindowCbk; | |
733 | wndclass.hInstance = wxGetInstance(); | |
734 | wndclass.lpszClassName = gs_classForHiddenWindow; | |
735 | ||
736 | if ( !::RegisterClass(&wndclass) ) | |
737 | { | |
f6bcfd97 | 738 | wxLogLastError(wxT("RegisterClass(hidden window)")); |
b568d04f VZ |
739 | } |
740 | } | |
741 | ||
cb6827a8 VZ |
742 | // create a hidden window to receive notification about process |
743 | // termination | |
b568d04f | 744 | HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL, |
0d7ea902 VZ |
745 | WS_OVERLAPPEDWINDOW, |
746 | 0, 0, 0, 0, NULL, | |
cb6827a8 | 747 | (HMENU)NULL, wxGetInstance(), 0); |
223d09f6 | 748 | wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") ); |
e6045e08 | 749 | |
cb6827a8 VZ |
750 | // Alloc data |
751 | wxExecuteData *data = new wxExecuteData; | |
752 | data->hProcess = pi.hProcess; | |
753 | data->dwProcessId = pi.dwProcessId; | |
754 | data->hWnd = hwnd; | |
fbf456aa VZ |
755 | data->state = (flags & wxEXEC_SYNC) != 0; |
756 | if ( flags & wxEXEC_SYNC ) | |
e6045e08 | 757 | { |
5e1febfa VZ |
758 | // handler may be !NULL for capturing program output, but we don't use |
759 | // it wxExecuteData struct in this case | |
e6045e08 VZ |
760 | data->handler = NULL; |
761 | } | |
762 | else | |
763 | { | |
764 | // may be NULL or not | |
765 | data->handler = handler; | |
766 | } | |
cb6827a8 VZ |
767 | |
768 | DWORD tid; | |
769 | HANDLE hThread = ::CreateThread(NULL, | |
770 | 0, | |
19193a2c | 771 | wxExecuteThread, |
cb6827a8 VZ |
772 | (void *)data, |
773 | 0, | |
774 | &tid); | |
775 | ||
0d7ea902 VZ |
776 | // resume process we created now - whether the thread creation succeeded or |
777 | // not | |
778 | if ( ::ResumeThread(pi.hThread) == (DWORD)-1 ) | |
779 | { | |
780 | // ignore it - what can we do? | |
f6bcfd97 | 781 | wxLogLastError(wxT("ResumeThread in wxExecute")); |
0d7ea902 VZ |
782 | } |
783 | ||
784 | // close unneeded handle | |
785 | if ( !::CloseHandle(pi.hThread) ) | |
f6bcfd97 | 786 | wxLogLastError(wxT("CloseHandle(hThread)")); |
0d7ea902 | 787 | |
cb6827a8 VZ |
788 | if ( !hThread ) |
789 | { | |
f6bcfd97 | 790 | wxLogLastError(wxT("CreateThread in wxExecute")); |
cb6827a8 VZ |
791 | |
792 | DestroyWindow(hwnd); | |
793 | delete data; | |
794 | ||
795 | // the process still started up successfully... | |
796 | return pi.dwProcessId; | |
797 | } | |
e6045e08 | 798 | |
f6bcfd97 BP |
799 | ::CloseHandle(hThread); |
800 | ||
731dd422 | 801 | #if wxUSE_IPC |
5bd3a2da VZ |
802 | // second part of DDE hack: now establish the DDE conversation with the |
803 | // just launched process | |
ca289436 | 804 | if ( !ddeServer.empty() ) |
5bd3a2da | 805 | { |
ca289436 VZ |
806 | bool ok; |
807 | ||
808 | // give the process the time to init itself | |
809 | // | |
810 | // we use a very big timeout hoping that WaitForInputIdle() will return | |
811 | // much sooner, but not INFINITE just in case the process hangs | |
812 | // completely - like this we will regain control sooner or later | |
813 | switch ( ::WaitForInputIdle(pi.hProcess, 10000 /* 10 seconds */) ) | |
6ba63600 | 814 | { |
ca289436 VZ |
815 | default: |
816 | wxFAIL_MSG( _T("unexpected WaitForInputIdle() return code") ); | |
817 | // fall through | |
6ba63600 | 818 | |
ca289436 VZ |
819 | case -1: |
820 | wxLogLastError(_T("WaitForInputIdle() in wxExecute")); | |
6ba63600 | 821 | |
ca289436 VZ |
822 | case WAIT_TIMEOUT: |
823 | wxLogDebug(_T("Timeout too small in WaitForInputIdle")); | |
824 | ||
825 | ok = FALSE; | |
826 | break; | |
827 | ||
828 | case 0: | |
829 | // ok, process ready to accept DDE requests | |
830 | ok = wxExecuteDDE(ddeServer, ddeTopic, ddeCommand); | |
6ba63600 | 831 | } |
5bd3a2da | 832 | } |
731dd422 | 833 | #endif // wxUSE_IPC |
5bd3a2da | 834 | |
fbf456aa | 835 | if ( !(flags & wxEXEC_SYNC) ) |
cb6827a8 VZ |
836 | { |
837 | // clean up will be done when the process terminates | |
e6045e08 VZ |
838 | |
839 | // return the pid | |
cb6827a8 VZ |
840 | return pi.dwProcessId; |
841 | } | |
e6045e08 | 842 | |
0d7ea902 VZ |
843 | // waiting until command executed (disable everything while doing it) |
844 | #if wxUSE_GUI | |
cd6ce4a9 VZ |
845 | { |
846 | wxBusyCursor bc; | |
847 | ||
848 | wxWindowDisabler wd; | |
0d7ea902 VZ |
849 | #endif // wxUSE_GUI |
850 | ||
79066131 VZ |
851 | // wait until the child process terminates |
852 | while ( data->state ) | |
853 | { | |
854 | #if wxUSE_STREAMS | |
855 | bufOut.Update(); | |
856 | bufErr.Update(); | |
857 | #endif // wxUSE_STREAMS | |
858 | ||
859 | // don't eat 100% of the CPU -- ugly but anything else requires | |
860 | // real async IO which we don't have for the moment | |
861 | ::Sleep(50); | |
862 | ||
863 | // repaint the GUI | |
864 | wxYield(); | |
865 | } | |
e6045e08 | 866 | |
0d7ea902 | 867 | #if wxUSE_GUI |
cd6ce4a9 | 868 | } |
0d7ea902 VZ |
869 | #endif // wxUSE_GUI |
870 | ||
e6045e08 | 871 | DWORD dwExitCode = data->dwExitCode; |
cb6827a8 VZ |
872 | delete data; |
873 | ||
e6045e08 VZ |
874 | // return the exit code |
875 | return dwExitCode; | |
cb6827a8 VZ |
876 | #else // Win16 |
877 | long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW); | |
fbf456aa VZ |
878 | if (instanceID < 32) |
879 | return flags & wxEXEC_SYNC ? -1 : 0; | |
e6045e08 | 880 | |
fbf456aa VZ |
881 | if ( flags & wxEXEC_SYNC ) |
882 | { | |
cb6827a8 | 883 | int running; |
fbf456aa VZ |
884 | do |
885 | { | |
cb6827a8 | 886 | wxYield(); |
27a9bd48 | 887 | running = GetModuleUsage((HINSTANCE)instanceID); |
cb6827a8 VZ |
888 | } while (running); |
889 | } | |
890 | ||
fbf456aa | 891 | return instanceID; |
cb6827a8 | 892 | #endif // Win16/32 |
32592631 | 893 | } |
c740f496 | 894 | |
fbf456aa | 895 | long wxExecute(char **argv, int flags, wxProcess *handler) |
c740f496 | 896 | { |
cb6827a8 | 897 | wxString command; |
e6045e08 | 898 | |
cb6827a8 VZ |
899 | while ( *argv != NULL ) |
900 | { | |
901 | command << *argv++ << ' '; | |
902 | } | |
903 | ||
904 | command.RemoveLast(); | |
905 | ||
fbf456aa | 906 | return wxExecute(command, flags, handler); |
c740f496 | 907 | } |
006162a9 | 908 |