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>
57 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
63 #define wxEXECUTE_WIN_MESSAGE 10000
65 struct wxExecuteData
{
74 static DWORD
wxExecuteThread(wxExecuteData
*data
)
76 WaitForSingleObject(data
->process
, INFINITE
);
78 // Send an implicit message to the window
79 SendMessage(data
->window
, wxEXECUTE_WIN_MESSAGE
, 0, (LPARAM
)data
);
86 LRESULT APIENTRY _EXPORT
wxExecuteWindowCbk(HWND hWnd
, UINT message
,
87 WPARAM wParam
, LPARAM lParam
)
89 wxExecuteData
*data
= (wxExecuteData
*)lParam
;
91 if (message
== wxEXECUTE_WIN_MESSAGE
) {
94 data
->handler
->OnTerminate((int)data
->process
);
104 extern char wxPanelClassName
[];
106 long wxExecute(const wxString
& command
, bool sync
, wxProcess
*handler
)
121 // copy the command line
122 clen
= command
.Length();
123 if (!clen
) return -1;
124 cl
= (char *) calloc( 1, 256);
126 strcpy( cl
, WXSTRINGCAST command
);
128 // isolate command and arguments
129 argp
= strchr( cl
, ' ');
134 result
= ShellExecute((HWND
) (wxTheApp
->GetTopWindow() ? (HWND
) wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
135 (const wchar_t) "open", (const wchar_t) cl
, (const wchar_t) argp
,
136 (const wchar_t) NULL
, SW_SHOWNORMAL
);
138 result
= ShellExecute( (HWND
) (wxTheApp
->GetTopWindow() ? wxTheApp
->GetTopWindow()->GetHWND() : NULL
),
139 "open", cl
, argp
, NULL
, SW_SHOWNORMAL
);
142 if (((long)result
) <= 32) {
145 wxLogSysError(_("Can't execute command '%s'"), command
.c_str());
150 data
= new wxExecuteData
;
153 window
= CreateWindow(wxPanelClassName
, NULL
, 0, 0, 0, 0, 0, NULL
,
154 (HMENU
) NULL
, wxGetInstance(), 0);
156 FARPROC ExecuteWindowInstance
= MakeProcInstance((FARPROC
) wxExecuteWindowCbk
,
159 SetWindowLong(window
, GWL_WNDPROC
, (LONG
) ExecuteWindowInstance
);
160 SetWindowLong(window
, GWL_USERDATA
, (LONG
) data
);
162 data
->process
= result
;
163 data
->window
= window
;
165 data
->handler
= (sync
) ? NULL
: handler
;
167 dresult
= (DWORD
)CreateThread(NULL
, 0,
168 (LPTHREAD_START_ROUTINE
)wxExecuteThread
,
169 (void *)data
, 0, &tid
);
171 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
172 DestroyWindow(window
);
182 // waiting until command executed
189 long instanceID
= WinExec((LPCSTR
) WXSTRINGCAST command
, SW_SHOW
);
190 if (instanceID
< 32) return(0);
196 running
= GetModuleUsage((HANDLE
)instanceID
);
203 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
205 wxString command
= "";
207 while (*argv
!= NULL
) {
212 command
.RemoveLast();
213 return wxExecute(command
, sync
, handler
);