]>
Commit | Line | Data |
---|---|---|
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__ | |
8b33ae2d | 40 | #include "wx/stream.h" |
b568d04f | 41 | #include "wx/process.h" |
1044a386 | 42 | #endif |
dbeac4bd | 43 | |
32592631 | 44 | #include "wx/msw/private.h" |
dbeac4bd | 45 | |
32592631 GL |
46 | #include <ctype.h> |
47 | ||
5ea105e0 | 48 | #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) |
b568d04f | 49 | #include <direct.h> |
17dff81c | 50 | #ifndef __MWERKS__ |
b568d04f | 51 | #include <dos.h> |
32592631 | 52 | #endif |
17dff81c | 53 | #endif |
32592631 | 54 | |
b568d04f VZ |
55 | #if defined(__GNUWIN32__) && !defined(__TWIN32__) |
56 | #include <sys/unistd.h> | |
57 | #include <sys/stat.h> | |
57c208c5 | 58 | #endif |
32592631 | 59 | |
5ea105e0 | 60 | #if defined(__WIN32__) && !defined(__WXWINE__) |
32592631 GL |
61 | #include <io.h> |
62 | ||
63 | #ifndef __GNUWIN32__ | |
64 | #include <shellapi.h> | |
65 | #endif | |
66 | #endif | |
67 | ||
68 | #include <stdio.h> | |
69 | #include <stdlib.h> | |
70 | #include <string.h> | |
71 | #ifndef __WATCOMC__ | |
3f4a0c5b VZ |
72 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
73 | #include <errno.h> | |
74 | #endif | |
32592631 GL |
75 | #endif |
76 | #include <stdarg.h> | |
77 | ||
731dd422 VZ |
78 | #if wxUSE_IPC |
79 | #include "wx/dde.h" // for WX_DDE hack in wxExecute | |
80 | #endif // wxUSE_IPC | |
5bd3a2da | 81 | |
b568d04f VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // constants | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
cb6827a8 VZ |
86 | // this message is sent when the process we're waiting for terminates |
87 | #define wxWM_PROC_TERMINATED (WM_USER + 10000) | |
32592631 | 88 | |
b568d04f VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // this module globals | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // we need to create a hidden window to receive the process termination | |
94 | // notifications and for this we need a (Win) class name for it which we will | |
95 | // register the first time it's needed | |
96 | static const wxChar *gs_classForHiddenWindow = NULL; | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // private types | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
cb6827a8 VZ |
102 | // structure describing the process we're being waiting for |
103 | struct wxExecuteData | |
104 | { | |
105 | public: | |
106 | ~wxExecuteData() | |
107 | { | |
750b78ba | 108 | #ifndef __WIN16__ |
cb6827a8 VZ |
109 | if ( !::CloseHandle(hProcess) ) |
110 | { | |
f6bcfd97 | 111 | wxLogLastError(wxT("CloseHandle(hProcess)")); |
cb6827a8 | 112 | } |
750b78ba | 113 | #endif |
cb6827a8 VZ |
114 | } |
115 | ||
116 | HWND hWnd; // window to send wxWM_PROC_TERMINATED to | |
117 | HANDLE hProcess; // handle of the process | |
118 | DWORD dwProcessId; // pid of the process | |
119 | wxProcess *handler; | |
120 | DWORD dwExitCode; // the exit code of the process | |
121 | bool state; // set to FALSE when the process finishes | |
32592631 GL |
122 | }; |
123 | ||
f6bcfd97 | 124 | #if defined(__WIN32__) && wxUSE_STREAMS |
8b33ae2d | 125 | |
8b33ae2d GL |
126 | // ---------------------------------------------------------------------------- |
127 | // wxPipeStreams | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
f6bcfd97 BP |
130 | class wxPipeInputStream: public wxInputStream |
131 | { | |
8b33ae2d GL |
132 | public: |
133 | wxPipeInputStream(HANDLE hInput); | |
134 | ~wxPipeInputStream(); | |
135 | ||
f6bcfd97 BP |
136 | virtual bool Eof() const; |
137 | ||
8b33ae2d GL |
138 | protected: |
139 | size_t OnSysRead(void *buffer, size_t len); | |
140 | ||
141 | protected: | |
142 | HANDLE m_hInput; | |
143 | }; | |
144 | ||
f6bcfd97 BP |
145 | class wxPipeOutputStream: public wxOutputStream |
146 | { | |
8b33ae2d GL |
147 | public: |
148 | wxPipeOutputStream(HANDLE hOutput); | |
149 | ~wxPipeOutputStream(); | |
150 | ||
151 | protected: | |
152 | size_t OnSysWrite(const void *buffer, size_t len); | |
153 | ||
154 | protected: | |
155 | HANDLE m_hOutput; | |
156 | }; | |
157 | ||
158 | // ================== | |
159 | // wxPipeInputStream | |
160 | // ================== | |
161 | ||
162 | wxPipeInputStream::wxPipeInputStream(HANDLE hInput) | |
163 | { | |
164 | m_hInput = hInput; | |
cd6ce4a9 | 165 | } |
8b33ae2d GL |
166 | |
167 | wxPipeInputStream::~wxPipeInputStream() | |
168 | { | |
169 | ::CloseHandle(m_hInput); | |
170 | } | |
171 | ||
f6bcfd97 | 172 | bool wxPipeInputStream::Eof() const |
8b33ae2d | 173 | { |
f6bcfd97 BP |
174 | DWORD nAvailable; |
175 | ||
176 | // function name is misleading, it works with anon pipes as well | |
177 | DWORD rc = ::PeekNamedPipe | |
178 | ( | |
179 | m_hInput, // handle | |
180 | NULL, 0, // ptr to buffer and its size | |
181 | NULL, // [out] bytes read | |
182 | &nAvailable, // [out] bytes available | |
183 | NULL // [out] bytes left | |
184 | ); | |
185 | ||
186 | if ( !rc ) | |
187 | { | |
188 | if ( ::GetLastError() != ERROR_BROKEN_PIPE ) | |
189 | { | |
190 | // unexpected error | |
191 | wxLogLastError(_T("PeekNamedPipe")); | |
192 | } | |
8b33ae2d | 193 | |
f6bcfd97 BP |
194 | // don't try to continue reading from a pipe if an error occured or if |
195 | // it had been closed | |
196 | return TRUE; | |
197 | } | |
198 | else | |
199 | { | |
200 | return nAvailable == 0; | |
201 | } | |
202 | } | |
203 | ||
204 | size_t wxPipeInputStream::OnSysRead(void *buffer, size_t len) | |
205 | { | |
206 | // reading from a pipe may block if there is no more data, always check for | |
207 | // EOF first | |
8b33ae2d | 208 | m_lasterror = wxSTREAM_NOERROR; |
f6bcfd97 BP |
209 | if ( Eof() ) |
210 | return 0; | |
211 | ||
212 | DWORD bytesRead; | |
213 | if ( !::ReadFile(m_hInput, buffer, len, &bytesRead, NULL) ) | |
214 | { | |
215 | if ( ::GetLastError() == ERROR_BROKEN_PIPE ) | |
8b33ae2d GL |
216 | m_lasterror = wxSTREAM_EOF; |
217 | else | |
218 | m_lasterror = wxSTREAM_READ_ERROR; | |
219 | } | |
f6bcfd97 | 220 | |
8b33ae2d GL |
221 | return bytesRead; |
222 | } | |
223 | ||
224 | // ================== | |
225 | // wxPipeOutputStream | |
226 | // ================== | |
227 | ||
228 | wxPipeOutputStream::wxPipeOutputStream(HANDLE hOutput) | |
229 | { | |
230 | m_hOutput = hOutput; | |
cd6ce4a9 | 231 | } |
8b33ae2d GL |
232 | |
233 | wxPipeOutputStream::~wxPipeOutputStream() | |
234 | { | |
235 | ::CloseHandle(m_hOutput); | |
236 | } | |
237 | ||
238 | size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len) | |
239 | { | |
240 | DWORD bytesRead; | |
241 | ||
242 | m_lasterror = wxSTREAM_NOERROR; | |
f6bcfd97 BP |
243 | if ( !::WriteFile(m_hOutput, buffer, len, &bytesRead, NULL) ) |
244 | { | |
245 | if ( ::GetLastError() == ERROR_BROKEN_PIPE ) | |
8b33ae2d GL |
246 | m_lasterror = wxSTREAM_EOF; |
247 | else | |
248 | m_lasterror = wxSTREAM_READ_ERROR; | |
249 | } | |
f6bcfd97 | 250 | |
8b33ae2d GL |
251 | return bytesRead; |
252 | } | |
253 | ||
254 | #endif // __WIN32__ | |
255 | ||
b568d04f VZ |
256 | // ============================================================================ |
257 | // implementation | |
258 | // ============================================================================ | |
5260b1c5 JS |
259 | |
260 | #ifdef __WIN32__ | |
f6bcfd97 | 261 | |
32592631 GL |
262 | static DWORD wxExecuteThread(wxExecuteData *data) |
263 | { | |
cb6827a8 VZ |
264 | WaitForSingleObject(data->hProcess, INFINITE); |
265 | ||
266 | // get the exit code | |
267 | if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) ) | |
268 | { | |
f6bcfd97 | 269 | wxLogLastError(wxT("GetExitCodeProcess")); |
cb6827a8 | 270 | } |
32592631 | 271 | |
cb6827a8 | 272 | wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE, |
223d09f6 | 273 | wxT("process should have terminated") ); |
32592631 | 274 | |
cb6827a8 VZ |
275 | // send a message indicating process termination to the window |
276 | SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data); | |
e6045e08 | 277 | |
cb6827a8 | 278 | return 0; |
32592631 | 279 | } |
5260b1c5 | 280 | |
cb6827a8 VZ |
281 | // window procedure of a hidden window which is created just to receive |
282 | // the notification message when a process exits | |
32592631 GL |
283 | LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message, |
284 | WPARAM wParam, LPARAM lParam) | |
285 | { | |
cb6827a8 VZ |
286 | if ( message == wxWM_PROC_TERMINATED ) |
287 | { | |
288 | DestroyWindow(hWnd); // we don't need it any more | |
289 | ||
290 | wxExecuteData *data = (wxExecuteData *)lParam; | |
291 | if ( data->handler ) | |
292 | { | |
293 | data->handler->OnTerminate((int)data->dwProcessId, | |
294 | (int)data->dwExitCode); | |
295 | } | |
e6045e08 | 296 | |
cb6827a8 VZ |
297 | if ( data->state ) |
298 | { | |
299 | // we're executing synchronously, tell the waiting thread | |
300 | // that the process finished | |
301 | data->state = 0; | |
302 | } | |
303 | else | |
304 | { | |
305 | // asynchronous execution - we should do the clean up | |
306 | delete data; | |
307 | } | |
cb6827a8 | 308 | |
0d7ea902 VZ |
309 | return 0; |
310 | } | |
311 | else | |
312 | { | |
313 | return DefWindowProc(hWnd, message, wParam, lParam); | |
314 | } | |
32592631 | 315 | } |
731dd422 | 316 | #endif // Win32 |
32592631 | 317 | |
5bd3a2da | 318 | long wxExecute(const wxString& cmd, bool sync, wxProcess *handler) |
32592631 | 319 | { |
5bd3a2da VZ |
320 | wxCHECK_MSG( !!cmd, 0, wxT("empty command in wxExecute") ); |
321 | ||
039f62f4 | 322 | wxString command; |
731dd422 | 323 | #if wxUSE_IPC |
5bd3a2da VZ |
324 | // DDE hack: this is really not pretty, but we need to allow this for |
325 | // transparent handling of DDE servers in wxMimeTypesManager. Usually it | |
326 | // returns the command which should be run to view/open/... a file of the | |
327 | // given type. Sometimes, however, this command just launches the server | |
328 | // and an additional DDE request must be made to really open the file. To | |
329 | // keep all this well hidden from the application, we allow a special form | |
330 | // of command: WX_DDE:<command>:DDE_SERVER:DDE_TOPIC:DDE_COMMAND in which | |
331 | // case we execute just <command> and process the rest below | |
039f62f4 | 332 | wxString ddeServer, ddeTopic, ddeCommand; |
5bd3a2da VZ |
333 | static const size_t lenDdePrefix = 7; // strlen("WX_DDE:") |
334 | if ( cmd.Left(lenDdePrefix) == _T("WX_DDE#") ) | |
335 | { | |
336 | const wxChar *p = cmd.c_str() + 7; | |
337 | while ( *p && *p != _T('#') ) | |
338 | { | |
339 | command += *p++; | |
340 | } | |
341 | ||
342 | if ( *p ) | |
343 | { | |
344 | // skip '#' | |
345 | p++; | |
346 | } | |
347 | else | |
348 | { | |
349 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
350 | } | |
351 | ||
352 | while ( *p && *p != _T('#') ) | |
353 | { | |
354 | ddeServer += *p++; | |
355 | } | |
356 | ||
357 | if ( *p ) | |
358 | { | |
359 | // skip '#' | |
360 | p++; | |
361 | } | |
362 | else | |
363 | { | |
364 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
365 | } | |
366 | ||
367 | while ( *p && *p != _T('#') ) | |
368 | { | |
369 | ddeTopic += *p++; | |
370 | } | |
371 | ||
372 | if ( *p ) | |
373 | { | |
374 | // skip '#' | |
375 | p++; | |
376 | } | |
377 | else | |
378 | { | |
379 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
380 | } | |
381 | ||
382 | while ( *p ) | |
383 | { | |
384 | ddeCommand += *p++; | |
385 | } | |
386 | } | |
387 | else | |
731dd422 | 388 | #endif // wxUSE_IPC |
5bd3a2da VZ |
389 | { |
390 | // no DDE | |
391 | command = cmd; | |
392 | } | |
32592631 | 393 | |
57c208c5 | 394 | #if defined(__WIN32__) && !defined(__TWIN32__) |
cb6827a8 | 395 | |
f6bcfd97 BP |
396 | // the IO redirection is only supported with wxUSE_STREAMS |
397 | BOOL redirect = FALSE; | |
398 | #if wxUSE_STREAMS | |
399 | // the first elements are reading ends, the second are the writing ones | |
400 | HANDLE hpipeStdin[2], | |
401 | hpipeStdinWrite = INVALID_HANDLE_VALUE, | |
402 | hpipeStdout[2], | |
403 | hpipeStderr[2]; | |
8b33ae2d | 404 | |
cd6ce4a9 | 405 | // open the pipes to which child process IO will be redirected if needed |
cd6ce4a9 VZ |
406 | if ( handler && handler->IsRedirected() ) |
407 | { | |
f6bcfd97 | 408 | // default secutiry attributes |
8b33ae2d GL |
409 | SECURITY_ATTRIBUTES security; |
410 | ||
411 | security.nLength = sizeof(security); | |
412 | security.lpSecurityDescriptor = NULL; | |
413 | security.bInheritHandle = TRUE; | |
414 | ||
f6bcfd97 BP |
415 | // create stdin pipe |
416 | if ( !::CreatePipe(&hpipeStdin[0], &hpipeStdin[1], &security, 0) ) | |
cd6ce4a9 VZ |
417 | { |
418 | wxLogSysError(_("Can't create the inter-process read pipe")); | |
8b33ae2d | 419 | |
f6bcfd97 BP |
420 | // indicate failure in both cases |
421 | return sync ? -1 : 0; | |
8b33ae2d GL |
422 | } |
423 | ||
f6bcfd97 BP |
424 | // and a stdout one |
425 | if ( !::CreatePipe(&hpipeStdout[0], &hpipeStdout[1], &security, 0) ) | |
cd6ce4a9 | 426 | { |
f6bcfd97 BP |
427 | ::CloseHandle(hpipeStdin[0]); |
428 | ::CloseHandle(hpipeStdin[1]); | |
cd6ce4a9 VZ |
429 | |
430 | wxLogSysError(_("Can't create the inter-process write pipe")); | |
8b33ae2d | 431 | |
f6bcfd97 | 432 | return sync ? -1 : 0; |
8b33ae2d GL |
433 | } |
434 | ||
f6bcfd97 | 435 | (void)::CreatePipe(&hpipeStderr[0], &hpipeStderr[1], &security, 0); |
8b33ae2d | 436 | |
f6bcfd97 | 437 | redirect = TRUE; |
8b33ae2d | 438 | } |
f6bcfd97 | 439 | #endif // wxUSE_STREAMS |
5bd3a2da | 440 | |
cb6827a8 VZ |
441 | // create the process |
442 | STARTUPINFO si; | |
11c7d5b6 | 443 | wxZeroMemory(si); |
cb6827a8 VZ |
444 | si.cb = sizeof(si); |
445 | ||
f6bcfd97 BP |
446 | #if wxUSE_STREAMS |
447 | if ( redirect ) | |
cb6827a8 | 448 | { |
f6bcfd97 BP |
449 | // when the std IO is redirected, we don't show the (console) process |
450 | // window | |
451 | si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; | |
452 | ||
453 | si.hStdInput = hpipeStdin[0]; | |
454 | si.hStdOutput = hpipeStdout[1]; | |
455 | si.hStdError = hpipeStderr[1]; | |
456 | ||
457 | si.wShowWindow = SW_HIDE; | |
458 | ||
459 | // we must duplicate the handle to the write side of stdin pipe to make | |
460 | // it non inheritable: indeed, we must close hpipeStdin[1] before | |
461 | // launching the child process as otherwise this handle will be | |
462 | // inherited by the child which will never close it and so the pipe | |
463 | // will never be closed and the child will be left stuck in ReadFile() | |
464 | if ( !::DuplicateHandle | |
465 | ( | |
466 | GetCurrentProcess(), | |
467 | hpipeStdin[1], | |
468 | GetCurrentProcess(), | |
469 | &hpipeStdinWrite, | |
470 | 0, // desired access: unused here | |
471 | FALSE, // not inherited | |
472 | DUPLICATE_SAME_ACCESS // same access as for src handle | |
473 | ) ) | |
cd6ce4a9 | 474 | { |
f6bcfd97 | 475 | wxLogLastError(_T("DuplicateHandle")); |
8b33ae2d | 476 | } |
cd6ce4a9 | 477 | |
f6bcfd97 BP |
478 | ::CloseHandle(hpipeStdin[1]); |
479 | } | |
480 | #endif // wxUSE_STREAMS | |
cb6827a8 | 481 | |
f6bcfd97 BP |
482 | PROCESS_INFORMATION pi; |
483 | DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED; | |
484 | ||
485 | bool ok = ::CreateProcess | |
486 | ( | |
487 | NULL, // application name (use only cmd line) | |
488 | (wxChar *) | |
489 | command.c_str(), // full command line | |
490 | NULL, // security attributes: defaults for both | |
491 | NULL, // the process and its main thread | |
492 | redirect, // inherit handles if we use pipes | |
493 | dwFlags, // process creation flags | |
494 | NULL, // environment (use the same) | |
495 | NULL, // current directory (use the same) | |
496 | &si, // startup info (unused here) | |
497 | &pi // process info | |
498 | ) != 0; | |
499 | ||
500 | #if wxUSE_STREAMS | |
501 | // we can close the pipe ends used by child anyhow | |
502 | if ( redirect ) | |
503 | { | |
504 | ::CloseHandle(hpipeStdin[0]); | |
505 | ::CloseHandle(hpipeStdout[1]); | |
506 | ::CloseHandle(hpipeStderr[1]); | |
cb6827a8 | 507 | } |
f6bcfd97 | 508 | #endif // wxUSE_STREAMS |
cb6827a8 | 509 | |
f6bcfd97 | 510 | if ( !ok ) |
5e1febfa | 511 | { |
f6bcfd97 BP |
512 | #if wxUSE_STREAMS |
513 | // close the other handles too | |
514 | if ( redirect ) | |
5e1febfa | 515 | { |
f6bcfd97 BP |
516 | ::CloseHandle(hpipeStdout[0]); |
517 | ::CloseHandle(hpipeStderr[0]); | |
5e1febfa | 518 | } |
f6bcfd97 | 519 | #endif // wxUSE_STREAMS |
5e1febfa | 520 | |
f6bcfd97 BP |
521 | wxLogSysError(_("Execution of command '%s' failed"), command.c_str()); |
522 | ||
523 | return sync ? -1 : 0; | |
524 | } | |
8b33ae2d | 525 | |
f6bcfd97 BP |
526 | #if wxUSE_STREAMS |
527 | if ( redirect ) | |
528 | { | |
8b33ae2d | 529 | // We can now initialize the wxStreams |
f6bcfd97 BP |
530 | wxInputStream *inStream = new wxPipeInputStream(hpipeStdout[0]), |
531 | *errStream = new wxPipeInputStream(hpipeStderr[0]); | |
532 | wxOutputStream *outStream = new wxPipeOutputStream(hpipeStdinWrite); | |
8b33ae2d | 533 | |
f6bcfd97 | 534 | handler->SetPipeStreams(inStream, outStream, errStream); |
8b33ae2d | 535 | } |
f6bcfd97 | 536 | #endif // wxUSE_STREAMS |
8b33ae2d | 537 | |
0d7ea902 | 538 | // register the class for the hidden window used for the notifications |
b568d04f VZ |
539 | if ( !gs_classForHiddenWindow ) |
540 | { | |
541 | gs_classForHiddenWindow = _T("wxHiddenWindow"); | |
542 | ||
543 | WNDCLASS wndclass; | |
544 | wxZeroMemory(wndclass); | |
545 | wndclass.lpfnWndProc = (WNDPROC)wxExecuteWindowCbk; | |
546 | wndclass.hInstance = wxGetInstance(); | |
547 | wndclass.lpszClassName = gs_classForHiddenWindow; | |
548 | ||
549 | if ( !::RegisterClass(&wndclass) ) | |
550 | { | |
f6bcfd97 | 551 | wxLogLastError(wxT("RegisterClass(hidden window)")); |
b568d04f VZ |
552 | } |
553 | } | |
554 | ||
cb6827a8 VZ |
555 | // create a hidden window to receive notification about process |
556 | // termination | |
b568d04f | 557 | HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL, |
0d7ea902 VZ |
558 | WS_OVERLAPPEDWINDOW, |
559 | 0, 0, 0, 0, NULL, | |
cb6827a8 | 560 | (HMENU)NULL, wxGetInstance(), 0); |
223d09f6 | 561 | wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") ); |
e6045e08 | 562 | |
cb6827a8 VZ |
563 | // Alloc data |
564 | wxExecuteData *data = new wxExecuteData; | |
565 | data->hProcess = pi.hProcess; | |
566 | data->dwProcessId = pi.dwProcessId; | |
567 | data->hWnd = hwnd; | |
568 | data->state = sync; | |
e6045e08 VZ |
569 | if ( sync ) |
570 | { | |
5e1febfa VZ |
571 | // handler may be !NULL for capturing program output, but we don't use |
572 | // it wxExecuteData struct in this case | |
e6045e08 VZ |
573 | data->handler = NULL; |
574 | } | |
575 | else | |
576 | { | |
577 | // may be NULL or not | |
578 | data->handler = handler; | |
579 | } | |
cb6827a8 VZ |
580 | |
581 | DWORD tid; | |
582 | HANDLE hThread = ::CreateThread(NULL, | |
583 | 0, | |
584 | (LPTHREAD_START_ROUTINE)wxExecuteThread, | |
585 | (void *)data, | |
586 | 0, | |
587 | &tid); | |
588 | ||
0d7ea902 VZ |
589 | // resume process we created now - whether the thread creation succeeded or |
590 | // not | |
591 | if ( ::ResumeThread(pi.hThread) == (DWORD)-1 ) | |
592 | { | |
593 | // ignore it - what can we do? | |
f6bcfd97 | 594 | wxLogLastError(wxT("ResumeThread in wxExecute")); |
0d7ea902 VZ |
595 | } |
596 | ||
597 | // close unneeded handle | |
598 | if ( !::CloseHandle(pi.hThread) ) | |
f6bcfd97 | 599 | wxLogLastError(wxT("CloseHandle(hThread)")); |
0d7ea902 | 600 | |
cb6827a8 VZ |
601 | if ( !hThread ) |
602 | { | |
f6bcfd97 | 603 | wxLogLastError(wxT("CreateThread in wxExecute")); |
cb6827a8 VZ |
604 | |
605 | DestroyWindow(hwnd); | |
606 | delete data; | |
607 | ||
608 | // the process still started up successfully... | |
609 | return pi.dwProcessId; | |
610 | } | |
e6045e08 | 611 | |
f6bcfd97 BP |
612 | ::CloseHandle(hThread); |
613 | ||
731dd422 | 614 | #if wxUSE_IPC |
5bd3a2da VZ |
615 | // second part of DDE hack: now establish the DDE conversation with the |
616 | // just launched process | |
617 | if ( !!ddeServer ) | |
618 | { | |
619 | wxDDEClient client; | |
620 | wxConnectionBase *conn = client.MakeConnection(_T(""), | |
621 | ddeServer, | |
622 | ddeTopic); | |
623 | if ( !conn || !conn->Execute(ddeCommand) ) | |
624 | { | |
625 | wxLogError(_("Couldn't launch DDE server '%s'."), command.c_str()); | |
626 | } | |
627 | } | |
731dd422 | 628 | #endif // wxUSE_IPC |
5bd3a2da | 629 | |
cb6827a8 VZ |
630 | if ( !sync ) |
631 | { | |
632 | // clean up will be done when the process terminates | |
e6045e08 VZ |
633 | |
634 | // return the pid | |
cb6827a8 VZ |
635 | return pi.dwProcessId; |
636 | } | |
e6045e08 | 637 | |
0d7ea902 VZ |
638 | // waiting until command executed (disable everything while doing it) |
639 | #if wxUSE_GUI | |
cd6ce4a9 VZ |
640 | { |
641 | wxBusyCursor bc; | |
642 | ||
643 | wxWindowDisabler wd; | |
0d7ea902 VZ |
644 | #endif // wxUSE_GUI |
645 | ||
cb6827a8 VZ |
646 | while ( data->state ) |
647 | wxYield(); | |
e6045e08 | 648 | |
0d7ea902 | 649 | #if wxUSE_GUI |
cd6ce4a9 | 650 | } |
0d7ea902 VZ |
651 | #endif // wxUSE_GUI |
652 | ||
e6045e08 | 653 | DWORD dwExitCode = data->dwExitCode; |
cb6827a8 VZ |
654 | delete data; |
655 | ||
e6045e08 VZ |
656 | // return the exit code |
657 | return dwExitCode; | |
cb6827a8 VZ |
658 | #else // Win16 |
659 | long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW); | |
660 | if (instanceID < 32) return(0); | |
e6045e08 | 661 | |
cb6827a8 VZ |
662 | if (sync) { |
663 | int running; | |
664 | do { | |
665 | wxYield(); | |
27a9bd48 | 666 | running = GetModuleUsage((HINSTANCE)instanceID); |
cb6827a8 VZ |
667 | } while (running); |
668 | } | |
669 | ||
670 | return(instanceID); | |
671 | #endif // Win16/32 | |
32592631 | 672 | } |
c740f496 GL |
673 | |
674 | long wxExecute(char **argv, bool sync, wxProcess *handler) | |
675 | { | |
cb6827a8 | 676 | wxString command; |
e6045e08 | 677 | |
cb6827a8 VZ |
678 | while ( *argv != NULL ) |
679 | { | |
680 | command << *argv++ << ' '; | |
681 | } | |
682 | ||
683 | command.RemoveLast(); | |
684 | ||
685 | return wxExecute(command, sync, handler); | |
c740f496 | 686 | } |
006162a9 | 687 | |
f6bcfd97 BP |
688 | #if wxUSE_GUI |
689 | ||
d9317fd4 VZ |
690 | // ---------------------------------------------------------------------------- |
691 | // Metafile helpers | |
692 | // ---------------------------------------------------------------------------- | |
693 | ||
694 | extern void PixelToHIMETRIC(LONG *x, LONG *y) | |
695 | { | |
696 | ScreenHDC hdcRef; | |
697 | ||
698 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
699 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
700 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
701 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
702 | ||
703 | *x *= (iWidthMM * 100); | |
704 | *x /= iWidthPels; | |
705 | *y *= (iHeightMM * 100); | |
706 | *y /= iHeightPels; | |
707 | } | |
708 | ||
709 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
710 | { | |
711 | ScreenHDC hdcRef; | |
712 | ||
713 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
714 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
715 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
716 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
717 | ||
718 | *x *= iWidthPels; | |
719 | *x /= (iWidthMM * 100); | |
720 | *y *= iHeightPels; | |
721 | *y /= (iHeightMM * 100); | |
722 | } | |
f6bcfd97 BP |
723 | |
724 | #endif // wxUSE_GUI |