1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProcess class
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_PROCESSH__
13 #define _WX_PROCESSH__
18 #include "wx/stream.h"
21 #include "wx/utils.h" // for wxSignal
23 // the wxProcess creation flags
27 wxPROCESS_DEFAULT
= 0,
29 // redirect the IO of the child process
30 wxPROCESS_REDIRECT
= 1
33 // ----------------------------------------------------------------------------
34 // A wxProcess object should be passed to wxExecute - than its OnTerminate()
35 // function will be called when the process terminates.
36 // ----------------------------------------------------------------------------
38 class WXDLLIMPEXP_BASE wxProcess
: public wxEvtHandler
41 // kill the process with the given PID
42 static wxKillError
Kill(int pid
, wxSignal sig
= wxSIGTERM
, int flags
= wxKILL_NOCHILDREN
);
44 // test if the given process exists
45 static bool Exists(int pid
);
47 // this function replaces the standard popen() one: it launches a process
48 // asynchronously and allows the caller to get the streams connected to its
51 // on error NULL is returned, in any case the process object will be
52 // deleted automatically when the process terminates and should *not* be
53 // deleted by the caller
54 static wxProcess
*Open(const wxString
& cmd
, int flags
= wxEXEC_ASYNC
);
58 wxProcess(wxEvtHandler
*parent
= NULL
, int nId
= wxID_ANY
)
59 { Init(parent
, nId
, wxPROCESS_DEFAULT
); }
61 wxProcess(int flags
) { Init(NULL
, wxID_ANY
, flags
); }
65 // get the process ID of the process executed by Open()
66 long GetPid() const { return m_pid
; }
68 // may be overridden to be notified about process termination
69 virtual void OnTerminate(int pid
, int status
);
71 // call this before passing the object to wxExecute() to redirect the
72 // launched process stdin/stdout, then use GetInputStream() and
73 // GetOutputStream() to get access to them
74 void Redirect() { m_redirect
= true; }
75 bool IsRedirected() const { return m_redirect
; }
77 // detach from the parent - should be called by the parent if it's deleted
78 // before the process it started terminates
83 wxInputStream
*GetInputStream() const { return m_inputStream
; }
84 wxInputStream
*GetErrorStream() const { return m_errorStream
; }
85 wxOutputStream
*GetOutputStream() const { return m_outputStream
; }
87 // close the output stream indicating that nothing more will be written
88 void CloseOutput() { delete m_outputStream
; m_outputStream
= NULL
; }
90 // return true if the child process stdout is not closed
91 bool IsInputOpened() const;
93 // return true if any input is available on the child process stdout/err
94 bool IsInputAvailable() const;
95 bool IsErrorAvailable() const;
97 // implementation only (for wxExecute)
99 // NB: the streams passed here should correspond to the child process
100 // stdout, stdin and stderr and here the normal naming convention is
101 // used unlike elsewhere in this class
102 void SetPipeStreams(wxInputStream
*outStream
,
103 wxOutputStream
*inStream
,
104 wxInputStream
*errStream
);
105 #endif // wxUSE_STREAMS
107 // implementation only - don't use!
108 // --------------------------------
110 // needs to be public since it needs to be used from wxExecute() global func
111 void SetPid(long pid
) { m_pid
= pid
; }
114 void Init(wxEvtHandler
*parent
, int id
, int flags
);
120 // these streams are connected to stdout, stderr and stdin of the child
121 // process respectively (yes, m_inputStream corresponds to stdout -- very
122 // confusing but too late to change now)
123 wxInputStream
*m_inputStream
,
125 wxOutputStream
*m_outputStream
;
126 #endif // wxUSE_STREAMS
130 DECLARE_DYNAMIC_CLASS(wxProcess
)
131 wxDECLARE_NO_COPY_CLASS(wxProcess
);
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 class WXDLLIMPEXP_FWD_BASE wxProcessEvent
;
140 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_BASE
, wxEVT_END_PROCESS
, wxProcessEvent
);
142 class WXDLLIMPEXP_BASE wxProcessEvent
: public wxEvent
145 wxProcessEvent(int nId
= 0, int pid
= 0, int exitcode
= 0) : wxEvent(nId
)
147 m_eventType
= wxEVT_END_PROCESS
;
149 m_exitcode
= exitcode
;
153 // PID of process which terminated
154 int GetPid() { return m_pid
; }
157 int GetExitCode() { return m_exitcode
; }
159 // implement the base class pure virtual
160 virtual wxEvent
*Clone() const { return new wxProcessEvent(*this); }
166 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent
)
169 typedef void (wxEvtHandler::*wxProcessEventFunction
)(wxProcessEvent
&);
171 #define wxProcessEventHandler(func) \
172 wxEVENT_HANDLER_CAST(wxProcessEventFunction, func)
174 #define EVT_END_PROCESS(id, func) \
175 wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func))
177 #endif // _WX_PROCESSH__