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