]>
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; |
6ba63600 | 323 | |
731dd422 | 324 | #if wxUSE_IPC |
5bd3a2da VZ |
325 | // DDE hack: this is really not pretty, but we need to allow this for |
326 | // transparent handling of DDE servers in wxMimeTypesManager. Usually it | |
327 | // returns the command which should be run to view/open/... a file of the | |
328 | // given type. Sometimes, however, this command just launches the server | |
329 | // and an additional DDE request must be made to really open the file. To | |
330 | // keep all this well hidden from the application, we allow a special form | |
6ba63600 | 331 | // of command: WX_DDE#<command>#DDE_SERVER#DDE_TOPIC#DDE_COMMAND in which |
5bd3a2da | 332 | // case we execute just <command> and process the rest below |
039f62f4 | 333 | wxString ddeServer, ddeTopic, ddeCommand; |
5bd3a2da VZ |
334 | static const size_t lenDdePrefix = 7; // strlen("WX_DDE:") |
335 | if ( cmd.Left(lenDdePrefix) == _T("WX_DDE#") ) | |
336 | { | |
337 | const wxChar *p = cmd.c_str() + 7; | |
338 | while ( *p && *p != _T('#') ) | |
339 | { | |
340 | command += *p++; | |
341 | } | |
342 | ||
343 | if ( *p ) | |
344 | { | |
345 | // skip '#' | |
346 | p++; | |
347 | } | |
348 | else | |
349 | { | |
350 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
351 | } | |
352 | ||
353 | while ( *p && *p != _T('#') ) | |
354 | { | |
355 | ddeServer += *p++; | |
356 | } | |
357 | ||
358 | if ( *p ) | |
359 | { | |
360 | // skip '#' | |
361 | p++; | |
362 | } | |
363 | else | |
364 | { | |
365 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
366 | } | |
367 | ||
368 | while ( *p && *p != _T('#') ) | |
369 | { | |
370 | ddeTopic += *p++; | |
371 | } | |
372 | ||
373 | if ( *p ) | |
374 | { | |
375 | // skip '#' | |
376 | p++; | |
377 | } | |
378 | else | |
379 | { | |
380 | wxFAIL_MSG(_T("invalid WX_DDE command in wxExecute")); | |
381 | } | |
382 | ||
383 | while ( *p ) | |
384 | { | |
385 | ddeCommand += *p++; | |
386 | } | |
6ba63600 VZ |
387 | |
388 | // maybe we don't have to launch the DDE server at all - if it is | |
389 | // already running, for example | |
390 | wxDDEClient client; | |
391 | wxLogNull nolog; | |
392 | wxConnectionBase *conn = client.MakeConnection(_T(""), | |
393 | ddeServer, | |
394 | ddeTopic); | |
395 | if ( conn ) | |
396 | { | |
397 | // FIXME we don't check the return code as for some strange reason | |
398 | // it will sometimes be FALSE - it is probably a bug in our | |
399 | // DDE code but I don't see anything wrong there | |
400 | (void)conn->Execute(ddeCommand); | |
401 | ||
402 | // ok, the command executed - return value indicating success, | |
403 | // making it up for async case as we really don't have any way to | |
404 | // get the real PID of the DDE server here | |
405 | return sync ? 0 : -1; | |
406 | } | |
407 | //else: couldn't establish DDE conversation, now try launching the app | |
408 | // and sending the DDE request again | |
5bd3a2da VZ |
409 | } |
410 | else | |
731dd422 | 411 | #endif // wxUSE_IPC |
5bd3a2da VZ |
412 | { |
413 | // no DDE | |
414 | command = cmd; | |
415 | } | |
32592631 | 416 | |
57c208c5 | 417 | #if defined(__WIN32__) && !defined(__TWIN32__) |
cb6827a8 | 418 | |
f6bcfd97 BP |
419 | // the IO redirection is only supported with wxUSE_STREAMS |
420 | BOOL redirect = FALSE; | |
421 | #if wxUSE_STREAMS | |
422 | // the first elements are reading ends, the second are the writing ones | |
423 | HANDLE hpipeStdin[2], | |
f6bcfd97 BP |
424 | hpipeStdout[2], |
425 | hpipeStderr[2]; | |
33ac7e6f | 426 | HANDLE hpipeStdinWrite = INVALID_HANDLE_VALUE; |
8b33ae2d | 427 | |
cd6ce4a9 | 428 | // open the pipes to which child process IO will be redirected if needed |
cd6ce4a9 VZ |
429 | if ( handler && handler->IsRedirected() ) |
430 | { | |
f6bcfd97 | 431 | // default secutiry attributes |
8b33ae2d GL |
432 | SECURITY_ATTRIBUTES security; |
433 | ||
434 | security.nLength = sizeof(security); | |
435 | security.lpSecurityDescriptor = NULL; | |
436 | security.bInheritHandle = TRUE; | |
437 | ||
f6bcfd97 BP |
438 | // create stdin pipe |
439 | if ( !::CreatePipe(&hpipeStdin[0], &hpipeStdin[1], &security, 0) ) | |
cd6ce4a9 VZ |
440 | { |
441 | wxLogSysError(_("Can't create the inter-process read pipe")); | |
8b33ae2d | 442 | |
f6bcfd97 BP |
443 | // indicate failure in both cases |
444 | return sync ? -1 : 0; | |
8b33ae2d GL |
445 | } |
446 | ||
f6bcfd97 BP |
447 | // and a stdout one |
448 | if ( !::CreatePipe(&hpipeStdout[0], &hpipeStdout[1], &security, 0) ) | |
cd6ce4a9 | 449 | { |
f6bcfd97 BP |
450 | ::CloseHandle(hpipeStdin[0]); |
451 | ::CloseHandle(hpipeStdin[1]); | |
cd6ce4a9 VZ |
452 | |
453 | wxLogSysError(_("Can't create the inter-process write pipe")); | |
8b33ae2d | 454 | |
f6bcfd97 | 455 | return sync ? -1 : 0; |
8b33ae2d GL |
456 | } |
457 | ||
f6bcfd97 | 458 | (void)::CreatePipe(&hpipeStderr[0], &hpipeStderr[1], &security, 0); |
8b33ae2d | 459 | |
f6bcfd97 | 460 | redirect = TRUE; |
8b33ae2d | 461 | } |
f6bcfd97 | 462 | #endif // wxUSE_STREAMS |
5bd3a2da | 463 | |
cb6827a8 VZ |
464 | // create the process |
465 | STARTUPINFO si; | |
11c7d5b6 | 466 | wxZeroMemory(si); |
cb6827a8 VZ |
467 | si.cb = sizeof(si); |
468 | ||
f6bcfd97 BP |
469 | #if wxUSE_STREAMS |
470 | if ( redirect ) | |
cb6827a8 | 471 | { |
f6bcfd97 BP |
472 | // when the std IO is redirected, we don't show the (console) process |
473 | // window | |
474 | si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; | |
475 | ||
476 | si.hStdInput = hpipeStdin[0]; | |
477 | si.hStdOutput = hpipeStdout[1]; | |
478 | si.hStdError = hpipeStderr[1]; | |
479 | ||
480 | si.wShowWindow = SW_HIDE; | |
481 | ||
482 | // we must duplicate the handle to the write side of stdin pipe to make | |
483 | // it non inheritable: indeed, we must close hpipeStdin[1] before | |
484 | // launching the child process as otherwise this handle will be | |
485 | // inherited by the child which will never close it and so the pipe | |
486 | // will never be closed and the child will be left stuck in ReadFile() | |
487 | if ( !::DuplicateHandle | |
488 | ( | |
489 | GetCurrentProcess(), | |
490 | hpipeStdin[1], | |
491 | GetCurrentProcess(), | |
492 | &hpipeStdinWrite, | |
493 | 0, // desired access: unused here | |
494 | FALSE, // not inherited | |
495 | DUPLICATE_SAME_ACCESS // same access as for src handle | |
496 | ) ) | |
cd6ce4a9 | 497 | { |
f6bcfd97 | 498 | wxLogLastError(_T("DuplicateHandle")); |
8b33ae2d | 499 | } |
cd6ce4a9 | 500 | |
f6bcfd97 BP |
501 | ::CloseHandle(hpipeStdin[1]); |
502 | } | |
503 | #endif // wxUSE_STREAMS | |
cb6827a8 | 504 | |
f6bcfd97 BP |
505 | PROCESS_INFORMATION pi; |
506 | DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED; | |
507 | ||
508 | bool ok = ::CreateProcess | |
509 | ( | |
510 | NULL, // application name (use only cmd line) | |
511 | (wxChar *) | |
512 | command.c_str(), // full command line | |
513 | NULL, // security attributes: defaults for both | |
514 | NULL, // the process and its main thread | |
515 | redirect, // inherit handles if we use pipes | |
516 | dwFlags, // process creation flags | |
517 | NULL, // environment (use the same) | |
518 | NULL, // current directory (use the same) | |
519 | &si, // startup info (unused here) | |
520 | &pi // process info | |
521 | ) != 0; | |
522 | ||
523 | #if wxUSE_STREAMS | |
524 | // we can close the pipe ends used by child anyhow | |
525 | if ( redirect ) | |
526 | { | |
527 | ::CloseHandle(hpipeStdin[0]); | |
528 | ::CloseHandle(hpipeStdout[1]); | |
529 | ::CloseHandle(hpipeStderr[1]); | |
cb6827a8 | 530 | } |
f6bcfd97 | 531 | #endif // wxUSE_STREAMS |
cb6827a8 | 532 | |
f6bcfd97 | 533 | if ( !ok ) |
5e1febfa | 534 | { |
f6bcfd97 BP |
535 | #if wxUSE_STREAMS |
536 | // close the other handles too | |
537 | if ( redirect ) | |
5e1febfa | 538 | { |
f6bcfd97 BP |
539 | ::CloseHandle(hpipeStdout[0]); |
540 | ::CloseHandle(hpipeStderr[0]); | |
5e1febfa | 541 | } |
f6bcfd97 | 542 | #endif // wxUSE_STREAMS |
5e1febfa | 543 | |
f6bcfd97 BP |
544 | wxLogSysError(_("Execution of command '%s' failed"), command.c_str()); |
545 | ||
546 | return sync ? -1 : 0; | |
547 | } | |
8b33ae2d | 548 | |
f6bcfd97 BP |
549 | #if wxUSE_STREAMS |
550 | if ( redirect ) | |
551 | { | |
8b33ae2d | 552 | // We can now initialize the wxStreams |
f6bcfd97 BP |
553 | wxInputStream *inStream = new wxPipeInputStream(hpipeStdout[0]), |
554 | *errStream = new wxPipeInputStream(hpipeStderr[0]); | |
555 | wxOutputStream *outStream = new wxPipeOutputStream(hpipeStdinWrite); | |
8b33ae2d | 556 | |
f6bcfd97 | 557 | handler->SetPipeStreams(inStream, outStream, errStream); |
8b33ae2d | 558 | } |
f6bcfd97 | 559 | #endif // wxUSE_STREAMS |
8b33ae2d | 560 | |
0d7ea902 | 561 | // register the class for the hidden window used for the notifications |
b568d04f VZ |
562 | if ( !gs_classForHiddenWindow ) |
563 | { | |
564 | gs_classForHiddenWindow = _T("wxHiddenWindow"); | |
565 | ||
566 | WNDCLASS wndclass; | |
567 | wxZeroMemory(wndclass); | |
568 | wndclass.lpfnWndProc = (WNDPROC)wxExecuteWindowCbk; | |
569 | wndclass.hInstance = wxGetInstance(); | |
570 | wndclass.lpszClassName = gs_classForHiddenWindow; | |
571 | ||
572 | if ( !::RegisterClass(&wndclass) ) | |
573 | { | |
f6bcfd97 | 574 | wxLogLastError(wxT("RegisterClass(hidden window)")); |
b568d04f VZ |
575 | } |
576 | } | |
577 | ||
cb6827a8 VZ |
578 | // create a hidden window to receive notification about process |
579 | // termination | |
b568d04f | 580 | HWND hwnd = ::CreateWindow(gs_classForHiddenWindow, NULL, |
0d7ea902 VZ |
581 | WS_OVERLAPPEDWINDOW, |
582 | 0, 0, 0, 0, NULL, | |
cb6827a8 | 583 | (HMENU)NULL, wxGetInstance(), 0); |
223d09f6 | 584 | wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") ); |
e6045e08 | 585 | |
cb6827a8 VZ |
586 | // Alloc data |
587 | wxExecuteData *data = new wxExecuteData; | |
588 | data->hProcess = pi.hProcess; | |
589 | data->dwProcessId = pi.dwProcessId; | |
590 | data->hWnd = hwnd; | |
591 | data->state = sync; | |
e6045e08 VZ |
592 | if ( sync ) |
593 | { | |
5e1febfa VZ |
594 | // handler may be !NULL for capturing program output, but we don't use |
595 | // it wxExecuteData struct in this case | |
e6045e08 VZ |
596 | data->handler = NULL; |
597 | } | |
598 | else | |
599 | { | |
600 | // may be NULL or not | |
601 | data->handler = handler; | |
602 | } | |
cb6827a8 VZ |
603 | |
604 | DWORD tid; | |
605 | HANDLE hThread = ::CreateThread(NULL, | |
606 | 0, | |
607 | (LPTHREAD_START_ROUTINE)wxExecuteThread, | |
608 | (void *)data, | |
609 | 0, | |
610 | &tid); | |
611 | ||
0d7ea902 VZ |
612 | // resume process we created now - whether the thread creation succeeded or |
613 | // not | |
614 | if ( ::ResumeThread(pi.hThread) == (DWORD)-1 ) | |
615 | { | |
616 | // ignore it - what can we do? | |
f6bcfd97 | 617 | wxLogLastError(wxT("ResumeThread in wxExecute")); |
0d7ea902 VZ |
618 | } |
619 | ||
620 | // close unneeded handle | |
621 | if ( !::CloseHandle(pi.hThread) ) | |
f6bcfd97 | 622 | wxLogLastError(wxT("CloseHandle(hThread)")); |
0d7ea902 | 623 | |
cb6827a8 VZ |
624 | if ( !hThread ) |
625 | { | |
f6bcfd97 | 626 | wxLogLastError(wxT("CreateThread in wxExecute")); |
cb6827a8 VZ |
627 | |
628 | DestroyWindow(hwnd); | |
629 | delete data; | |
630 | ||
631 | // the process still started up successfully... | |
632 | return pi.dwProcessId; | |
633 | } | |
e6045e08 | 634 | |
f6bcfd97 BP |
635 | ::CloseHandle(hThread); |
636 | ||
731dd422 | 637 | #if wxUSE_IPC |
5bd3a2da VZ |
638 | // second part of DDE hack: now establish the DDE conversation with the |
639 | // just launched process | |
640 | if ( !!ddeServer ) | |
641 | { | |
642 | wxDDEClient client; | |
6ba63600 VZ |
643 | wxConnectionBase *conn; |
644 | ||
645 | { | |
646 | // try doing it the first time without error messages | |
647 | wxLogNull nolog; | |
648 | ||
649 | conn = client.MakeConnection(_T(""), ddeServer, ddeTopic); | |
650 | } | |
651 | ||
652 | if ( !conn ) | |
653 | { | |
654 | // give the app some time to initialize itself: in fact, a common | |
655 | // reason for failure is that we tried to open DDE conversation too | |
656 | // soon (before the app had time to setup its DDE server), so wait | |
657 | // a bit and try again | |
658 | ::Sleep(2000); | |
659 | ||
660 | wxConnectionBase *conn = client.MakeConnection(_T(""), | |
661 | ddeServer, | |
662 | ddeTopic); | |
663 | if ( !conn ) | |
664 | { | |
665 | wxLogError(_("Couldn't launch DDE server '%s'."), command.c_str()); | |
666 | } | |
667 | } | |
668 | ||
669 | if ( conn ) | |
5bd3a2da | 670 | { |
6ba63600 VZ |
671 | // FIXME just as above we don't check Execute() return code |
672 | wxLogNull nolog; | |
673 | (void)conn->Execute(ddeCommand); | |
5bd3a2da VZ |
674 | } |
675 | } | |
731dd422 | 676 | #endif // wxUSE_IPC |
5bd3a2da | 677 | |
cb6827a8 VZ |
678 | if ( !sync ) |
679 | { | |
680 | // clean up will be done when the process terminates | |
e6045e08 VZ |
681 | |
682 | // return the pid | |
cb6827a8 VZ |
683 | return pi.dwProcessId; |
684 | } | |
e6045e08 | 685 | |
0d7ea902 VZ |
686 | // waiting until command executed (disable everything while doing it) |
687 | #if wxUSE_GUI | |
cd6ce4a9 VZ |
688 | { |
689 | wxBusyCursor bc; | |
690 | ||
691 | wxWindowDisabler wd; | |
0d7ea902 VZ |
692 | #endif // wxUSE_GUI |
693 | ||
cb6827a8 VZ |
694 | while ( data->state ) |
695 | wxYield(); | |
e6045e08 | 696 | |
0d7ea902 | 697 | #if wxUSE_GUI |
cd6ce4a9 | 698 | } |
0d7ea902 VZ |
699 | #endif // wxUSE_GUI |
700 | ||
e6045e08 | 701 | DWORD dwExitCode = data->dwExitCode; |
cb6827a8 VZ |
702 | delete data; |
703 | ||
e6045e08 VZ |
704 | // return the exit code |
705 | return dwExitCode; | |
cb6827a8 VZ |
706 | #else // Win16 |
707 | long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW); | |
708 | if (instanceID < 32) return(0); | |
e6045e08 | 709 | |
cb6827a8 VZ |
710 | if (sync) { |
711 | int running; | |
712 | do { | |
713 | wxYield(); | |
27a9bd48 | 714 | running = GetModuleUsage((HINSTANCE)instanceID); |
cb6827a8 VZ |
715 | } while (running); |
716 | } | |
717 | ||
718 | return(instanceID); | |
719 | #endif // Win16/32 | |
32592631 | 720 | } |
c740f496 GL |
721 | |
722 | long wxExecute(char **argv, bool sync, wxProcess *handler) | |
723 | { | |
cb6827a8 | 724 | wxString command; |
e6045e08 | 725 | |
cb6827a8 VZ |
726 | while ( *argv != NULL ) |
727 | { | |
728 | command << *argv++ << ' '; | |
729 | } | |
730 | ||
731 | command.RemoveLast(); | |
732 | ||
733 | return wxExecute(command, sync, handler); | |
c740f496 | 734 | } |
006162a9 | 735 | |
f6bcfd97 BP |
736 | #if wxUSE_GUI |
737 | ||
d9317fd4 VZ |
738 | // ---------------------------------------------------------------------------- |
739 | // Metafile helpers | |
740 | // ---------------------------------------------------------------------------- | |
741 | ||
742 | extern void PixelToHIMETRIC(LONG *x, LONG *y) | |
743 | { | |
744 | ScreenHDC hdcRef; | |
745 | ||
746 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
747 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
748 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
749 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
750 | ||
751 | *x *= (iWidthMM * 100); | |
752 | *x /= iWidthPels; | |
753 | *y *= (iHeightMM * 100); | |
754 | *y /= iHeightPels; | |
755 | } | |
756 | ||
757 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
758 | { | |
759 | ScreenHDC hdcRef; | |
760 | ||
761 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
762 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
763 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
764 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
765 | ||
766 | *x *= iWidthPels; | |
767 | *x /= (iWidthMM * 100); | |
768 | *y *= iHeightPels; | |
769 | *y /= (iHeightMM * 100); | |
770 | } | |
f6bcfd97 BP |
771 | |
772 | #endif // wxUSE_GUI |