]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/utilsexc.cpp
Fixed wxPalette memory leak when loading BMP files
[wxWidgets.git] / src / msw / utilsexc.cpp
... / ...
CommitLineData
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
75struct wxExecuteData
76{
77public:
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__
98static 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
120LRESULT 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
150extern char wxPanelClassName[];
151
152long 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#ifdef __GNUWIN32__
215 memset(&si, 0, sizeof(si));
216#else
217 ::ZeroMemory(&si, sizeof(si));
218#endif
219
220 si.cb = sizeof(si);
221
222 PROCESS_INFORMATION pi;
223
224 if ( ::CreateProcess(
225 NULL, // application name (use only cmd line)
226 (char *)command.c_str(), // full command line
227 NULL, // security attributes: defaults for both
228 NULL, // the process and its main thread
229 FALSE, // don't inherit handles
230 CREATE_DEFAULT_ERROR_MODE, // flags
231 NULL, // environment (use the same)
232 NULL, // current directory (use the same)
233 &si, // startup info (unused here)
234 &pi // process info
235 ) == 0 )
236 {
237 wxLogSysError(_("Execution of command '%s' failed"), command.c_str());
238
239 return 0;
240 }
241
242 // close unneeded handle
243 if ( !::CloseHandle(pi.hThread) )
244 wxLogLastError("CloseHandle(hThread)");
245
246 // create a hidden window to receive notification about process
247 // termination
248 HWND hwnd = ::CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
249 (HMENU)NULL, wxGetInstance(), 0);
250 wxASSERT_MSG( hwnd, "can't create a hidden window for wxExecute" );
251
252 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC)wxExecuteWindowCbk,
253 wxGetInstance());
254
255 ::SetWindowLong(hwnd, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
256
257 // Alloc data
258 wxExecuteData *data = new wxExecuteData;
259 data->hProcess = pi.hProcess;
260 data->dwProcessId = pi.dwProcessId;
261 data->hWnd = hwnd;
262 data->state = sync;
263 if ( sync )
264 {
265 wxASSERT_MSG( !handler, "wxProcess param ignored for sync execution" );
266
267 data->handler = NULL;
268 }
269 else
270 {
271 // may be NULL or not
272 data->handler = handler;
273 }
274
275 DWORD tid;
276 HANDLE hThread = ::CreateThread(NULL,
277 0,
278 (LPTHREAD_START_ROUTINE)wxExecuteThread,
279 (void *)data,
280 0,
281 &tid);
282
283 if ( !hThread )
284 {
285 wxLogLastError("CreateThread in wxExecute");
286
287 DestroyWindow(hwnd);
288 delete data;
289
290 // the process still started up successfully...
291 return pi.dwProcessId;
292 }
293
294 if ( !sync )
295 {
296 // clean up will be done when the process terminates
297
298 // return the pid
299 return pi.dwProcessId;
300 }
301
302 // waiting until command executed
303 while ( data->state )
304 wxYield();
305
306 DWORD dwExitCode = data->dwExitCode;
307 delete data;
308
309 // return the exit code
310 return dwExitCode;
311#endif // 0/1
312#else // Win16
313 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
314 if (instanceID < 32) return(0);
315
316 if (sync) {
317 int running;
318 do {
319 wxYield();
320 running = GetModuleUsage((HANDLE)instanceID);
321 } while (running);
322 }
323
324 return(instanceID);
325#endif // Win16/32
326}
327
328long wxExecute(char **argv, bool sync, wxProcess *handler)
329{
330 wxString command;
331
332 while ( *argv != NULL )
333 {
334 command << *argv++ << ' ';
335 }
336
337 command.RemoveLast();
338
339 return wxExecute(command, sync, handler);
340}