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