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"
44 #include <sys/unistd.h>
60 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
66 #define wxEXECUTE_WIN_MESSAGE 10000
68 struct wxExecuteData
{
77 static DWORD
wxExecuteThread(wxExecuteData
*data
)
79 WaitForSingleObject(data
->process
, INFINITE
);
81 // Send an implicit message to the window
82 SendMessage(data
->window
, wxEXECUTE_WIN_MESSAGE
, 0, (LPARAM
)data
);
89 LRESULT APIENTRY _EXPORT
wxExecuteWindowCbk(HWND hWnd
, UINT message
,
90 WPARAM wParam
, LPARAM lParam
)
92 wxExecuteData
*data
= (wxExecuteData
*)lParam
;
94 if (message
== wxEXECUTE_WIN_MESSAGE
) {
97 data
->handler
->OnTerminate((int)data
->process
);
107 extern char wxPanelClassName
[];
109 long wxExecute(const wxString
& command
, bool sync
, wxProcess
*handler
)
124 // copy the command line
125 clen
= command
.Length();
126 if (!clen
) return -1;
127 cl
= (char *) calloc( 1, 256);
129 strcpy( cl
, WXSTRINGCAST command
);
131 // isolate command and arguments
132 argp
= strchr( cl
, ' ');
137 result
= ShellExecute((HWND
) (wxTheApp
->GetTopWindow() ? (HWND
) wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
138 (const wchar_t) "open", (const wchar_t) cl
, (const wchar_t) argp
,
139 (const wchar_t) NULL
, SW_SHOWNORMAL
);
141 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
142 "open", cl
, argp
, NULL
, SW_SHOWNORMAL
);
145 if (((long)result
) <= 32) {
148 wxLogSysError(_("Can't execute command '%s'"), command
.c_str());
153 data
= new wxExecuteData
;
156 window
= CreateWindow(wxPanelClassName
, NULL
, 0, 0, 0, 0, 0, NULL
,
157 (HMENU
) NULL
, wxGetInstance(), 0);
159 FARPROC ExecuteWindowInstance
= MakeProcInstance((FARPROC
) wxExecuteWindowCbk
,
162 SetWindowLong(window
, GWL_WNDPROC
, (LONG
) ExecuteWindowInstance
);
163 SetWindowLong(window
, GWL_USERDATA
, (LONG
) data
);
165 data
->process
= result
;
166 data
->window
= window
;
168 data
->handler
= (sync
) ? NULL
: handler
;
170 dresult
= (DWORD
)CreateThread(NULL
, 0,
171 (LPTHREAD_START_ROUTINE
)wxExecuteThread
,
172 (void *)data
, 0, &tid
);
174 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
175 DestroyWindow(window
);
185 // waiting until command executed
192 long instanceID
= WinExec((LPCSTR
) WXSTRINGCAST command
, SW_SHOW
);
193 if (instanceID
< 32) return(0);
199 running
= GetModuleUsage((HANDLE
)instanceID
);
206 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
208 wxString command
= "";
210 while (*argv
!= NULL
) {
215 command
.RemoveLast();
216 return wxExecute(command
, sync
, handler
);