]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/utilsexc.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/process.h"
26 #include "wx/os2/private.h"
43 // this message is sent when the process we're waiting for terminates
44 #define wxWM_PROC_TERMINATED (WM_USER + 10000)
46 // structure describing the process we're being waiting for
54 if ( !::CloseHandle(hProcess) )
56 wxLogLastError("CloseHandle(hProcess)");
61 HWND hWnd
; // window to send wxWM_PROC_TERMINATED to
62 HANDLE hProcess
; // handle of the process
63 DWORD dwProcessId
; // pid of the process
65 DWORD dwExitCode
; // the exit code of the process
66 bool state
; // set to FALSE when the process finishes
70 static DWORD
wxExecuteThread(wxExecuteData
*data
)
74 WaitForSingleObject(data->hProcess, INFINITE);
77 if ( !GetExitCodeProcess(data->hProcess, &data->dwExitCode) )
79 wxLogLastError("GetExitCodeProcess");
82 wxASSERT_MSG( data->dwExitCode != STILL_ACTIVE,
83 wxT("process should have terminated") );
85 // send a message indicating process termination to the window
86 SendMessage(data->hWnd, wxWM_PROC_TERMINATED, 0, (LPARAM)data);
91 // window procedure of a hidden window which is created just to receive
92 // the notification message when a process exits
93 MRESULT APIENTRY
wxExecuteWindowCbk(HWND hWnd
, UINT message
,
94 MPARAM wParam
, MPARAM lParam
)
96 if ( message
== wxWM_PROC_TERMINATED
)
98 // DestroyWindow(hWnd); // we don't need it any more
100 wxExecuteData
*data
= (wxExecuteData
*)lParam
;
103 data
->handler
->OnTerminate((int)data
->dwProcessId
,
104 (int)data
->dwExitCode
);
109 // we're executing synchronously, tell the waiting thread
110 // that the process finished
115 // asynchronous execution - we should do the clean up
123 extern wxChar wxPanelClassName
[];
125 long wxExecute(const wxString
& command
, bool sync
, wxProcess
*handler
)
127 wxCHECK_MSG( !!command
, 0, wxT("empty command in wxExecute") );
129 // the old code is disabled because we really need a process handle
130 // if we want to execute it asynchronously or even just get its
131 // return code and for this we must use CreateProcess() and not
134 // create the process
138 memset(&si, 0, sizeof(si));
141 PROCESS_INFORMATION pi;
143 if ( ::CreateProcess(
144 NULL, // application name (use only cmd line)
145 (wxChar *)command.c_str(), // full command line
146 NULL, // security attributes: defaults for both
147 NULL, // the process and its main thread
148 FALSE, // don't inherit handles
149 CREATE_DEFAULT_ERROR_MODE, // flags
150 NULL, // environment (use the same)
151 NULL, // current directory (use the same)
152 &si, // startup info (unused here)
156 wxLogSysError(_("Execution of command '%s' failed"), command.c_str());
161 // close unneeded handle
162 if ( !::CloseHandle(pi.hThread) )
163 wxLogLastError("CloseHandle(hThread)");
165 // create a hidden window to receive notification about process
167 HWND hwnd = ::CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
168 (HMENU)NULL, wxGetInstance(), 0);
169 wxASSERT_MSG( hwnd, wxT("can't create a hidden window for wxExecute") );
171 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC)wxExecuteWindowCbk,
174 ::SetWindowLong(hwnd, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
177 wxExecuteData *data = new wxExecuteData;
178 data->hProcess = pi.hProcess;
179 data->dwProcessId = pi.dwProcessId;
184 wxASSERT_MSG( !handler, wxT("wxProcess param ignored for sync execution") );
186 data->handler = NULL;
190 // may be NULL or not
191 data->handler = handler;
195 HANDLE hThread = ::CreateThread(NULL,
197 (LPTHREAD_START_ROUTINE)wxExecuteThread,
204 wxLogLastError("CreateThread in wxExecute");
209 // the process still started up successfully...
210 return pi.dwProcessId;
215 // clean up will be done when the process terminates
218 return pi.dwProcessId;
221 // waiting until command executed
222 while ( data->state )
225 DWORD dwExitCode = data->dwExitCode;
228 // return the exit code
234 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
238 while ( *argv
!= NULL
)
240 command
<< *argv
++ << ' ';
243 command
.RemoveLast();
245 return wxExecute(command
, sync
, handler
);
248 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
250 DWORD nSize
= maxSize
;
253 if ( !::GetComputerName(buf, &nSize) )
255 wxLogLastError("GetComputerName");