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