+#if wxUSE_IPC
+
+// connect to the given server via DDE and ask it to execute the command
+bool
+wxExecuteDDE(const wxString& ddeServer,
+ const wxString& ddeTopic,
+ const wxString& ddeCommand)
+{
+ bool ok wxDUMMY_INITIALIZE(false);
+
+ wxDDEClient client;
+ wxConnectionBase *
+ conn = client.MakeConnection(wxEmptyString, ddeServer, ddeTopic);
+ if ( !conn )
+ {
+ ok = false;
+ }
+ else // connected to DDE server
+ {
+ // the added complication here is that although most programs use
+ // XTYP_EXECUTE for their DDE API, some important ones -- like Word
+ // and other MS stuff - use XTYP_REQUEST!
+ //
+ // moreover, anotheri mportant program (IE) understands both but
+ // returns an error from Execute() so we must try Request() first
+ // to avoid doing it twice
+ {
+ // we're prepared for this one to fail, so don't show errors
+ wxLogNull noErrors;
+
+ ok = conn->Request(ddeCommand) != NULL;
+ }
+
+ if ( !ok )
+ {
+ // now try execute -- but show the errors
+ ok = conn->Execute(ddeCommand);
+ }
+ }
+
+ return ok;
+}
+
+#endif // wxUSE_IPC
+
+long wxExecute(const wxString& cmd, int flags, wxProcess *handler)
+{
+ wxCHECK_MSG( !cmd.empty(), 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