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