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"
43 #include <sys/unistd.h>
59 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
65 #define wxEXECUTE_WIN_MESSAGE 10000
67 struct wxExecuteData
{
76 static DWORD
wxExecuteThread(wxExecuteData
*data
)
78 WaitForSingleObject(data
->process
, INFINITE
);
80 // Send an implicit message to the window
81 SendMessage(data
->window
, wxEXECUTE_WIN_MESSAGE
, 0, (LPARAM
)data
);
88 LRESULT APIENTRY _EXPORT
wxExecuteWindowCbk(HWND hWnd
, UINT message
,
89 WPARAM wParam
, LPARAM lParam
)
91 wxExecuteData
*data
= (wxExecuteData
*)lParam
;
93 if (message
== wxEXECUTE_WIN_MESSAGE
) {
96 data
->handler
->OnTerminate((int)data
->process
);
106 extern char wxPanelClassName
[];
108 long wxExecute(const wxString
& command
, bool sync
, wxProcess
*handler
)
123 // copy the command line
124 clen
= command
.Length();
125 if (!clen
) return -1;
126 cl
= (char *) calloc( 1, 256);
128 strcpy( cl
, WXSTRINGCAST command
);
130 // isolate command and arguments
131 argp
= strchr( cl
, ' ');
136 result
= ShellExecute((HWND
) (wxTheApp
->GetTopWindow() ? (HWND
) wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
137 (const wchar_t) "open", (const wchar_t) cl
, (const wchar_t) argp
,
138 (const wchar_t) NULL
, SW_SHOWNORMAL
);
140 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
141 "open", cl
, argp
, NULL
, SW_SHOWNORMAL
);
144 if (((long)result
) <= 32) {
147 wxLogSysError(_("Can't execute command '%s'"), command
.c_str());
152 data
= new wxExecuteData
;
155 window
= CreateWindow(wxPanelClassName
, NULL
, 0, 0, 0, 0, 0, NULL
,
156 (HMENU
) NULL
, wxGetInstance(), 0);
158 FARPROC ExecuteWindowInstance
= MakeProcInstance((FARPROC
) wxExecuteWindowCbk
,
161 SetWindowLong(window
, GWL_WNDPROC
, (LONG
) ExecuteWindowInstance
);
162 SetWindowLong(window
, GWL_USERDATA
, (LONG
) data
);
164 data
->process
= result
;
165 data
->window
= window
;
167 data
->handler
= (sync
) ? NULL
: handler
;
169 dresult
= (DWORD
)CreateThread(NULL
, 0,
170 (LPTHREAD_START_ROUTINE
)wxExecuteThread
,
171 (void *)data
, 0, &tid
);
173 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
174 DestroyWindow(window
);
184 // waiting until command executed
191 long instanceID
= WinExec((LPCSTR
) WXSTRINGCAST command
, SW_SHOW
);
192 if (instanceID
< 32) return(0);
198 running
= GetModuleUsage((HANDLE
)instanceID
);
205 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
207 wxString command
= "";
209 while (*argv
!= NULL
) {
214 command
.RemoveLast();
215 return wxExecute(command
, sync
, handler
);