]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsexc.cpp
mac support as stubs added
[wxWidgets.git] / src / msw / utilsexc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilsexec.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #include "wx/utils.h"
26 #include "wx/app.h"
27 #endif
28
29 #include "wx/log.h"
30 #include "wx/msw/private.h"
31 #include <windows.h>
32
33 #include <ctype.h>
34
35 #ifndef __GNUWIN32__
36 #include <direct.h>
37 #ifndef __MWERKS__
38 #include <dos.h>
39 #endif
40 #endif
41
42 #ifdef __GNUWIN32__
43 #include <sys/unistd.h>
44 #include <sys/stat.h>
45 #endif
46
47 #ifdef __WIN32__
48 #include <io.h>
49
50 #ifndef __GNUWIN32__
51 #include <shellapi.h>
52 #endif
53 #endif
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #ifndef __WATCOMC__
59 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
60 #include <errno.h>
61 #endif
62 #endif
63 #include <stdarg.h>
64
65 #define wxEXECUTE_WIN_MESSAGE 10000
66
67 struct wxExecuteData {
68 HWND window;
69 HINSTANCE process;
70 wxProcess *handler;
71 char state;
72 };
73
74
75 #ifdef __WIN32__
76 static DWORD wxExecuteThread(wxExecuteData *data)
77 {
78 WaitForSingleObject(data->process, INFINITE);
79
80 // Send an implicit message to the window
81 SendMessage(data->window, wxEXECUTE_WIN_MESSAGE, 0, (LPARAM)data);
82
83 return 0;
84 }
85 #endif
86
87
88 LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
89 WPARAM wParam, LPARAM lParam)
90 {
91 wxExecuteData *data = (wxExecuteData *)lParam;
92
93 if (message == wxEXECUTE_WIN_MESSAGE) {
94 DestroyWindow(hWnd);
95 if (data->handler)
96 data->handler->OnTerminate((int)data->process);
97
98 if (data->state)
99 data->state = 0;
100 else
101 delete data;
102 }
103 return 0;
104 }
105
106 extern char wxPanelClassName[];
107
108 long wxExecute(const wxString& command, bool sync, wxProcess *handler)
109 {
110 if (command == "")
111 return 0;
112
113 #ifdef __WIN32__
114 char * cl;
115 char * argp;
116 int clen;
117 HINSTANCE result;
118 DWORD dresult;
119 HWND window;
120 wxExecuteData *data;
121 DWORD tid;
122
123 // copy the command line
124 clen = command.Length();
125 if (!clen) return -1;
126 cl = (char *) calloc( 1, 256);
127 if (!cl) return -1;
128 strcpy( cl, WXSTRINGCAST command);
129
130 // isolate command and arguments
131 argp = strchr( cl, ' ');
132 if (argp)
133 *argp++ = '\0';
134
135 #ifdef __GNUWIN32__
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);
139 #else
140 result = ShellExecute( (HWND) (wxTheApp->GetTopWindow() ? wxTheApp->GetTopWindow()->GetHWND() : NULL),
141 "open", cl, argp, NULL, SW_SHOWNORMAL);
142 #endif
143
144 if (((long)result) <= 32) {
145 free(cl);
146
147 wxLogSysError(_("Can't execute command '%s'"), command.c_str());
148 return 0;
149 }
150
151 // Alloc data
152 data = new wxExecuteData;
153
154 // Create window
155 window = CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
156 (HMENU) NULL, wxGetInstance(), 0);
157
158 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC) wxExecuteWindowCbk,
159 wxGetInstance());
160
161 SetWindowLong(window, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
162 SetWindowLong(window, GWL_USERDATA, (LONG) data);
163
164 data->process = result;
165 data->window = window;
166 data->state = sync;
167 data->handler = (sync) ? NULL : handler;
168
169 dresult = (DWORD)CreateThread(NULL, 0,
170 (LPTHREAD_START_ROUTINE)wxExecuteThread,
171 (void *)data, 0, &tid);
172 if (dresult == 0) {
173 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
174 DestroyWindow(window);
175 return (long)result;
176 }
177
178 if (!sync)
179 {
180 free(cl);
181 return (long)result;
182 }
183
184 // waiting until command executed
185 while (data->state)
186 wxYield();
187
188 free(cl);
189 return 0;
190 #else
191 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
192 if (instanceID < 32) return(0);
193
194 if (sync) {
195 int running;
196 do {
197 wxYield();
198 running = GetModuleUsage((HANDLE)instanceID);
199 } while (running);
200 }
201 return(instanceID);
202 #endif
203 }
204
205 long wxExecute(char **argv, bool sync, wxProcess *handler)
206 {
207 wxString command = "";
208
209 while (*argv != NULL) {
210 command += *argv;
211 command += ' ';
212 argv++;
213 }
214 command.RemoveLast();
215 return wxExecute(command, sync, handler);
216 }