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