]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsexc.cpp
added test mode to wxLongLongWx which allowed to find the bug in operator*=()
[wxWidgets.git] / src / msw / utilsexc.cpp
CommitLineData
32592631 1/////////////////////////////////////////////////////////////////////////////
b568d04f 2// Name: msw/utilsexec.cpp
32592631
GL
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
3f4a0c5b 9// Licence: wxWindows license
32592631
GL
10/////////////////////////////////////////////////////////////////////////////
11
b568d04f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
32592631 20#ifdef __GNUG__
b568d04f 21 #pragma implementation
32592631
GL
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
b568d04f 28 #pragma hdrstop
32592631
GL
29#endif
30
31#ifndef WX_PRECOMP
b568d04f
VZ
32 #include "wx/utils.h"
33 #include "wx/app.h"
34 #include "wx/intl.h"
32592631
GL
35#endif
36
3e64d4e1 37#include "wx/log.h"
1044a386
JS
38
39#ifdef __WIN32__
b568d04f 40 #include "wx/process.h"
1044a386 41#endif
dbeac4bd 42
32592631 43#include "wx/msw/private.h"
dbeac4bd 44
32592631
GL
45#include <ctype.h>
46
5ea105e0 47#if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__)
b568d04f 48 #include <direct.h>
17dff81c 49#ifndef __MWERKS__
b568d04f 50 #include <dos.h>
32592631 51#endif
17dff81c 52#endif
32592631 53
b568d04f
VZ
54#if defined(__GNUWIN32__) && !defined(__TWIN32__)
55 #include <sys/unistd.h>
56 #include <sys/stat.h>
57c208c5 57#endif
32592631 58
5ea105e0 59#if defined(__WIN32__) && !defined(__WXWINE__)
32592631
GL
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__
3f4a0c5b
VZ
71 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
72 #include <errno.h>
73 #endif
32592631
GL
74#endif
75#include <stdarg.h>
76
b568d04f
VZ
77// ----------------------------------------------------------------------------
78// constants
79// ----------------------------------------------------------------------------
80
cb6827a8
VZ
81// this message is sent when the process we're waiting for terminates
82#define wxWM_PROC_TERMINATED (WM_USER + 10000)
32592631 83
b568d04f
VZ
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
cb6827a8
VZ
97// structure describing the process we're being waiting for
98struct wxExecuteData
99{
100public:
101 ~wxExecuteData()
102 {
750b78ba 103#ifndef __WIN16__
cb6827a8
VZ
104 if ( !::CloseHandle(hProcess) )
105 {
106 wxLogLastError("CloseHandle(hProcess)");
107 }
750b78ba 108#endif
cb6827a8
VZ
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
32592631
GL
117};
118
b568d04f
VZ
119// ============================================================================
120// implementation
121// ============================================================================
5260b1c5
JS
122
123#ifdef __WIN32__
32592631
GL
124static DWORD wxExecuteThread(wxExecuteData *data)
125{
cb6827a8
VZ
126 WaitForSingleObject(data->hProcess, INFINITE);
127
128 // get the exit code
129 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
130 {
131 wxLogLastError("GetExitCodeProcess");
132 }
32592631 133
cb6827a8 134 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
223d09f6 135 wxT("process should have terminated") );
32592631 136
cb6827a8
VZ
137 // send a message indicating process termination to the window
138 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
e6045e08 139
cb6827a8 140 return 0;
32592631 141}
5260b1c5 142
cb6827a8
VZ
143// window procedure of a hidden window which is created just to receive
144// the notification message when a process exits
32592631
GL
145LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
146 WPARAM wParam, LPARAM lParam)
147{
cb6827a8
VZ
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 }
e6045e08 158
cb6827a8
VZ
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;
32592631 173}
1044a386 174#endif
32592631 175
32592631
GL
176long wxExecute(const wxString& command, bool sync, wxProcess *handler)
177{
223d09f6 178 wxCHECK_MSG( !!command, 0, wxT("empty command in wxExecute") );
32592631 179
57c208c5 180#if defined(__WIN32__) && !defined(__TWIN32__)
cb6827a8
VZ
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;
32592631 219#ifdef __GNUWIN32__
cb6827a8
VZ
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
e6045e08 230
cb6827a8
VZ
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;
11c7d5b6 238 wxZeroMemory(si);
cf0b3979 239
cb6827a8
VZ
240 si.cb = sizeof(si);
241
242 PROCESS_INFORMATION pi;
243
244 if ( ::CreateProcess(
245 NULL, // application name (use only cmd line)
837e5743 246 (wxChar *)command.c_str(), // full command line
cb6827a8
VZ
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
b568d04f
VZ
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
cb6827a8
VZ
284 // create a hidden window to receive notification about process
285 // termination
b568d04f
VZ
286 HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL,
287 0, 0, 0, 0, 0, NULL,
cb6827a8 288 (HMENU)NULL, wxGetInstance(), 0);
223d09f6 289 wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") );
e6045e08 290
cb6827a8
VZ
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;
e6045e08
VZ
297 if ( sync )
298 {
223d09f6 299 wxASSERT_MSG( !handler, wxT("wxProcess param ignored for sync execution") );
e6045e08
VZ
300
301 data->handler = NULL;
302 }
303 else
304 {
305 // may be NULL or not
306 data->handler = handler;
307 }
cb6827a8
VZ
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 }
e6045e08 327
cb6827a8
VZ
328 if ( !sync )
329 {
330 // clean up will be done when the process terminates
e6045e08
VZ
331
332 // return the pid
cb6827a8
VZ
333 return pi.dwProcessId;
334 }
e6045e08 335
cb6827a8
VZ
336 // waiting until command executed
337 while ( data->state )
338 wxYield();
e6045e08
VZ
339
340 DWORD dwExitCode = data->dwExitCode;
cb6827a8
VZ
341 delete data;
342
e6045e08
VZ
343 // return the exit code
344 return dwExitCode;
cb6827a8
VZ
345#endif // 0/1
346#else // Win16
347 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
348 if (instanceID < 32) return(0);
e6045e08 349
cb6827a8
VZ
350 if (sync) {
351 int running;
352 do {
353 wxYield();
27a9bd48 354 running = GetModuleUsage((HINSTANCE)instanceID);
cb6827a8
VZ
355 } while (running);
356 }
357
358 return(instanceID);
359#endif // Win16/32
32592631 360}
c740f496
GL
361
362long wxExecute(char **argv, bool sync, wxProcess *handler)
363{
cb6827a8 364 wxString command;
e6045e08 365
cb6827a8
VZ
366 while ( *argv != NULL )
367 {
368 command << *argv++ << ' ';
369 }
370
371 command.RemoveLast();
372
373 return wxExecute(command, sync, handler);
c740f496 374}
006162a9 375