/////////////////////////////////////////////////////////////////////////////
// ============================================================================
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// we need to create a hidden window to receive the process termination
// notifications and for this we need a (Win) class name for it which we will
// register the first time it's needed
// we need to create a hidden window to receive the process termination
// notifications and for this we need a (Win) class name for it which we will
// register the first time it's needed
static const wxChar *gs_classForHiddenWindow = NULL;
// ----------------------------------------------------------------------------
static const wxChar *gs_classForHiddenWindow = NULL;
// ----------------------------------------------------------------------------
-#if defined(__WIN32__) && wxUSE_STREAMS
+class wxExecuteModule : public wxModule
+{
+public:
+ virtual bool OnInit() { return true; }
+ virtual void OnExit()
+ {
+ if ( *gs_classForHiddenWindow )
+ {
+ if ( !::UnregisterClass(wxMSWEXEC_WNDCLASSNAME, wxGetInstance()) )
+ {
+ wxLogLastError(_T("UnregisterClass(wxExecClass)"));
+ }
+
+ gs_classForHiddenWindow = NULL;
+ }
+ }
+
+private:
+ DECLARE_DYNAMIC_CLASS(wxExecuteModule)
+};
+
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
// default ctor doesn't do anything
wxPipe() { m_handles[Read] = m_handles[Write] = INVALID_HANDLE_VALUE; }
// default ctor doesn't do anything
wxPipe() { m_handles[Read] = m_handles[Write] = INVALID_HANDLE_VALUE; }
bool IsOk() const { return m_handles[Read] != INVALID_HANDLE_VALUE; }
// return the descriptor for one of the pipe ends
bool IsOk() const { return m_handles[Read] != INVALID_HANDLE_VALUE; }
// return the descriptor for one of the pipe ends
- HANDLE operator[](Direction which) const
- {
- wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_handles),
- _T("invalid pipe index") );
-
- return m_handles[which];
- }
+ HANDLE operator[](Direction which) const { return m_handles[which]; }
// detach a descriptor, meaning that the pipe dtor won't close it, and
// return it
HANDLE Detach(Direction which)
{
// detach a descriptor, meaning that the pipe dtor won't close it, and
// return it
HANDLE Detach(Direction which)
{
// ----------------------------------------------------------------------------
// process termination detecting support
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// process termination detecting support
// ----------------------------------------------------------------------------
// thread function for the thread monitoring the process termination
static DWORD __stdcall wxExecuteThread(void *arg)
{
// thread function for the thread monitoring the process termination
static DWORD __stdcall wxExecuteThread(void *arg)
{
- return DefWindowProc(hWnd, message, wParam, lParam);
+ return ::DefWindowProc(hWnd, message, wParam, lParam);
// implementation of IO redirection support classes
// ============================================================================
// implementation of IO redirection support classes
// ============================================================================
- wxConstCast(this, wxPipeInputStream)->m_hInput = INVALID_HANDLE_VALUE;
+ wxPipeInputStream *self = wxConstCast(this, wxPipeInputStream);
+
+ self->m_hInput = INVALID_HANDLE_VALUE;
+ self->m_lasterror = wxSTREAM_EOF;
DWORD bytesRead;
if ( !::ReadFile(m_hInput, buffer, len, &bytesRead, NULL) )
{
DWORD bytesRead;
if ( !::ReadFile(m_hInput, buffer, len, &bytesRead, NULL) )
{
+
+ // unblock the pipe to prevent deadlocks when we're writing to the pipe
+ // from which the child process can't read because it is writing in its own
+ // end of it
+ DWORD mode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
+ if ( !::SetNamedPipeHandleState
+ (
+ m_hOutput,
+ &mode,
+ NULL, // collection count (we don't set it)
+ NULL // timeout (we don't set it neither)
+ ) )
+ {
+ wxLogLastError(_T("SetNamedPipeHandleState(PIPE_NOWAIT)"));
+ }
- m_lasterror = wxSTREAM_NOERROR;
- if ( !::WriteFile(m_hOutput, buffer, len, &bytesRead, NULL) )
+ DWORD totalWritten = 0;
+ while ( len > 0 )
- if ( ::GetLastError() == ERROR_BROKEN_PIPE )
- m_lasterror = wxSTREAM_EOF;
- else
- m_lasterror = wxSTREAM_READ_ERROR;
+ 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;
// ============================================================================
// wxExecute functions family
// ============================================================================
// ============================================================================
// wxExecute functions family
// ============================================================================
-static bool wxExecuteDDE(const wxString& ddeServer,
- const wxString& ddeTopic,
- const wxString& ddeCommand)
+bool
+wxExecuteDDE(const wxString& ddeServer,
+ const wxString& ddeTopic,
+ const wxString& ddeCommand)
- // 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!
+ // 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!
- // so we try it first and then the other one if it
- // failed
+ // moreover, anotheri mportant program (IE) understands both but
+ // returns an error from Execute() so we must try Request() first
+ // to avoid doing it twice
- wxCHECK_MSG( !!cmd, 0, wxT("empty command in wxExecute") );
+ 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
wxPipe pipeIn, pipeOut, pipeErr;
// we'll save here the copy of pipeIn[Write]
wxPipe pipeIn, pipeOut, pipeErr;
// we'll save here the copy of pipeIn[Write]
- DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED;
+ DWORD dwFlags = CREATE_SUSPENDED;
+
+#ifndef __WXWINCE__
+ dwFlags |= CREATE_DEFAULT_ERROR_MODE ;
+#else
+ // we are assuming commands without spaces for now
+ wxString moduleName = command.BeforeFirst(wxT(' '));
+ wxString arguments = command.AfterFirst(wxT(' '));
+#endif
- NULL, // application name (use only cmd line)
+ // WinCE requires appname to be non null
+ // Win32 allows for null
+#ifdef __WXWINCE__
+ (wxChar *)
+ moduleName.c_str(), // application name
+ (wxChar *)
+ arguments.c_str(), // arguments
+#else
+ NULL, // application name (use only cmd line)
- 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
+ command.c_str(), // full command line
+#endif
+ 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
// the input buffer bufOut is connected to stdout, this is why it is
// called bufOut and not bufIn
wxStreamTempInputBuffer bufOut,
// the input buffer bufOut is connected to stdout, this is why it is
// called bufOut and not bufIn
wxStreamTempInputBuffer bufOut,
- // 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)"));
- }
- }
-
- HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL,
- WS_OVERLAPPEDWINDOW,
- 0, 0, 0, 0, NULL,
- (HMENU)NULL, wxGetInstance(), 0);
+ HWND hwnd = wxCreateHiddenWindow
+ (
+ &gs_classForHiddenWindow,
+ wxMSWEXEC_WNDCLASSNAME,
+ (WNDPROC)wxExecuteWindowCbk
+ );
+
break;
case 0:
// ok, process ready to accept DDE requests
ok = wxExecuteDDE(ddeServer, ddeTopic, ddeCommand);
}
break;
case 0:
// ok, process ready to accept DDE requests
ok = wxExecuteDDE(ddeServer, ddeTopic, ddeCommand);
}
- // waiting until command executed (disable everything while doing it)
-#if wxUSE_GUI
- {
- wxBusyCursor bc;
+ wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
+ wxCHECK_MSG( traits, -1, _T("no wxAppTraits in wxExecute()?") );
- // don't eat 100% of the CPU -- ugly but anything else requires
- // real async IO which we don't have for the moment
- ::Sleep(50);
+ // don't eat 100% of the CPU -- ugly but anything else requires
+ // real async IO which we don't have for the moment
+ ::Sleep(50);
-#else // Win16
- long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
- if (instanceID < 32)
- return flags & wxEXEC_SYNC ? -1 : 0;
-
- if ( flags & wxEXEC_SYNC )
- {
- int running;
- do
- {
- wxYield();
- running = GetModuleUsage((HINSTANCE)instanceID);
- } while (running);
- }
-
- return instanceID;
-#endif // Win16/32