+#endif // wxUSE_IPC
+
+long wxExecute(const wxString& cmd, int flags, wxProcess *handler)
+{
+ wxCHECK_MSG( !!cmd, 0, wxT("empty command in wxExecute") );
+
+#if wxUSE_THREADS
+ // for many reasons, the code below breaks down if it's called from another
+ // thread -- this could be fixed, but as Unix versions don't support this
+ // neither I don't want to waste time on this now
+ wxASSERT_MSG( wxThread::IsMain(),
+ _T("wxExecute() can be called only from the main thread") );
+#endif // wxUSE_THREADS
+
+ wxString command;
+
+#if wxUSE_IPC
+ // DDE hack: this is really not pretty, but we need to allow this for
+ // transparent handling of DDE servers in wxMimeTypesManager. Usually it
+ // returns the command which should be run to view/open/... a file of the
+ // given type. Sometimes, however, this command just launches the server
+ // and an additional DDE request must be made to really open the file. To
+ // keep all this well hidden from the application, we allow a special form
+ // of command: WX_DDE#<command>#DDE_SERVER#DDE_TOPIC#DDE_COMMAND in which
+ // case we execute just <command> and process the rest below
+ wxString ddeServer, ddeTopic, ddeCommand;
+ static const size_t lenDdePrefix = 7; // strlen("WX_DDE:")
+ if ( cmd.Left(lenDdePrefix) == _T("WX_DDE#") )
+ {
+ // speed up the concatenations below
+ ddeServer.reserve(256);
+ ddeTopic.reserve(256);
+ ddeCommand.reserve(256);
+
+ const wxChar *p = cmd.c_str() + 7;
+ while ( *p && *p != _T('#') )
+ {
+ command += *p++;
+ }
+
+ if ( *p )
+ {
+ // skip '#'
+ p++;
+ }
+ else
+ {
+ wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute"));
+ }
+
+ while ( *p && *p != _T('#') )
+ {
+ ddeServer += *p++;
+ }
+
+ if ( *p )
+ {
+ // skip '#'
+ p++;
+ }
+ else
+ {
+ wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute"));
+ }
+
+ while ( *p && *p != _T('#') )
+ {
+ ddeTopic += *p++;
+ }
+
+ if ( *p )
+ {
+ // skip '#'
+ p++;
+ }
+ else
+ {
+ wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute"));
+ }
+
+ while ( *p )
+ {
+ ddeCommand += *p++;
+ }
+
+ // if we want to just launch the program and not wait for its
+ // termination, try to execute DDE command right now, it can succeed if
+ // the process is already running - but as it fails if it's not
+ // running, suppress any errors it might generate
+ if ( !(flags & wxEXEC_SYNC) )
+ {
+ wxLogNull noErrors;
+ if ( wxExecuteDDE(ddeServer, ddeTopic, ddeCommand) )
+ {
+ // a dummy PID - this is a hack, of course, but it's well worth
+ // it as we don't open a new server each time we're called
+ // which would be quite bad
+ return -1;
+ }
+ }
+ }
+ else
+#endif // wxUSE_IPC
+ {
+ // no DDE
+ command = cmd;
+ }
+
+ // the IO redirection is only supported with wxUSE_STREAMS
+ BOOL redirect = FALSE;
+
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
+ wxPipe pipeIn, pipeOut, pipeErr;
+
+ // we'll save here the copy of pipeIn[Write]
+ HANDLE hpipeStdinWrite = INVALID_HANDLE_VALUE;
+
+ // open the pipes to which child process IO will be redirected if needed
+ if ( handler && handler->IsRedirected() )
+ {
+ // create pipes for redirecting stdin, stdout and stderr
+ if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() )
+ {
+ wxLogSysError(_("Failed to redirect the child process IO"));
+
+ // indicate failure: we need to return different error code
+ // depending on the sync flag
+ return flags & wxEXEC_SYNC ? -1 : 0;
+ }
+
+ redirect = TRUE;
+ }
+#endif // wxUSE_STREAMS
+
+ // create the process
+ STARTUPINFO si;
+ wxZeroMemory(si);
+ si.cb = sizeof(si);
+
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
+ if ( redirect )
+ {
+ si.dwFlags = STARTF_USESTDHANDLES;
+
+ si.hStdInput = pipeIn[wxPipe::Read];
+ si.hStdOutput = pipeOut[wxPipe::Write];
+ si.hStdError = pipeErr[wxPipe::Write];
+
+ // when the std IO is redirected, we don't show the (console) process
+ // window by default, but this can be overridden by the caller by
+ // specifying wxEXEC_NOHIDE flag
+ if ( !(flags & wxEXEC_NOHIDE) )
+ {
+ si.dwFlags |= STARTF_USESHOWWINDOW;
+ si.wShowWindow = SW_HIDE;
+ }
+
+ // we must duplicate the handle to the write side of stdin pipe to make
+ // it non inheritable: indeed, we must close the writing end of pipeIn
+ // before launching the child process as otherwise this handle will be
+ // inherited by the child which will never close it and so the pipe
+ // will never be closed and the child will be left stuck in ReadFile()
+ HANDLE pipeInWrite = pipeIn.Detach(wxPipe::Write);
+ if ( !::DuplicateHandle
+ (
+ ::GetCurrentProcess(),
+ pipeInWrite,
+ ::GetCurrentProcess(),
+ &hpipeStdinWrite,
+ 0, // desired access: unused here
+ FALSE, // not inherited
+ DUPLICATE_SAME_ACCESS // same access as for src handle
+ ) )
+ {
+ wxLogLastError(_T("DuplicateHandle"));
+ }
+
+ ::CloseHandle(pipeInWrite);
+ }
+#endif // wxUSE_STREAMS
+
+ PROCESS_INFORMATION pi;
+ DWORD dwFlags = CREATE_SUSPENDED;
+#ifndef __WXWINCE__
+ dwFlags |= CREATE_DEFAULT_ERROR_MODE ;
+#endif
+
+ bool ok = ::CreateProcess
+ (
+ NULL, // application name (use only cmd line)
+ (wxChar *)
+ command.c_str(), // full command line
+ NULL, // security attributes: defaults for both
+ NULL, // the process and its main thread
+ redirect, // inherit handles if we use pipes
+ dwFlags, // process creation flags
+ NULL, // environment (use the same)
+ NULL, // current directory (use the same)
+ &si, // startup info (unused here)
+ &pi // process info
+ ) != 0;
+
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
+ // we can close the pipe ends used by child anyhow
+ if ( redirect )
+ {
+ ::CloseHandle(pipeIn.Detach(wxPipe::Read));
+ ::CloseHandle(pipeOut.Detach(wxPipe::Write));
+ ::CloseHandle(pipeErr.Detach(wxPipe::Write));
+ }
+#endif // wxUSE_STREAMS
+
+ if ( !ok )
+ {
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
+ // close the other handles too
+ if ( redirect )
+ {
+ ::CloseHandle(pipeOut.Detach(wxPipe::Read));
+ ::CloseHandle(pipeErr.Detach(wxPipe::Read));
+ }
+#endif // wxUSE_STREAMS
+
+ wxLogSysError(_("Execution of command '%s' failed"), command.c_str());
+
+ return flags & wxEXEC_SYNC ? -1 : 0;
+ }
+
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
+ // the input buffer bufOut is connected to stdout, this is why it is
+ // called bufOut and not bufIn
+ wxStreamTempInputBuffer bufOut,
+ bufErr;
+
+ if ( redirect )
+ {
+ // We can now initialize the wxStreams
+ wxPipeInputStream *
+ outStream = new wxPipeInputStream(pipeOut.Detach(wxPipe::Read));
+ wxPipeInputStream *
+ errStream = new wxPipeInputStream(pipeErr.Detach(wxPipe::Read));
+ wxPipeOutputStream *
+ inStream = new wxPipeOutputStream(hpipeStdinWrite);
+
+ handler->SetPipeStreams(outStream, inStream, errStream);
+
+ bufOut.Init(outStream);
+ bufErr.Init(errStream);
+ }
+#endif // wxUSE_STREAMS
+
+ // register the class for the hidden window used for the notifications
+ if ( !gs_classForHiddenWindow )
+ {
+ gs_classForHiddenWindow = _T("wxHiddenWindow");
+
+ WNDCLASS wndclass;
+ wxZeroMemory(wndclass);
+ wndclass.lpfnWndProc = (WNDPROC)wxExecuteWindowCbk;
+ wndclass.hInstance = wxGetInstance();
+ wndclass.lpszClassName = gs_classForHiddenWindow;
+
+ if ( !::RegisterClass(&wndclass) )
+ {
+ wxLogLastError(wxT("RegisterClass(hidden window)"));
+ }
+ }
+
+ // create a hidden window to receive notification about process
+ // termination
+#ifdef __WXWINCE__
+ HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL,
+ WS_OVERLAPPED,
+ 0, 0, 0, 0, NULL,
+ (HMENU)NULL, wxGetInstance(), 0);