]> git.saurik.com Git - wxWidgets.git/blame - src/msw/utilsexc.cpp
Minor doc tweaks plus major wxTreeCtrl bug fixed (double deletion of tree items
[wxWidgets.git] / src / msw / utilsexc.cpp
CommitLineData
32592631
GL
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
3f4a0c5b 9// Licence: wxWindows license
32592631
GL
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
cfe780fb 13#pragma implementation
32592631
GL
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"
2432b92d 27#include "wx/intl.h"
32592631
GL
28#endif
29
3e64d4e1 30#include "wx/log.h"
dbeac4bd
VZ
31#include "wx/process.h"
32
32592631 33#include "wx/msw/private.h"
dbeac4bd 34
32592631
GL
35#include <windows.h>
36
37#include <ctype.h>
38
ce3ed50d 39#if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
32592631 40#include <direct.h>
17dff81c 41#ifndef __MWERKS__
32592631
GL
42#include <dos.h>
43#endif
17dff81c 44#endif
32592631
GL
45
46#ifdef __GNUWIN32__
57c208c5 47#ifndef __TWIN32__
32592631
GL
48#include <sys/unistd.h>
49#include <sys/stat.h>
32592631 50#endif
57c208c5 51#endif
32592631
GL
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__
3f4a0c5b
VZ
65 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
66 #include <errno.h>
67 #endif
32592631
GL
68#endif
69#include <stdarg.h>
70
cb6827a8
VZ
71// this message is sent when the process we're waiting for terminates
72#define wxWM_PROC_TERMINATED (WM_USER + 10000)
32592631 73
cb6827a8
VZ
74// structure describing the process we're being waiting for
75struct wxExecuteData
76{
77public:
78 ~wxExecuteData()
79 {
750b78ba 80#ifndef __WIN16__
cb6827a8
VZ
81 if ( !::CloseHandle(hProcess) )
82 {
83 wxLogLastError("CloseHandle(hProcess)");
84 }
750b78ba 85#endif
cb6827a8
VZ
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
32592631
GL
94};
95
5260b1c5
JS
96
97#ifdef __WIN32__
32592631
GL
98static DWORD wxExecuteThread(wxExecuteData *data)
99{
cb6827a8
VZ
100 WaitForSingleObject(data->hProcess, INFINITE);
101
102 // get the exit code
103 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
104 {
105 wxLogLastError("GetExitCodeProcess");
106 }
32592631 107
cb6827a8
VZ
108 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
109 "process should have terminated" );
32592631 110
cb6827a8
VZ
111 // send a message indicating process termination to the window
112 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
e6045e08 113
cb6827a8 114 return 0;
32592631 115}
5260b1c5
JS
116#endif
117
cb6827a8
VZ
118// window procedure of a hidden window which is created just to receive
119// the notification message when a process exits
32592631
GL
120LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
121 WPARAM wParam, LPARAM lParam)
122{
cb6827a8
VZ
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 }
e6045e08 133
cb6827a8
VZ
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;
32592631
GL
148}
149
150extern char wxPanelClassName[];
151
152long wxExecute(const wxString& command, bool sync, wxProcess *handler)
153{
cb6827a8 154 wxCHECK_MSG( !!command, 0, "empty command in wxExecute" );
32592631 155
57c208c5 156#if defined(__WIN32__) && !defined(__TWIN32__)
cb6827a8
VZ
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;
32592631 195#ifdef __GNUWIN32__
cb6827a8
VZ
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
e6045e08 206
cb6827a8
VZ
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;
cf0b3979
JS
214#ifdef __GNUWIN32__
215 memset(&si, 0, sizeof(si));
216#else
cb6827a8 217 ::ZeroMemory(&si, sizeof(si));
cf0b3979
JS
218#endif
219
cb6827a8
VZ
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" );
e6045e08 251
cb6827a8
VZ
252 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC)wxExecuteWindowCbk,
253 wxGetInstance());
e6045e08 254
cb6827a8 255 ::SetWindowLong(hwnd, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
e6045e08 256
cb6827a8
VZ
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;
e6045e08
VZ
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 }
cb6827a8
VZ
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 }
e6045e08 293
cb6827a8
VZ
294 if ( !sync )
295 {
296 // clean up will be done when the process terminates
e6045e08
VZ
297
298 // return the pid
cb6827a8
VZ
299 return pi.dwProcessId;
300 }
e6045e08 301
cb6827a8
VZ
302 // waiting until command executed
303 while ( data->state )
304 wxYield();
e6045e08
VZ
305
306 DWORD dwExitCode = data->dwExitCode;
cb6827a8
VZ
307 delete data;
308
e6045e08
VZ
309 // return the exit code
310 return dwExitCode;
cb6827a8
VZ
311#endif // 0/1
312#else // Win16
313 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
314 if (instanceID < 32) return(0);
e6045e08 315
cb6827a8
VZ
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
32592631 326}
c740f496
GL
327
328long wxExecute(char **argv, bool sync, wxProcess *handler)
329{
cb6827a8 330 wxString command;
e6045e08 331
cb6827a8
VZ
332 while ( *argv != NULL )
333 {
334 command << *argv++ << ' ';
335 }
336
337 command.RemoveLast();
338
339 return wxExecute(command, sync, handler);
c740f496 340}