1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProcess class
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
7 // Copyright: (c) 1998 Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_PROCESSH__
12 #define _WX_PROCESSH__
17 #include "wx/stream.h"
20 #include "wx/utils.h" // for wxSignal
22 // the wxProcess creation flags
26 wxPROCESS_DEFAULT
= 0,
28 // redirect the IO of the child process
29 wxPROCESS_REDIRECT
= 1
32 // ----------------------------------------------------------------------------
33 // A wxProcess object should be passed to wxExecute - than its OnTerminate()
34 // function will be called when the process terminates.
35 // ----------------------------------------------------------------------------
37 class WXDLLIMPEXP_BASE wxProcess
: public wxEvtHandler
40 // kill the process with the given PID
41 static wxKillError
Kill(int pid
, wxSignal sig
= wxSIGTERM
, int flags
= wxKILL_NOCHILDREN
);
43 // test if the given process exists
44 static bool Exists(int pid
);
46 // this function replaces the standard popen() one: it launches a process
47 // asynchronously and allows the caller to get the streams connected to its
50 // on error NULL is returned, in any case the process object will be
51 // deleted automatically when the process terminates and should *not* be
52 // deleted by the caller
53 static wxProcess
*Open(const wxString
& cmd
, int flags
= wxEXEC_ASYNC
);
57 wxProcess(wxEvtHandler
*parent
= NULL
, int nId
= wxID_ANY
)
58 { Init(parent
, nId
, wxPROCESS_DEFAULT
); }
60 wxProcess(int flags
) { Init(NULL
, wxID_ANY
, flags
); }
64 // get the process ID of the process executed by Open()
65 long GetPid() const { return m_pid
; }
67 // may be overridden to be notified about process termination
68 virtual void OnTerminate(int pid
, int status
);
70 // call this before passing the object to wxExecute() to redirect the
71 // launched process stdin/stdout, then use GetInputStream() and
72 // GetOutputStream() to get access to them
73 void Redirect() { m_redirect
= true; }
74 bool IsRedirected() const { return m_redirect
; }
76 // detach from the parent - should be called by the parent if it's deleted
77 // before the process it started terminates
82 wxInputStream
*GetInputStream() const { return m_inputStream
; }
83 wxInputStream
*GetErrorStream() const { return m_errorStream
; }
84 wxOutputStream
*GetOutputStream() const { return m_outputStream
; }
86 // close the output stream indicating that nothing more will be written
87 void CloseOutput() { delete m_outputStream
; m_outputStream
= NULL
; }
89 // return true if the child process stdout is not closed
90 bool IsInputOpened() const;
92 // return true if any input is available on the child process stdout/err
93 bool IsInputAvailable() const;
94 bool IsErrorAvailable() const;
96 // implementation only (for wxExecute)
98 // NB: the streams passed here should correspond to the child process
99 // stdout, stdin and stderr and here the normal naming convention is
100 // used unlike elsewhere in this class
101 void SetPipeStreams(wxInputStream
*outStream
,
102 wxOutputStream
*inStream
,
103 wxInputStream
*errStream
);
104 #endif // wxUSE_STREAMS
107 // Sets the priority to the given value: see wxPRIORITY_XXX constants.
109 // NB: the priority can only be set before the process is created
110 void SetPriority(unsigned priority
);
112 // Get the current priority.
113 unsigned GetPriority() const { return m_priority
; }
115 // implementation only - don't use!
116 // --------------------------------
118 // needs to be public since it needs to be used from wxExecute() global func
119 void SetPid(long pid
) { m_pid
= pid
; }
122 void Init(wxEvtHandler
*parent
, int id
, int flags
);
130 // these streams are connected to stdout, stderr and stdin of the child
131 // process respectively (yes, m_inputStream corresponds to stdout -- very
132 // confusing but too late to change now)
133 wxInputStream
*m_inputStream
,
135 wxOutputStream
*m_outputStream
;
136 #endif // wxUSE_STREAMS
140 DECLARE_DYNAMIC_CLASS(wxProcess
)
141 wxDECLARE_NO_COPY_CLASS(wxProcess
);
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 class WXDLLIMPEXP_FWD_BASE wxProcessEvent
;
150 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_BASE
, wxEVT_END_PROCESS
, wxProcessEvent
);
152 class WXDLLIMPEXP_BASE wxProcessEvent
: public wxEvent
155 wxProcessEvent(int nId
= 0, int pid
= 0, int exitcode
= 0) : wxEvent(nId
)
157 m_eventType
= wxEVT_END_PROCESS
;
159 m_exitcode
= exitcode
;
163 // PID of process which terminated
164 int GetPid() { return m_pid
; }
167 int GetExitCode() { return m_exitcode
; }
169 // implement the base class pure virtual
170 virtual wxEvent
*Clone() const { return new wxProcessEvent(*this); }
176 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent
)
179 typedef void (wxEvtHandler::*wxProcessEventFunction
)(wxProcessEvent
&);
181 #define wxProcessEventHandler(func) \
182 wxEVENT_HANDLER_CAST(wxProcessEventFunction, func)
184 #define EVT_END_PROCESS(id, func) \
185 wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func))
187 #endif // _WX_PROCESSH__