]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/process.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxProcess
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 Signal constants used by wxProcess.
14 wxSIGNONE
= 0, //!< verify if the process exists under Unix
23 wxSIGKILL
, //!< forcefully kill, dangerous!
29 wxSIGTERM
//!< terminate the process gently
33 Return values for wxProcess::Kill.
37 wxKILL_OK
, //!< no error
38 wxKILL_BAD_SIGNAL
, //!< no such signal
39 wxKILL_ACCESS_DENIED
, //!< permission denied
40 wxKILL_NO_PROCESS
, //!< no such process
41 wxKILL_ERROR
//!< another, unspecified error
48 The objects of this class are used in conjunction with the ::wxExecute() function.
49 When a wxProcess object is passed to ::wxExecute(), its OnTerminate() virtual method
50 is called when the process terminates. This allows the program to be (asynchronously)
51 notified about the process termination and also retrieve its exit status which is
52 unavailable from ::wxExecute() in the case of asynchronous execution.
55 If the @c wxEVT_END_PROCESS event sent after termination is processed by the
56 parent, then it is responsible for deleting the wxProcess object which sent it.
57 However, if it is not processed, the object will <b>delete itself</b> and so the
58 library users should only delete those objects whose notifications have been
59 processed (and call wxProcess::Detach for others).
60 This also means that unless you're going to process the @c wxEVT_END_PROCESS event,
61 you <b>must</b> allocate the wxProcess class on the heap.
63 wxProcess also supports IO redirection of the child process. For this, you have
64 to call its Redirect() method before passing it to ::wxExecute().
65 If the child process was launched successfully, GetInputStream(), GetOutputStream()
66 and GetErrorStream() can then be used to retrieve the streams corresponding to the
67 child process standard output, input and error output respectively.
69 @beginEventEmissionTable{wxProcessEvent}
70 @event{EVT_END_PROCESS(id, func)}
71 Process a @c wxEVT_END_PROCESS event, sent by wxProcess::OnTerminate upon
72 the external process termination.
76 @category{appmanagement}
79 In wxPerl this class has an additional @c Destroy method,
80 for explicit destruction.
83 @see wxExecute(), @ref page_samples_exec
85 class wxProcess
: public wxEvtHandler
89 Constructs a process object. @a id is only used in the case you want to
90 use wxWidgets events. It identifies this object, or another window that will
93 If the @a parent parameter is different from @NULL, it will receive
94 a @c wxEVT_END_PROCESS notification event (you should insert @c EVT_END_PROCESS
95 macro in the event table of the parent to handle it) with the given @a id.
98 The event handler parent.
102 wxProcess(wxEvtHandler
* parent
= NULL
, int id
= -1);
105 Creates an object without any associated parent (and hence no id neither)
106 but allows to specify the @a flags which can have the value of
107 @c wxPROCESS_DEFAULT or @c wxPROCESS_REDIRECT.
109 Specifying the former value has no particular effect while using the latter
110 one is equivalent to calling Redirect().
112 wxProcess(int flags
);
115 Destroys the wxProcess object.
117 virtual ~wxProcess();
120 Closes the output stream (the one connected to the stdin of the child
123 This function can be used to indicate to the child process that
124 there is no more data to be read - usually, a filter program will only
125 terminate when the input stream is closed.
130 Detaches this event handler from the parent specified in the constructor
131 (see wxEvtHandler::Unlink() for a similar but not identic function).
133 Normally, a wxProcess object is deleted by its parent when it receives the
134 notification about the process termination.
136 However, it might happen that the parent object is destroyed before the external
137 process is terminated (e.g. a window from which this external process was launched
138 is closed by the user) and in this case it @b should not delete the wxProcess
139 object, but @b should call Detach() instead.
141 After the wxProcess object is detached from its parent, no notification events
142 will be sent to the parent and the object will delete itself upon reception of
143 the process termination notification.
148 Returns @true if the given process exists in the system.
150 @see Kill(), @ref page_samples_exec "Exec sample"
152 static bool Exists(int pid
);
155 Returns an input stream which corresponds to the standard error output (stderr)
156 of the child process.
158 wxInputStream
* GetErrorStream() const;
161 It returns an input stream corresponding to the standard output stream of the
162 subprocess. If it is @NULL, you have not turned on the redirection.
166 wxInputStream
* GetInputStream() const;
169 It returns an output stream correspoding to the input stream of the subprocess.
170 If it is @NULL, you have not turned on the redirection.
174 wxOutputStream
* GetOutputStream() const;
177 Returns the process ID of the process launched by Open() or set by
178 wxExecute() (if you passed this wxProcess as argument).
183 Returns @true if there is data to be read on the child process standard
186 @see IsInputAvailable()
188 bool IsErrorAvailable() const;
191 Returns @true if there is data to be read on the child process standard
194 This allows to write simple (and extremely inefficient) polling-based code
195 waiting for a better mechanism in future wxWidgets versions.
196 See the @ref page_samples_exec "exec sample" for an example of using this
201 bool IsInputAvailable() const;
204 Returns @true if the child process standard output stream is opened.
206 bool IsInputOpened() const;
209 Send the specified signal to the given process. Possible signal values
210 can be one of the ::wxSignal enumeration values.
212 @c wxSIGNONE, @c wxSIGKILL and @c wxSIGTERM have the same meaning
213 under both Unix and Windows but all the other signals are equivalent to
214 @c wxSIGTERM under Windows.
216 The @a flags parameter can be @c wxKILL_NOCHILDREN (the default),
217 or @c wxKILL_CHILDREN, in which case the child processes of this
218 process will be killed too. Note that under Unix, for @c wxKILL_CHILDREN
219 to work you should have created the process passing @c wxEXEC_MAKE_GROUP_LEADER.
221 Returns the element of ::wxKillError enum.
223 @see Exists(), wxKill(), @ref page_samples_exec "Exec sample"
225 static wxKillError
Kill(int pid
, wxSignal sig
= wxSIGTERM
,
226 int flags
= wxKILL_NOCHILDREN
);
229 It is called when the process with the pid @a pid finishes.
230 It raises a wxWidgets event when it isn't overridden.
232 Note that this function won't be called if you Kill() the process.
235 The pid of the process which has just terminated.
237 The exit code of the process.
239 virtual void OnTerminate(int pid
, int status
);
242 This static method replaces the standard @c popen() function: it launches
243 the process specified by the @a cmd parameter and returns the wxProcess
244 object which can be used to retrieve the streams connected to the standard
245 input, output and error output of the child process.
247 If the process couldn't be launched, @NULL is returned.
250 In any case the returned pointer should @b not be deleted, rather the process
251 object will be destroyed automatically when the child process terminates. This
252 does mean that the child process should be told to quit before the main program
253 exits to avoid memory leaks.
256 The command to execute, including optional arguments.
258 The flags to pass to ::wxExecute().
259 Note: @c wxEXEC_SYNC should not be used.
261 @return A pointer to new wxProcess object or @NULL on error.
265 static wxProcess
* Open(const wxString
& cmd
,
266 int flags
= wxEXEC_ASYNC
);
269 Turns on redirection.
271 ::wxExecute() will try to open a couple of pipes to catch the subprocess stdio.
272 The caught input stream is returned by GetOutputStream() as a non-seekable stream.
273 The caught output stream is returned by GetInputStream() as a non-seekable stream.
281 @class wxProcessEvent
283 A process event is sent to the wxEvtHandler specified to wxProcess
284 when a process is terminated.
286 @beginEventTable{wxProcessEvent}
287 @event{EVT_END_PROCESS(id, func)}
288 Process a @c wxEVT_END_PROCESS event. @a id is the identifier of the process
289 object (the id passed to the wxProcess constructor) or a window to receive
296 @see wxProcess, @ref overview_events
298 class wxProcessEvent
: public wxEvent
304 Takes a wxProcessObject or window id, a process id and an exit status.
306 wxProcessEvent(int id
= 0, int pid
= 0, int exitcode
= 0);
309 Returns the exist status.
314 Returns the process id.