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"
31 #include "wx/msw/private.h"
36 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
45 #include <sys/unistd.h>
62 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
68 #define wxEXECUTE_WIN_MESSAGE 10000
70 struct wxExecuteData
{
79 static DWORD
wxExecuteThread(wxExecuteData
*data
)
81 WaitForSingleObject(data
->process
, INFINITE
);
83 // Send an implicit message to the window
84 SendMessage(data
->window
, wxEXECUTE_WIN_MESSAGE
, 0, (LPARAM
)data
);
91 LRESULT APIENTRY _EXPORT
wxExecuteWindowCbk(HWND hWnd
, UINT message
,
92 WPARAM wParam
, LPARAM lParam
)
94 wxExecuteData
*data
= (wxExecuteData
*)lParam
;
96 if (message
== wxEXECUTE_WIN_MESSAGE
) {
99 data
->handler
->OnTerminate((int)data
->process
);
109 extern char wxPanelClassName
[];
111 long wxExecute(const wxString
& command
, bool sync
, wxProcess
*handler
)
116 #if defined(__WIN32__) && !defined(__TWIN32__)
126 // copy the command line
127 clen
= command
.Length();
128 if (!clen
) return -1;
129 cl
= (char *) calloc( 1, 256);
131 strcpy( cl
, WXSTRINGCAST command
);
133 // isolate command and arguments
134 argp
= strchr( cl
, ' ');
139 result
= ShellExecute((HWND
) (wxTheApp
->GetTopWindow() ? (HWND
) wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
140 (const wchar_t) "open", (const wchar_t) cl
, (const wchar_t) argp
,
141 (const wchar_t) NULL
, SW_SHOWNORMAL
);
143 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
144 "open", cl
, argp
, NULL
, SW_SHOWNORMAL
);
147 if (((long)result
) <= 32) {
150 wxLogSysError(_("Can't execute command '%s'"), command
.c_str());
155 data
= new wxExecuteData
;
158 window
= CreateWindow(wxPanelClassName
, NULL
, 0, 0, 0, 0, 0, NULL
,
159 (HMENU
) NULL
, wxGetInstance(), 0);
161 FARPROC ExecuteWindowInstance
= MakeProcInstance((FARPROC
) wxExecuteWindowCbk
,
164 SetWindowLong(window
, GWL_WNDPROC
, (LONG
) ExecuteWindowInstance
);
165 SetWindowLong(window
, GWL_USERDATA
, (LONG
) data
);
167 data
->process
= result
;
168 data
->window
= window
;
170 data
->handler
= (sync
) ? NULL
: handler
;
172 dresult
= (DWORD
)CreateThread(NULL
, 0,
173 (LPTHREAD_START_ROUTINE
)wxExecuteThread
,
174 (void *)data
, 0, &tid
);
176 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
177 DestroyWindow(window
);
187 // waiting until command executed
194 long instanceID
= WinExec((LPCSTR
) WXSTRINGCAST command
, SW_SHOW
);
195 if (instanceID
< 32) return(0);
201 running
= GetModuleUsage((HANDLE
)instanceID
);
208 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
210 wxString command
= "";
212 while (*argv
!= NULL
) {
217 command
.RemoveLast();
218 return wxExecute(command
, sync
, handler
);