]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsexc.cpp
Cured some small doc typos; some WIN16 fixes; transferred DLL WinMain to
[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 #ifndef __WIN16__
81 if ( !::CloseHandle(hProcess) )
82 {
83 wxLogLastError("CloseHandle(hProcess)");
84 }
85 #endif
86 }
87
88 HWND hWnd; // window to send wxWM_PROC_TERMINATED to
89 HANDLE hProcess; // handle of the process
90 DWORD dwProcessId; // pid of the process
91 wxProcess *handler;
92 DWORD dwExitCode; // the exit code of the process
93 bool state; // set to FALSE when the process finishes
94 };
95
96
97 #ifdef __WIN32__
98 static DWORD wxExecuteThread(wxExecuteData *data)
99 {
100 WaitForSingleObject(data->hProcess, INFINITE);
101
102 // get the exit code
103 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
104 {
105 wxLogLastError("GetExitCodeProcess");
106 }
107
108 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
109 "process should have terminated" );
110
111 // send a message indicating process termination to the window
112 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
113
114 return 0;
115 }
116 #endif
117
118 // window procedure of a hidden window which is created just to receive
119 // the notification message when a process exits
120 LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
121 WPARAM wParam, LPARAM lParam)
122 {
123 if ( message == wxWM_PROC_TERMINATED )
124 {
125 DestroyWindow(hWnd); // we don't need it any more
126
127 wxExecuteData *data = (wxExecuteData *)lParam;
128 if ( data->handler )
129 {
130 data->handler->OnTerminate((int)data->dwProcessId,
131 (int)data->dwExitCode);
132 }
133
134 if ( data->state )
135 {
136 // we're executing synchronously, tell the waiting thread
137 // that the process finished
138 data->state = 0;
139 }
140 else
141 {
142 // asynchronous execution - we should do the clean up
143 delete data;
144 }
145 }
146
147 return 0;
148 }
149
150 extern char wxPanelClassName[];
151
152 long wxExecute(const wxString& command, bool sync, wxProcess *handler)
153 {
154 wxCHECK_MSG( !!command, 0, "empty command in wxExecute" );
155
156 #if defined(__WIN32__) && !defined(__TWIN32__)
157 // the old code is disabled because we really need a process handle
158 // if we want to execute it asynchronously or even just get its
159 // return code and for this we must use CreateProcess() and not
160 // ShellExecute()
161 #if 0
162 // isolate command and arguments
163 wxString commandName;
164 bool insideQuotes = FALSE;
165 const char *pc;
166 for ( pc = command.c_str(); *pc != '\0'; pc++ )
167 {
168 switch ( *pc )
169 {
170 case ' ':
171 case '\t':
172 if ( !insideQuotes )
173 break;
174 // fall through
175
176 case '"':
177 insideQuotes = !insideQuotes;
178 // fall through
179
180 default:
181 commandName += *pc;
182 continue; // skip the next break
183 }
184
185 // only reached for space not inside quotes
186 break;
187 }
188
189 wxString commandArgs = pc;
190
191 wxWindow *winTop = wxTheApp->GetTopWindow();
192 HWND hwndTop = (HWND)(winTop ? winTop->GetHWND() : 0);
193
194 HANDLE result;
195 #ifdef __GNUWIN32__
196 result = ShellExecute(hwndTop,
197 (const wchar_t)"open",
198 (const wchar_t)commandName,
199 (const wchar_t)commandArgs,
200 (const wchar_t)NULL,
201 SW_SHOWNORMAL);
202 #else // !GNUWIN32
203 result = ShellExecute(hwndTop, "open", commandName,
204 commandArgs, NULL, SW_SHOWNORMAL);
205 #endif // GNUWIN32
206
207 if ( ((long)result) <= 32 )
208 wxLogSysError(_("Can't execute command '%s'"), command.c_str());
209
210 return result;
211 #else // 1
212 // create the process
213 STARTUPINFO si;
214 ::ZeroMemory(&si, sizeof(si));
215 si.cb = sizeof(si);
216
217 PROCESS_INFORMATION pi;
218
219 if ( ::CreateProcess(
220 NULL, // application name (use only cmd line)
221 (char *)command.c_str(), // full command line
222 NULL, // security attributes: defaults for both
223 NULL, // the process and its main thread
224 FALSE, // don't inherit handles
225 CREATE_DEFAULT_ERROR_MODE, // flags
226 NULL, // environment (use the same)
227 NULL, // current directory (use the same)
228 &si, // startup info (unused here)
229 &pi // process info
230 ) == 0 )
231 {
232 wxLogSysError(_("Execution of command '%s' failed"), command.c_str());
233
234 return 0;
235 }
236
237 // close unneeded handle
238 if ( !::CloseHandle(pi.hThread) )
239 wxLogLastError("CloseHandle(hThread)");
240
241 // create a hidden window to receive notification about process
242 // termination
243 HWND hwnd = ::CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
244 (HMENU)NULL, wxGetInstance(), 0);
245 wxASSERT_MSG( hwnd, "can't create a hidden window for wxExecute" );
246
247 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC)wxExecuteWindowCbk,
248 wxGetInstance());
249
250 ::SetWindowLong(hwnd, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
251
252 // Alloc data
253 wxExecuteData *data = new wxExecuteData;
254 data->hProcess = pi.hProcess;
255 data->dwProcessId = pi.dwProcessId;
256 data->hWnd = hwnd;
257 data->state = sync;
258 data->handler = handler;
259
260 DWORD tid;
261 HANDLE hThread = ::CreateThread(NULL,
262 0,
263 (LPTHREAD_START_ROUTINE)wxExecuteThread,
264 (void *)data,
265 0,
266 &tid);
267
268 if ( !hThread )
269 {
270 wxLogLastError("CreateThread in wxExecute");
271
272 DestroyWindow(hwnd);
273 delete data;
274
275 // the process still started up successfully...
276 return pi.dwProcessId;
277 }
278
279 if ( !sync )
280 {
281 // clean up will be done when the process terminates
282 return pi.dwProcessId;
283 }
284
285 // waiting until command executed
286 while ( data->state )
287 wxYield();
288
289 delete data;
290
291 return pi.dwProcessId;
292 #endif // 0/1
293 #else // Win16
294 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
295 if (instanceID < 32) return(0);
296
297 if (sync) {
298 int running;
299 do {
300 wxYield();
301 running = GetModuleUsage((HANDLE)instanceID);
302 } while (running);
303 }
304
305 return(instanceID);
306 #endif // Win16/32
307 }
308
309 long wxExecute(char **argv, bool sync, wxProcess *handler)
310 {
311 wxString command;
312
313 while ( *argv != NULL )
314 {
315 command << *argv++ << ' ';
316 }
317
318 command.RemoveLast();
319
320 return wxExecute(command, sync, handler);
321 }