X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..9f4de6b2cf06f53a98b28f53640f3b9ec8cf4182:/src/msw/utilsexc.cpp diff --git a/src/msw/utilsexc.cpp b/src/msw/utilsexc.cpp index e6287d9f28..21b74439c3 100644 --- a/src/msw/utilsexc.cpp +++ b/src/msw/utilsexc.cpp @@ -45,7 +45,7 @@ #include -#if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) +#if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) #include #ifndef __MWERKS__ #include @@ -57,7 +57,7 @@ #include #endif -#if defined(__WIN32__) && !defined(__WXWINE__) +#if defined(__WIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__) #include #ifndef __GNUWIN32__ @@ -259,8 +259,10 @@ size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len) #ifdef __WIN32__ -static DWORD wxExecuteThread(wxExecuteData *data) +static DWORD __stdcall wxExecuteThread(void *arg) { + wxExecuteData *data = (wxExecuteData*)arg; + WaitForSingleObject(data->hProcess, INFINITE); // get the exit code @@ -315,11 +317,55 @@ LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message, } #endif // Win32 -long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) +#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; + + wxDDEClient client; + wxConnectionBase *conn = client.MakeConnection(_T(""), + 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 IE and other MS stuff - use + // XTYP_REQUEST! + // + // so we try it first and then the other one if it + // failed + { + 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, 0, wxT("empty command in wxExecute") ); 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 @@ -327,12 +373,17 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) // 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::DDE_SERVER:DDE_TOPIC:DDE_COMMAND in which + // of command: WX_DDE##DDE_SERVER#DDE_TOPIC#DDE_COMMAND in which // case we execute just 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('#') ) { @@ -383,6 +434,22 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) { 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 @@ -398,9 +465,9 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) #if wxUSE_STREAMS // the first elements are reading ends, the second are the writing ones HANDLE hpipeStdin[2], - hpipeStdinWrite = INVALID_HANDLE_VALUE, hpipeStdout[2], hpipeStderr[2]; + HANDLE hpipeStdinWrite = INVALID_HANDLE_VALUE; // open the pipes to which child process IO will be redirected if needed if ( handler && handler->IsRedirected() ) @@ -417,8 +484,9 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) { wxLogSysError(_("Can't create the inter-process read pipe")); - // indicate failure in both cases - return sync ? -1 : 0; + // indicate failure: we need to return different error code + // depending on the sync flag + return flags & wxEXEC_SYNC ? -1 : 0; } // and a stdout one @@ -429,7 +497,7 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) wxLogSysError(_("Can't create the inter-process write pipe")); - return sync ? -1 : 0; + return flags & wxEXEC_SYNC ? -1 : 0; } (void)::CreatePipe(&hpipeStderr[0], &hpipeStderr[1], &security, 0); @@ -446,15 +514,20 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) #if wxUSE_STREAMS if ( redirect ) { - // when the std IO is redirected, we don't show the (console) process - // window - si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = hpipeStdin[0]; si.hStdOutput = hpipeStdout[1]; si.hStdError = hpipeStderr[1]; - si.wShowWindow = SW_HIDE; + // 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 hpipeStdin[1] before @@ -520,7 +593,7 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) wxLogSysError(_("Execution of command '%s' failed"), command.c_str()); - return sync ? -1 : 0; + return flags & wxEXEC_SYNC ? -1 : 0; } #if wxUSE_STREAMS @@ -565,8 +638,8 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) data->hProcess = pi.hProcess; data->dwProcessId = pi.dwProcessId; data->hWnd = hwnd; - data->state = sync; - if ( sync ) + data->state = (flags & wxEXEC_SYNC) != 0; + if ( flags & wxEXEC_SYNC ) { // handler may be !NULL for capturing program output, but we don't use // it wxExecuteData struct in this case @@ -581,7 +654,7 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) DWORD tid; HANDLE hThread = ::CreateThread(NULL, 0, - (LPTHREAD_START_ROUTINE)wxExecuteThread, + wxExecuteThread, (void *)data, 0, &tid); @@ -614,20 +687,38 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) #if wxUSE_IPC // second part of DDE hack: now establish the DDE conversation with the // just launched process - if ( !!ddeServer ) + if ( !ddeServer.empty() ) { - wxDDEClient client; - wxConnectionBase *conn = client.MakeConnection(_T(""), - ddeServer, - ddeTopic); - if ( !conn || !conn->Execute(ddeCommand) ) + bool ok; + + // give the process the time to init itself + // + // we use a very big timeout hoping that WaitForInputIdle() will return + // much sooner, but not INFINITE just in case the process hangs + // completely - like this we will regain control sooner or later + switch ( ::WaitForInputIdle(pi.hProcess, 10000 /* 10 seconds */) ) { - wxLogError(_("Couldn't launch DDE server '%s'."), command.c_str()); + default: + wxFAIL_MSG( _T("unexpected WaitForInputIdle() return code") ); + // fall through + + case -1: + wxLogLastError(_T("WaitForInputIdle() in wxExecute")); + + case WAIT_TIMEOUT: + wxLogDebug(_T("Timeout too small in WaitForInputIdle")); + + ok = FALSE; + break; + + case 0: + // ok, process ready to accept DDE requests + ok = wxExecuteDDE(ddeServer, ddeTopic, ddeCommand); } } #endif // wxUSE_IPC - if ( !sync ) + if ( !(flags & wxEXEC_SYNC) ) { // clean up will be done when the process terminates @@ -644,7 +735,11 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) #endif // wxUSE_GUI while ( data->state ) + { + // don't take 100% of the CPU + ::Sleep(500); wxYield(); + } #if wxUSE_GUI } @@ -657,21 +752,24 @@ long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) return dwExitCode; #else // Win16 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW); - if (instanceID < 32) return(0); + if (instanceID < 32) + return flags & wxEXEC_SYNC ? -1 : 0; - if (sync) { + if ( flags & wxEXEC_SYNC ) + { int running; - do { + do + { wxYield(); running = GetModuleUsage((HINSTANCE)instanceID); } while (running); } - return(instanceID); + return instanceID; #endif // Win16/32 } -long wxExecute(char **argv, bool sync, wxProcess *handler) +long wxExecute(char **argv, int flags, wxProcess *handler) { wxString command; @@ -682,43 +780,6 @@ long wxExecute(char **argv, bool sync, wxProcess *handler) command.RemoveLast(); - return wxExecute(command, sync, handler); -} - -#if wxUSE_GUI - -// ---------------------------------------------------------------------------- -// Metafile helpers -// ---------------------------------------------------------------------------- - -extern void PixelToHIMETRIC(LONG *x, LONG *y) -{ - ScreenHDC hdcRef; - - int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), - iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), - iWidthPels = GetDeviceCaps(hdcRef, HORZRES), - iHeightPels = GetDeviceCaps(hdcRef, VERTRES); - - *x *= (iWidthMM * 100); - *x /= iWidthPels; - *y *= (iHeightMM * 100); - *y /= iHeightPels; + return wxExecute(command, flags, handler); } -extern void HIMETRICToPixel(LONG *x, LONG *y) -{ - ScreenHDC hdcRef; - - int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), - iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), - iWidthPels = GetDeviceCaps(hdcRef, HORZRES), - iHeightPels = GetDeviceCaps(hdcRef, VERTRES); - - *x *= iWidthPels; - *x /= (iWidthMM * 100); - *y *= iHeightPels; - *y /= (iHeightMM * 100); -} - -#endif // wxUSE_GUI