]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utilsexc.cpp
Made wxSocket compile using makefiles; #ifdefed out <<, >> operators in stream.cpp
[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/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 #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 return 0;
148 }
149
150 // Alloc data
151 data = new wxExecuteData;
152
153 // Create window
154 window = CreateWindow(wxPanelClassName, NULL, 0, 0, 0, 0, 0, NULL,
155 (HMENU) NULL, wxGetInstance(), 0);
156
157 FARPROC ExecuteWindowInstance = MakeProcInstance((FARPROC) wxExecuteWindowCbk,
158 wxGetInstance());
159
160 SetWindowLong(window, GWL_WNDPROC, (LONG) ExecuteWindowInstance);
161 SetWindowLong(window, GWL_USERDATA, (LONG) data);
162
163 data->process = result;
164 data->window = window;
165 data->state = sync;
166 data->handler = (sync) ? NULL : handler;
167
168 dresult = (DWORD)CreateThread(NULL, 0,
169 (LPTHREAD_START_ROUTINE)wxExecuteThread,
170 (void *)data, 0, &tid);
171 if (dresult == 0) {
172 wxDebugMsg("wxExecute PANIC: I can't create the waiting thread !");
173 DestroyWindow(window);
174 return (long)result;
175 }
176
177 if (!sync)
178 {
179 free(cl);
180 return (long)result;
181 }
182
183 // waiting until command executed
184 while (data->state)
185 wxYield();
186
187 free(cl);
188 return 0;
189 #else
190 long instanceID = WinExec((LPCSTR) WXSTRINGCAST command, SW_SHOW);
191 if (instanceID < 32) return(0);
192
193 if (sync) {
194 int running;
195 do {
196 wxYield();
197 running = GetModuleUsage((HANDLE)instanceID);
198 } while (running);
199 }
200 return(instanceID);
201 #endif
202 }