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