]>
git.saurik.com Git - wxWidgets.git/blob - interface/process.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxProcess
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 The objects of this class are used in conjunction with the
14 wxExecute() function. When a wxProcess object is passed to
15 wxExecute(), its wxProcess::OnTerminate virtual method
16 is called when the process terminates. This allows the program to be
17 (asynchronously) notified about the process termination and also retrieve its
18 exit status which is unavailable from wxExecute() in the case of
19 asynchronous execution.
21 Please note that if the process termination notification is processed by the
22 parent, it is responsible for deleting the wxProcess object which sent it.
23 However, if it is not processed, the object will delete itself and so the
24 library users should only delete those objects whose notifications have been
25 processed (and call wxProcess::Detach for others).
27 wxProcess also supports IO redirection of the child process. For this, you have
28 to call its wxProcess::Redirect method before passing it to
29 wxExecute(). If the child process was launched successfully,
30 wxProcess::GetInputStream,
31 wxProcess::GetOutputStream and
32 wxProcess::GetErrorStream can then be used to retrieve
33 the streams corresponding to the child process standard output, input and
34 error output respectively.
36 @b wxPerl note: In wxPerl this class has an additional @c Destroy method,
37 for explicit destruction.
40 @category{appmanagement}
42 @see wxExecute(), @ref overview_sampleexec "exec sample"
44 class wxProcess
: public wxEvtHandler
49 Constructs a process object. @a id is only used in the case you want to
50 use wxWidgets events. It identifies this object, or another window that will
52 If the @a parent parameter is different from @NULL, it will receive
53 a wxEVT_END_PROCESS notification event (you should insert EVT_END_PROCESS
54 macro in the event table of the parent to handle it) with the given @e id.
55 The second constructor creates an object without any associated parent (and
56 hence no id neither) but allows to specify the @a flags which can have the
57 value of @c wxPROCESS_DEFAULT or @c wxPROCESS_REDIRECT. Specifying the
58 former value has no particular effect while using the latter one is equivalent
59 to calling Redirect().
62 The event handler parent.
66 either wxPROCESS_DEFAULT or wxPROCESS_REDIRECT
68 wxProcess(wxEvtHandler
* parent
= NULL
, int id
= -1);
73 Destroys the wxProcess object.
78 Closes the output stream (the one connected to the stdin of the child
79 process). This function can be used to indicate to the child process that
80 there is no more data to be read - usually, a filter program will only
81 terminate when the input stream is closed.
86 Normally, a wxProcess object is deleted by its parent when it receives the
87 notification about the process termination. However, it might happen that the
88 parent object is destroyed before the external process is terminated (e.g. a
89 window from which this external process was launched is closed by the user)
90 and in this case it @b should not delete the wxProcess object, but
91 @b should call Detach() instead. After the wxProcess object is detached
92 from its parent, no notification events will be sent to the parent and the
93 object will delete itself upon reception of the process termination
99 Returns @true if the given process exists in the system.
101 @see Kill(), @ref overview_sampleexec "Exec sample"
103 static bool Exists(int pid
);
106 Returns an input stream which corresponds to the standard error output (stderr)
107 of the child process.
109 wxInputStream
* GetErrorStream() const;
112 It returns an input stream corresponding to the standard output stream of the
113 subprocess. If it is @NULL, you have not turned on the redirection.
116 wxInputStream
* GetInputStream() const;
119 It returns an output stream correspoding to the input stream of the subprocess.
120 If it is @NULL, you have not turned on the redirection.
123 wxOutputStream
* GetOutputStream() const;
126 Returns the process ID of the process launched by Open().
131 Returns @true if there is data to be read on the child process standard
134 @see IsInputAvailable()
136 bool IsErrorAvailable() const;
139 Returns @true if there is data to be read on the child process standard
140 output stream. This allows to write simple (and extremely inefficient)
141 polling-based code waiting for a better mechanism in future wxWidgets versions.
142 See the @ref overview_sampleexec "exec sample" for an example of using this
147 bool IsInputAvailable() const;
150 Returns @true if the child process standard output stream is opened.
152 bool IsInputOpened() const;
155 Send the specified signal to the given process. Possible signal values are:
157 @c wxSIGNONE, @c wxSIGKILL and @c wxSIGTERM have the same meaning
158 under both Unix and Windows but all the other signals are equivalent to
159 @c wxSIGTERM under Windows.
160 The @a flags parameter can be wxKILL_NOCHILDREN (the default),
161 or wxKILL_CHILDREN, in which case the child processes of this
162 process will be killed too. Note that under Unix, for wxKILL_CHILDREN
163 to work you should have created the process passing wxEXEC_MAKE_GROUP_LEADER.
164 Returns the element of @c wxKillError enum:
166 @see Exists(), wxKill(), @ref overview_sampleexec "Exec sample"
168 static wxKillError
Kill(int pid
, wxSignal signal
= wxSIGNONE
,
169 int flags
= wxKILL_NOCHILDREN
);
172 It is called when the process with the pid
175 It raises a wxWidgets event when it isn't overridden.
178 The pid of the process which has just terminated.
180 The exit code of the process.
182 void OnTerminate(int pid
, int status
);
185 This static method replaces the standard @c popen() function: it launches
186 the process specified by the @a cmd parameter and returns the wxProcess
187 object which can be used to retrieve the streams connected to the standard
188 input, output and error output of the child process.
189 If the process couldn't be launched, @NULL is returned. Note that in any
190 case the returned pointer should @b not be deleted, rather the process
191 object will be destroyed automatically when the child process terminates. This
192 does mean that the child process should be told to quit before the main program
193 exits to avoid memory leaks.
196 The command to execute, including optional arguments.
198 The flags to pass to wxExecute.
199 NOTE: wxEXEC_SYNC should not be used.
201 @returns A pointer to new wxProcess object or @NULL on error.
205 static wxProcess
* Open(const wxString
& cmd
,
206 int flags
= wxEXEC_ASYNC
);
209 Turns on redirection. wxExecute will try to open a couple of pipes
210 to catch the subprocess stdio. The caught input stream is returned by
211 GetOutputStream() as a non-seekable stream. The caught output stream is returned
212 by GetInputStream() as a non-seekable stream.
220 @class wxProcessEvent
223 A process event is sent when a process is terminated.
228 @see wxProcess, @ref overview_eventhandlingoverview
230 class wxProcessEvent
: public wxEvent
234 Constructor. Takes a wxProcessObject or window id, a process id and an
237 wxProcessEvent(int id
= 0, int pid
= 0, int exitcode
= 0);
240 Returns the exist status.
245 Returns the process id.