- if (((long)result) <= 32) {
- free(cl);
-
- wxLogSysError(_("Can't execute command '%s'"), command.c_str());
- return 0;
- }
-
- // Alloc data
- data = new wxExecuteData;
-
- // Create window
- window = CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
- (HMENU) NULL, wxGetInstance(), 0);
-
- FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC) wxExecuteWindowCbk,
- wxGetInstance());
-
- SetWindowLong(window, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
- SetWindowLong(window, GWL_USERDATA, (LONG) data);
-
- data->process = result;
- data->window = window;
- data->state = sync;
- data->handler = (sync) ? NULL : handler;
-
- dresult = (DWORD)CreateThread(NULL, 0,
- (LPTHREAD_START_ROUTINE)wxExecuteThread,
- (void *)data, 0, &tid);
- if (dresult == 0) {
- wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
- DestroyWindow(window);
- return (long)result;
- }
-
- if (!sync)
- {
- free(cl);
- return (long)result;
- }
-
- // waiting until command executed
- while (data->state)
- wxYield();
-
- free(cl);
- return 0;
-#else
- long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
- if (instanceID < 32) return(0);
-
- if (sync) {
- int running;
- do {
- wxYield();
- running = GetModuleUsage((HANDLE)instanceID);
- } while (running);
- }
- return(instanceID);
+size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len)
+{
+ m_lasterror = wxSTREAM_NO_ERROR;
+
+ DWORD totalWritten = 0;
+ while ( len > 0 )
+ {
+ DWORD chunkWritten;
+ if ( !::WriteFile(m_hOutput, buffer, len, &chunkWritten, NULL) )
+ {
+ m_lasterror = ::GetLastError() == ERROR_BROKEN_PIPE
+ ? wxSTREAM_EOF
+ : wxSTREAM_WRITE_ERROR;
+ break;
+ }
+
+ if ( !chunkWritten )
+ break;
+
+ buffer = (char *)buffer + chunkWritten;
+ totalWritten += chunkWritten;
+ len -= chunkWritten;
+ }
+
+ return totalWritten;
+}
+
+#endif // wxUSE_STREAMS
+
+// ============================================================================
+// wxExecute functions family
+// ============================================================================
+
+#if wxUSE_IPC
+
+// connect to the given server via DDE and ask it to execute the command
+static 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.IsEmpty(), 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 ;