1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
30 #include "wx/msw/private.h"
41 #include <sys/unistd.h>
61 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
67 #define wxEXECUTE_WIN_MESSAGE 10000
69 struct wxExecuteData
{
78 static DWORD
wxExecuteThread(wxExecuteData
*data
)
80 WaitForSingleObject(data
->process
, INFINITE
);
82 // Send an implicit message to the window
83 SendMessage(data
->window
, wxEXECUTE_WIN_MESSAGE
, 0, (LPARAM
)data
);
90 LRESULT APIENTRY _EXPORT
wxExecuteWindowCbk(HWND hWnd
, UINT message
,
91 WPARAM wParam
, LPARAM lParam
)
93 wxExecuteData
*data
= (wxExecuteData
*)lParam
;
95 if (message
== wxEXECUTE_WIN_MESSAGE
) {
98 data
->handler
->OnTerminate((int)data
->process
);
108 extern char wxPanelClassName
[];
110 long wxExecute(const wxString
& command
, bool sync
, wxProcess
*handler
)
125 // copy the command line
126 clen
= command
.Length();
127 if (!clen
) return -1;
128 cl
= (char *) calloc( 1, 256);
130 strcpy( cl
, WXSTRINGCAST command
);
132 // isolate command and arguments
133 argp
= strchr( cl
, ' ');
138 result
= ShellExecute((HWND
) (wxTheApp
->GetTopWindow() ? (HWND
) wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
139 (const wchar_t) "open", (const wchar_t) cl
, (const wchar_t) argp
,
140 (const wchar_t) NULL
, SW_SHOWNORMAL
);
142 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
143 "open", cl
, argp
, NULL
, SW_SHOWNORMAL
);
146 if (((long)result
) <= 32) {
149 wxLogSysError(_("Can't execute command '%s'"), command
.c_str());
154 data
= new wxExecuteData
;
157 window
= CreateWindow(wxPanelClassName
, NULL
, 0, 0, 0, 0, 0, NULL
,
158 (HMENU
) NULL
, wxGetInstance(), 0);
160 FARPROC ExecuteWindowInstance
= MakeProcInstance((FARPROC
) wxExecuteWindowCbk
,
163 SetWindowLong(window
, GWL_WNDPROC
, (LONG
) ExecuteWindowInstance
);
164 SetWindowLong(window
, GWL_USERDATA
, (LONG
) data
);
166 data
->process
= result
;
167 data
->window
= window
;
169 data
->handler
= (sync
) ? NULL
: handler
;
171 dresult
= (DWORD
)CreateThread(NULL
, 0,
172 (LPTHREAD_START_ROUTINE
)wxExecuteThread
,
173 (void *)data
, 0, &tid
);
175 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
176 DestroyWindow(window
);
186 // waiting until command executed
193 long instanceID
= WinExec((LPCSTR
) WXSTRINGCAST command
, SW_SHOW
);
194 if (instanceID
< 32) return(0);
200 running
= GetModuleUsage((HANDLE
)instanceID
);
207 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
209 wxString command
= "";
211 while (*argv
!= NULL
) {
216 command
.RemoveLast();
217 return wxExecute(command
, sync
, handler
);