]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsexc.cpp
wxSplitterWindow::ReplaceWindow() function added and documented
[wxWidgets.git] / src / msw / utilsexc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilsexec.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #include "wx/utils.h"
26 #include "wx/app.h"
27 #include "wx/intl.h"
28 #endif
29
30 #include "wx/log.h"
31 #include "wx/process.h"
32
33 #include "wx/msw/private.h"
34
35 #include <windows.h>
36
37 #include <ctype.h>
38
39 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
40 #include <direct.h>
41 #ifndef __MWERKS__
42 #include <dos.h>
43 #endif
44 #endif
45
46 #ifdef __GNUWIN32__
47 #ifndef __TWIN32__
48 #include <sys/unistd.h>
49 #include <sys/stat.h>
50 #endif
51 #endif
52
53 #ifdef __WIN32__
54 #include <io.h>
55
56 #ifndef __GNUWIN32__
57 #include <shellapi.h>
58 #endif
59 #endif
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #ifndef __WATCOMC__
65 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
66 #include <errno.h>
67 #endif
68 #endif
69 #include <stdarg.h>
70
71 // this message is sent when the process we're waiting for terminates
72 #define wxWM_PROC_TERMINATED (WM_USER + 10000)
73
74 // structure describing the process we're being waiting for
75 struct wxExecuteData
76 {
77 public:
78 ~wxExecuteData()
79 {
80 if ( !::CloseHandle(hProcess) )
81 {
82 wxLogLastError("CloseHandle(hProcess)");
83 }
84 }
85
86 HWND hWnd; // window to send wxWM_PROC_TERMINATED to
87 HANDLE hProcess; // handle of the process
88 DWORD dwProcessId; // pid of the process
89 wxProcess *handler;
90 DWORD dwExitCode; // the exit code of the process
91 bool state; // set to FALSE when the process finishes
92 };
93
94
95 #ifdef __WIN32__
96 static DWORD wxExecuteThread(wxExecuteData *data)
97 {
98 WaitForSingleObject(data->hProcess, INFINITE);
99
100 // get the exit code
101 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
102 {
103 wxLogLastError("GetExitCodeProcess");
104 }
105
106 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
107 "process should have terminated" );
108
109 // send a message indicating process termination to the window
110 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
111
112 return 0;
113 }
114 #endif
115
116 // window procedure of a hidden window which is created just to receive
117 // the notification message when a process exits
118 LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
119 WPARAM wParam, LPARAM lParam)
120 {
121 if ( message == wxWM_PROC_TERMINATED )
122 {
123 DestroyWindow(hWnd); // we don't need it any more
124
125 wxExecuteData *data = (wxExecuteData *)lParam;
126 if ( data->handler )
127 {
128 data->handler->OnTerminate((int)data->dwProcessId,
129 (int)data->dwExitCode);
130 }
131
132 if ( data->state )
133 {
134 // we're executing synchronously, tell the waiting thread
135 // that the process finished
136 data->state = 0;
137 }
138 else
139 {
140 // asynchronous execution - we should do the clean up
141 delete data;
142 }
143 }
144
145 return 0;
146 }
147
148 extern char wxPanelClassName[];
149
150 long wxExecute(const wxString& command, bool sync, wxProcess *handler)
151 {
152 wxCHECK_MSG( !!command, 0, "empty command in wxExecute" );
153
154 #if defined(__WIN32__) && !defined(__TWIN32__)
155 // the old code is disabled because we really need a process handle
156 // if we want to execute it asynchronously or even just get its
157 // return code and for this we must use CreateProcess() and not
158 // ShellExecute()
159 #if 0
160 // isolate command and arguments
161 wxString commandName;
162 bool insideQuotes = FALSE;
163 const char *pc;
164 for ( pc = command.c_str(); *pc != '\0'; pc++ )
165 {
166 switch ( *pc )
167 {
168 case ' ':
169 case '\t':
170 if ( !insideQuotes )
171 break;
172 // fall through
173
174 case '"':
175 insideQuotes = !insideQuotes;
176 // fall through
177
178 default:
179 commandName += *pc;
180 continue; // skip the next break
181 }
182
183 // only reached for space not inside quotes
184 break;
185 }
186
187 wxString commandArgs = pc;
188
189 wxWindow *winTop = wxTheApp->GetTopWindow();
190 HWND hwndTop = (HWND)(winTop ? winTop->GetHWND() : 0);
191
192 HANDLE result;
193 #ifdef __GNUWIN32__
194 result = ShellExecute(hwndTop,
195 (const wchar_t)"open",
196 (const wchar_t)commandName,
197 (const wchar_t)commandArgs,
198 (const wchar_t)NULL,
199 SW_SHOWNORMAL);
200 #else // !GNUWIN32
201 result = ShellExecute(hwndTop, "open", commandName,
202 commandArgs, NULL, SW_SHOWNORMAL);
203 #endif // GNUWIN32
204
205 if ( ((long)result) <= 32 )
206 wxLogSysError(_("Can't execute command '%s'"), command.c_str());
207
208 return result;
209 #else // 1
210 // create the process
211 STARTUPINFO si;
212 ::ZeroMemory(&si, sizeof(si));
213 si.cb = sizeof(si);
214
215 PROCESS_INFORMATION pi;
216
217 if ( ::CreateProcess(
218 NULL, // application name (use only cmd line)
219 (char *)command.c_str(), // full command line
220 NULL, // security attributes: defaults for both
221 NULL, // the process and its main thread
222 FALSE, // don't inherit handles
223 CREATE_DEFAULT_ERROR_MODE, // flags
224 NULL, // environment (use the same)
225 NULL, // current directory (use the same)
226 &si, // startup info (unused here)
227 &pi // process info
228 ) == 0 )
229 {
230 wxLogSysError(_("Execution of command '%s' failed"), command.c_str());
231
232 return 0;
233 }
234
235 // close unneeded handle
236 if ( !::CloseHandle(pi.hThread) )
237 wxLogLastError("CloseHandle(hThread)");
238
239 // create a hidden window to receive notification about process
240 // termination
241 HWND hwnd = ::CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
242 (HMENU)NULL, wxGetInstance(), 0);
243 wxASSERT_MSG( hwnd, "can't create a hidden window for wxExecute" );
244
245 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC)wxExecuteWindowCbk,
246 wxGetInstance());
247
248 ::SetWindowLong(hwnd, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
249
250 // Alloc data
251 wxExecuteData *data = new wxExecuteData;
252 data->hProcess = pi.hProcess;
253 data->dwProcessId = pi.dwProcessId;
254 data->hWnd = hwnd;
255 data->state = sync;
256 data->handler = handler;
257
258 DWORD tid;
259 HANDLE hThread = ::CreateThread(NULL,
260 0,
261 (LPTHREAD_START_ROUTINE)wxExecuteThread,
262 (void *)data,
263 0,
264 &tid);
265
266 if ( !hThread )
267 {
268 wxLogLastError("CreateThread in wxExecute");
269
270 DestroyWindow(hwnd);
271 delete data;
272
273 // the process still started up successfully...
274 return pi.dwProcessId;
275 }
276
277 if ( !sync )
278 {
279 // clean up will be done when the process terminates
280 return pi.dwProcessId;
281 }
282
283 // waiting until command executed
284 while ( data->state )
285 wxYield();
286
287 delete data;
288
289 return pi.dwProcessId;
290 #endif // 0/1
291 #else // Win16
292 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
293 if (instanceID < 32) return(0);
294
295 if (sync) {
296 int running;
297 do {
298 wxYield();
299 running = GetModuleUsage((HANDLE)instanceID);
300 } while (running);
301 }
302
303 return(instanceID);
304 #endif // Win16/32
305 }
306
307 long wxExecute(char **argv, bool sync, wxProcess *handler)
308 {
309 wxString command;
310
311 while ( *argv != NULL )
312 {
313 command << *argv++ << ' ';
314 }
315
316 command.RemoveLast();
317
318 return wxExecute(command, sync, handler);
319 }