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