]>
Commit | Line | Data |
---|---|---|
eafc087e GL |
1 | \section{\class{wxProcess}}\label{wxprocess} |
2 | ||
e626d7c7 | 3 | The objects of this class are used in conjunction with the |
7e84f02d VZ |
4 | \helpref{wxExecute}{wxexecute} function. When a wxProcess object is passed to |
5 | wxExecute(), its \helpref{OnTerminate()}{wxprocessonterminate} virtual method | |
6 | is called when the process terminates. This allows the program to be | |
7 | (asynchronously) notified about the process termination and also retrieve its | |
8 | exit status which is unavailable from wxExecute() in the case of | |
9 | asynchronous execution. | |
10 | ||
11 | Please note that if the process termination notification is processed by the | |
12 | parent, it is responsible for deleting the wxProcess object which sent it. | |
13 | However, if it is not processed, the object will delete itself and so the | |
14 | library users should only delete those objects whose notifications have been | |
15 | processed (and call \helpref{Detach()}{wxprocessdetach} for others). | |
eafc087e | 16 | |
f6bcfd97 | 17 | wxProcess also supports IO redirection of the child process. For this, you have |
e626d7c7 RD |
18 | to call its \helpref{Redirect}{wxprocessredirect} method before passing it to |
19 | \helpref{wxExecute}{wxexecute}. If the child process was launched successfully, | |
20 | \helpref{GetInputStream}{wxprocessgetinputstream}, | |
21 | \helpref{GetOutputStream}{wxprocessgetoutputstream} and | |
f6bcfd97 | 22 | \helpref{GetErrorStream}{wxprocessgeterrorstream} can then be used to retrieve |
2edb0bde | 23 | the streams corresponding to the child process standard output, input and |
f6bcfd97 BP |
24 | error output respectively. |
25 | ||
9722642d MB |
26 | \perlnote{In wxPerl this class has an additional {\tt Destroy} method, |
27 | for explicit destruction.} | |
28 | ||
eafc087e GL |
29 | \wxheading{Derived from} |
30 | ||
31 | \helpref{wxEvtHandler}{wxevthandler} | |
32 | ||
954b8ae6 JS |
33 | \wxheading{Include files} |
34 | ||
35 | <wx/process.h> | |
36 | ||
a7af285d VZ |
37 | \wxheading{Library} |
38 | ||
39 | \helpref{wxBase}{librarieslist} | |
40 | ||
f6bcfd97 BP |
41 | \wxheading{See also} |
42 | ||
43 | \helpref{wxExecute}{wxexecute}\\ | |
44 | \helpref{exec sample}{sampleexec} | |
45 | ||
eafc087e GL |
46 | \latexignore{\rtfignore{\wxheading{Members}}} |
47 | ||
3e79fa75 | 48 | \membersection{wxProcess::wxProcess}\label{wxprocessctor} |
eafc087e | 49 | |
e8482f24 | 50 | \func{}{wxProcess}{\param{wxEvtHandler *}{ parent = NULL}, \param{int}{ id = -1}} |
eafc087e | 51 | |
da00a8bb VZ |
52 | \func{}{wxProcess}{\param{int }{flags}} |
53 | ||
eafc087e | 54 | Constructs a process object. {\it id} is only used in the case you want to |
fc2171bd | 55 | use wxWidgets events. It identifies this object, or another window that will |
3972fb49 | 56 | receive the event. |
eafc087e | 57 | |
7e84f02d VZ |
58 | If the {\it parent} parameter is different from NULL, it will receive |
59 | a wxEVT\_END\_PROCESS notification event (you should insert EVT\_END\_PROCESS | |
60 | macro in the event table of the parent to handle it) with the given {\it id}. | |
61 | ||
da00a8bb VZ |
62 | The second constructor creates an object without any associated parent (and |
63 | hence no id neither) but allows to specify the {\it flags} which can have the | |
64 | value of {\tt wxPROCESS\_DEFAULT} or {\tt wxPROCESS\_REDIRECT}. Specifying the | |
65 | former value has no particular effect while using the latter one is equivalent | |
66 | to calling \helpref{Redirect}{wxprocessredirect}. | |
67 | ||
eafc087e GL |
68 | \wxheading{Parameters} |
69 | ||
70 | \docparam{parent}{The event handler parent.} | |
71 | ||
72 | \docparam{id}{id of an event.} | |
73 | ||
da00a8bb VZ |
74 | \docparam{flags}{either {\tt wxPROCESS\_DEFAULT} or {\tt wxPROCESS\_REDIRECT}} |
75 | ||
3e79fa75 | 76 | \membersection{wxProcess::\destruct{wxProcess}}\label{wxprocessdtor} |
eafc087e GL |
77 | |
78 | \func{}{\destruct{wxProcess}}{\void} | |
79 | ||
80 | Destroys the wxProcess object. | |
81 | ||
f6bcfd97 BP |
82 | \membersection{wxProcess::CloseOutput}\label{wxprocesscloseoutput} |
83 | ||
84 | \func{void}{CloseOutput}{\void} | |
85 | ||
86 | Closes the output stream (the one connected to the stdin of the child | |
87 | process). This function can be used to indicate to the child process that | |
88 | there is no more data to be read - usually, a filter program will only | |
89 | terminate when the input stream is closed. | |
90 | ||
7e84f02d VZ |
91 | \membersection{wxProcess::Detach}\label{wxprocessdetach} |
92 | ||
93 | \func{void}{Detach}{\void} | |
94 | ||
95 | Normally, a wxProcess object is deleted by its parent when it receives the | |
96 | notification about the process termination. However, it might happen that the | |
97 | parent object is destroyed before the external process is terminated (e.g. a | |
98 | window from which this external process was launched is closed by the user) | |
e626d7c7 | 99 | and in this case it {\bf should not delete} the wxProcess object, but |
7e84f02d VZ |
100 | {\bf should call Detach()} instead. After the wxProcess object is detached |
101 | from its parent, no notification events will be sent to the parent and the | |
102 | object will delete itself upon reception of the process termination | |
103 | notification. | |
104 | ||
f6bcfd97 BP |
105 | \membersection{wxProcess::GetErrorStream}\label{wxprocessgeterrorstream} |
106 | ||
107 | \constfunc{wxInputStream* }{GetErrorStream}{\void} | |
108 | ||
109 | Returns an input stream which corresponds to the standard error output (stderr) | |
110 | of the child process. | |
111 | ||
70dc22dc GL |
112 | \membersection{wxProcess::GetInputStream}\label{wxprocessgetinputstream} |
113 | ||
114 | \constfunc{wxInputStream* }{GetInputStream}{\void} | |
115 | ||
fe25efa3 | 116 | It returns an input stream corresponding to the standard output stream of the |
f6bcfd97 | 117 | subprocess. If it is NULL, you have not turned on the redirection. |
e8482f24 | 118 | See \helpref{wxProcess::Redirect}{wxprocessredirect}. |
70dc22dc | 119 | |
66f55ec6 | 120 | \membersection{wxProcess::GetOutputStream}\label{wxprocessgetoutputstream} |
70dc22dc | 121 | |
66f55ec6 | 122 | \constfunc{wxOutputStream* }{GetOutputStream}{\void} |
70dc22dc | 123 | |
f6bcfd97 | 124 | It returns an output stream correspoding to the input stream of the subprocess. |
e8482f24 GL |
125 | If it is NULL, you have not turned on the redirection. |
126 | See \helpref{wxProcess::Redirect}{wxprocessredirect}. | |
70dc22dc | 127 | |
411165f3 VZ |
128 | \membersection{wxProcess::IsErrorAvailable}\label{wxprocessiserroravailable} |
129 | ||
130 | \constfunc{bool}{IsErrorAvailable}{\void} | |
131 | ||
cc81d32f | 132 | Returns {\tt true} if there is data to be read on the child process standard |
411165f3 VZ |
133 | error stream. |
134 | ||
135 | \wxheading{See also} | |
136 | ||
137 | \helpref{IsInputAvailable}{wxprocessisinputavailable} | |
138 | ||
139 | \membersection{wxProcess::IsInputAvailable}\label{wxprocessisinputavailable} | |
140 | ||
141 | \constfunc{bool}{IsInputAvailable}{\void} | |
142 | ||
cc81d32f | 143 | Returns {\tt true} if there is data to be read on the child process standard |
411165f3 | 144 | output stream. This allows to write simple (and extremely inefficient) |
fc2171bd | 145 | polling-based code waiting for a better mechanism in future wxWidgets versions. |
411165f3 VZ |
146 | |
147 | See the \helpref{exec sample}{sampleexec} for an example of using this | |
148 | function. | |
149 | ||
150 | \wxheading{See also} | |
151 | ||
152 | \helpref{IsInputOpened}{wxprocessisinputopened} | |
153 | ||
154 | \membersection{wxProcess::IsInputOpened}\label{wxprocessisinputopened} | |
155 | ||
156 | \constfunc{bool}{IsInputOpened}{\void} | |
157 | ||
cc81d32f | 158 | Returns {\tt true} if the child process standard output stream is opened. |
411165f3 | 159 | |
50567b69 VZ |
160 | \membersection{wxProcess::Kill}\label{wxprocesskill} |
161 | ||
e0f6b731 | 162 | \func{static wxKillError}{Kill}{\param{int}{ pid}, \param{wxSignal}{ signal = wxSIGNONE}, \param{int }{flags = wxKILL\_NOCHILDREN}} |
50567b69 VZ |
163 | |
164 | Send the specified signal to the given process. Possible signal values are: | |
165 | ||
166 | \begin{verbatim} | |
167 | enum wxSignal | |
168 | { | |
169 | wxSIGNONE = 0, // verify if the process exists under Unix | |
170 | wxSIGHUP, | |
171 | wxSIGINT, | |
172 | wxSIGQUIT, | |
173 | wxSIGILL, | |
174 | wxSIGTRAP, | |
175 | wxSIGABRT, | |
176 | wxSIGEMT, | |
177 | wxSIGFPE, | |
178 | wxSIGKILL, // forcefully kill, dangerous! | |
179 | wxSIGBUS, | |
180 | wxSIGSEGV, | |
181 | wxSIGSYS, | |
182 | wxSIGPIPE, | |
183 | wxSIGALRM, | |
184 | wxSIGTERM // terminate the process gently | |
185 | }; | |
186 | \end{verbatim} | |
187 | ||
188 | {\tt wxSIGNONE}, {\tt wxSIGKILL} and {\tt wxSIGTERM} have the same meaning | |
e626d7c7 | 189 | under both Unix and Windows but all the other signals are equivalent to |
50567b69 VZ |
190 | {\tt wxSIGTERM} under Windows. |
191 | ||
e0f6b731 JS |
192 | The {\it flags} parameter can be wxKILL\_NOCHILDREN (the default), |
193 | or wxKILL\_CHILDREN, in which case the child processes of this | |
194 | process will be killed too. Note that under Unix, for wxKILL\_CHILDREN | |
bb772a8e | 195 | to work you should have created the process passing wxEXEC\_MAKE\_GROUP\_LEADER. |
e0f6b731 | 196 | |
50567b69 VZ |
197 | Returns the element of {\tt wxKillError} enum: |
198 | ||
199 | \begin{verbatim} | |
200 | enum wxKillError | |
201 | { | |
202 | wxKILL_OK, // no error | |
203 | wxKILL_BAD_SIGNAL, // no such signal | |
204 | wxKILL_ACCESS_DENIED, // permission denied | |
205 | wxKILL_NO_PROCESS, // no such process | |
206 | wxKILL_ERROR // another, unspecified error | |
207 | }; | |
208 | \end{verbatim} | |
209 | ||
210 | \wxheading{See also} | |
211 | ||
212 | \helpref{wxProcess::Exists}{wxprocessexists},\rtfsp | |
213 | \helpref{wxKill}{wxkill},\rtfsp | |
cb35465e | 214 | \helpref{Exec sample}{sampleexec} |
50567b69 | 215 | |
9722642d | 216 | \membersection{wxProcess::Exists}\label{wxprocessexists} |
50567b69 VZ |
217 | |
218 | \func{static bool}{Exists}{\param{int}{ pid}} | |
219 | ||
cc81d32f | 220 | Returns {\tt true} if the given process exists in the system. |
50567b69 VZ |
221 | |
222 | \wxheading{See also} | |
223 | ||
224 | \helpref{wxProcess::Kill}{wxprocesskill},\rtfsp | |
cb35465e | 225 | \helpref{Exec sample}{sampleexec} |
50567b69 | 226 | |
eafc087e GL |
227 | \membersection{wxProcess::OnTerminate}\label{wxprocessonterminate} |
228 | ||
6d98ee99 | 229 | \func{void}{OnTerminate}{\param{int}{ pid}, \param{int}{ status}} |
eafc087e GL |
230 | |
231 | It is called when the process with the pid {\it pid} finishes. | |
fc2171bd | 232 | It raises a wxWidgets event when it isn't overridden. |
eafc087e | 233 | |
7e84f02d VZ |
234 | \docparam{pid}{The pid of the process which has just terminated.} |
235 | ||
236 | \docparam{status}{The exit code of the process.} | |
3972fb49 | 237 | |
da00a8bb VZ |
238 | \membersection{wxProcess::Open}\label{wxprocessopen} |
239 | ||
ece83544 | 240 | \func{static wxProcess *}{Open}{\param{const wxString\& }{cmd}, \param{int }{flags = wxEXEC\_ASYNC}} |
da00a8bb VZ |
241 | |
242 | This static method replaces the standard {\tt popen()} function: it launches | |
243 | the process specified by the {\it 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. | |
246 | ||
247 | If the process couldn't be launched, {\tt NULL} is returned. Note that in any | |
248 | case the returned pointer should {\bf not} be deleted, rather the process | |
249 | object will be destroyed automatically when the child process terminates. This | |
250 | does mean that the child process should be told to quit before the main program | |
251 | exits to avoid memory leaks. | |
252 | ||
253 | \wxheading{Parameters} | |
254 | ||
255 | \docparam{cmd}{The command to execute, including optional arguments.} | |
feb6cde4 | 256 | \docparam{flags}{The flags to pass to \helpref{wxExecute}{wxexecute}. |
1233cee5 | 257 | NOTE: wxEXEC\_SYNC should not be used.} |
da00a8bb VZ |
258 | |
259 | \wxheading{Return value} | |
260 | ||
261 | A pointer to new wxProcess object or {\tt NULL} on error. | |
262 | ||
263 | \wxheading{See also} | |
264 | ||
265 | \helpref{wxExecute}{wxexecute} | |
266 | ||
a387938f JS |
267 | \membersection{wxProcess::GetPid}\label{wxprocessgetpid} |
268 | ||
269 | \constfunc{long}{GetPid}{\void} | |
270 | ||
271 | Returns the process ID of the process launched by \helpref{Open}{wxprocessopen}. | |
272 | ||
e8482f24 GL |
273 | \membersection{wxProcess::Redirect}\label{wxprocessredirect} |
274 | ||
275 | \func{void}{Redirect}{\void} | |
276 | ||
fe25efa3 | 277 | Turns on redirection. wxExecute will try to open a couple of pipes |
e8482f24 GL |
278 | to catch the subprocess stdio. The caught input stream is returned by |
279 | GetOutputStream() as a non-seekable stream. The caught output stream is returned | |
280 | by GetInputStream() as a non-seekable stream. | |
66f55ec6 | 281 |