]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsexc.cpp
no message
[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 #include <dos.h>
38 #endif
39
40 #ifdef __GNUWIN32__
41 #include <sys/unistd.h>
42 #include <sys/stat.h>
43 #ifndef __MINGW32__
44 #include <std.h>
45 #endif
46
47 #endif
48
49 #ifdef __WIN32__
50 #include <io.h>
51
52 #ifndef __GNUWIN32__
53 #include <shellapi.h>
54 #endif
55 #endif
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #ifndef __WATCOMC__
61 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
62 #include <errno.h>
63 #endif
64 #endif
65 #include <stdarg.h>
66
67 #define wxEXECUTE_WIN_MESSAGE 10000
68
69 struct wxExecuteData {
70 HWND window;
71 HINSTANCE process;
72 wxProcess *handler;
73 char state;
74 };
75
76
77 #ifdef __WIN32__
78 static DWORD wxExecuteThread(wxExecuteData *data)
79 {
80 WaitForSingleObject(data->process, INFINITE);
81
82 // Send an implicit message to the window
83 SendMessage(data->window, wxEXECUTE_WIN_MESSAGE, 0, (LPARAM)data);
84
85 return 0;
86 }
87 #endif
88
89
90 LRESULT APIENTRY _EXPORT wxExecuteWindowCbk(HWND hWnd, UINT message,
91 WPARAM wParam, LPARAM lParam)
92 {
93 wxExecuteData *data = (wxExecuteData *)lParam;
94
95 if (message == wxEXECUTE_WIN_MESSAGE) {
96 DestroyWindow(hWnd);
97 if (data->handler)
98 data->handler->OnTerminate((int)data->process);
99
100 if (data->state)
101 data->state = 0;
102 else
103 delete data;
104 }
105 return 0;
106 }
107
108 extern char wxPanelClassName[];
109
110 long wxExecute(const wxString& command, bool sync, wxProcess *handler)
111 {
112 if (command == "")
113 return 0;
114
115 #ifdef __WIN32__
116 char * cl;
117 char * argp;
118 int clen;
119 HINSTANCE result;
120 DWORD dresult;
121 HWND window;
122 wxExecuteData *data;
123 DWORD tid;
124
125 // copy the command line
126 clen = command.Length();
127 if (!clen) return -1;
128 cl = (char *) calloc( 1, 256);
129 if (!cl) return -1;
130 strcpy( cl, WXSTRINGCAST command);
131
132 // isolate command and arguments
133 argp = strchr( cl, ' ');
134 if (argp)
135 *argp++ = '\0';
136
137 #ifdef __GNUWIN32__
138 result = ShellExecute((HWND) (wxTheApp->GetTopWindow() ? (HWND) wxTheApp->GetTopWindow()->GetHWND() : NULL),
139 (const wchar_t) "open", (const wchar_t) cl, (const wchar_t) argp,
140 (const wchar_t) NULL, SW_SHOWNORMAL);
141 #else
142 result = ShellExecute( (HWND) (wxTheApp->GetTopWindow() ? wxTheApp->GetTopWindow()->GetHWND() : NULL),
143 "open", cl, argp, NULL, SW_SHOWNORMAL);
144 #endif
145
146 if (((long)result) <= 32) {
147 free(cl);
148
149 wxLogSysError(_("Can't execute command '%s'"), command.c_str());
150 return 0;
151 }
152
153 // Alloc data
154 data = new wxExecuteData;
155
156 // Create window
157 window = CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
158 (HMENU) NULL, wxGetInstance(), 0);
159
160 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC) wxExecuteWindowCbk,
161 wxGetInstance());
162
163 SetWindowLong(window, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
164 SetWindowLong(window, GWL_USERDATA, (LONG) data);
165
166 data->process = result;
167 data->window = window;
168 data->state = sync;
169 data->handler = (sync) ? NULL : handler;
170
171 dresult = (DWORD)CreateThread(NULL, 0,
172 (LPTHREAD_START_ROUTINE)wxExecuteThread,
173 (void *)data, 0, &tid);
174 if (dresult == 0) {
175 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
176 DestroyWindow(window);
177 return (long)result;
178 }
179
180 if (!sync)
181 {
182 free(cl);
183 return (long)result;
184 }
185
186 // waiting until command executed
187 while (data->state)
188 wxYield();
189
190 free(cl);
191 return 0;
192 #else
193 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
194 if (instanceID < 32) return(0);
195
196 if (sync) {
197 int running;
198 do {
199 wxYield();
200 running = GetModuleUsage((HANDLE)instanceID);
201 } while (running);
202 }
203 return(instanceID);
204 #endif
205 }
206
207 long wxExecute(char **argv, bool sync, wxProcess *handler)
208 {
209 wxString command = "";
210
211 while (*argv != NULL) {
212 command += *argv;
213 command += ' ';
214 argv++;
215 }
216 command.RemoveLast();
217 return wxExecute(command, sync, handler);
218 }