source: trunk/gsdl/src/w32server/startbrowser.cpp@ 611

Last change on this file since 611 was 611, checked in by sjboddie, 25 years ago

initial commit of windows server code

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1#include "startbrowser.h"
2#include <windows.h>
3#include <DDE.H>
4#include <DDEML.H>
5#include <string.h>
6#include <direct.h>
7#include "locate.h"
8
9#define SB_COMLEN 1024
10#define SB_DDETIMEOUT 60000UL // One minute in milliseconds
11
12int numbrowserstarted = 0;
13
14static int inited = 0;
15
16static DWORD idInst = 0;
17static HINSTANCE hinst = NULL;
18static HSZ hszNetscapeName = NULL;
19static HSZ hszIExploreName = NULL;
20static HSZ hszOpenURLTopic = NULL;
21static HSZ hszActivateTopic = NULL;
22
23
24// budget callback
25HDDEDATA CALLBACK DdeCallback(UINT uType, /* transaction type */
26 UINT uFmt, /* clipboard data format */
27 HCONV hconv, /* handle of conversation */
28 HSZ hsz1, /* handle of string */
29 HSZ hsz2, /* handle of string */
30 HDDEDATA hdata, /* handle of global memory object */
31 DWORD dwData1, /* transaction-specific data */
32 DWORD dwData2) /* transaction-specific data */
33{
34 return (HDDEDATA) NULL;
35}
36
37
38// returns 0 on failure
39static int talkdde (HSZ hszServName, HSZ hszSysTopic, char *requeststr) {
40 HSZ hszRequest;
41 HCONV hConv;
42 HDDEDATA result;
43
44 // try to connect to the newly started browser
45 hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
46 if (hConv == NULL) return 0;
47
48 hszRequest = DdeCreateStringHandle (idInst, requeststr, CP_WINANSI);
49
50//HDDEDATA hRetVal = DdeClientTransaction(NULL, 0ul, m_hConv,
51// hszItem, CF_TEXT,
52// XTYP_REQUEST,
53// DDETIMEOUT, NULL);
54 result = DdeClientTransaction (
55 NULL, // pointer to data to pass to server
56 0, // length of data
57 hConv, // handle to conversation
58 hszRequest, // handle to item name string
59 CF_TEXT, // clipboard data format
60 XTYP_REQUEST, // transaction type
61 SB_DDETIMEOUT, // time-out duration
62 NULL // pointer to transaction result
63 );
64
65 if (result != NULL) DdeFreeDataHandle (result);
66 DdeDisconnect(hConv);
67 DdeFreeStringHandle (idInst, hszRequest);
68
69 return 1;
70}
71
72
73// returns 0 on failure
74static int openurl(HSZ hszServName, HSZ hszActivateTopic, HSZ hszOpenURLTopic, char *url) {
75 char requeststr[65600];
76
77 // bring the browser to the front, don't worry if this fails
78 talkdde (hszServName, hszActivateTopic, "0xFFFFFFFF,0x0");
79
80 // tell the browser to open a url
81 strcpy (requeststr, "\"");
82 strcat (requeststr, url);
83 strcat (requeststr, "\",,0xFFFFFFFF,0x0,,,");
84
85 return talkdde (hszServName, hszOpenURLTopic, requeststr);
86}
87
88// returns 0 on failure
89static int tryconnect(HSZ hszServName, HSZ hszSysTopic) {
90 // try to connect to a running browser
91 HCONV hConv = DdeConnect (idInst, hszServName, hszSysTopic, (PCONVCONTEXT) NULL);
92 if (hConv == NULL) return 0;
93 DdeDisconnect(hConv);
94 return 1;
95}
96
97static void freestrings () {
98 // free up any currently allocated strings
99 if (hszNetscapeName != NULL) DdeFreeStringHandle (idInst, hszNetscapeName);
100 hszNetscapeName = NULL;
101 if (hszIExploreName != NULL) DdeFreeStringHandle (idInst, hszIExploreName);
102 hszIExploreName = NULL;
103 if (hszOpenURLTopic != NULL) DdeFreeStringHandle (idInst, hszOpenURLTopic);
104 hszOpenURLTopic = NULL;
105 if (hszActivateTopic != NULL) DdeFreeStringHandle (idInst, hszActivateTopic);
106 hszActivateTopic = NULL;
107}
108
109
110
111static void getbrowserinfo() {
112 freestrings();
113
114 hszNetscapeName = DdeCreateStringHandle(idInst,"NETSCAPE",CP_WINANSI);
115 hszIExploreName = DdeCreateStringHandle(idInst,"IExplore",CP_WINANSI);
116
117 hszOpenURLTopic = DdeCreateStringHandle(idInst,"WWW_OpenURL",CP_WINANSI);
118 hszActivateTopic = DdeCreateStringHandle(idInst,"WWW_Activate",CP_WINANSI);
119}
120
121
122
123// returns SB_NOERROR on success,
124// SB_ALREADYINIT or SB_DDE_FAILINIT on failure
125int initstartbrowser () {
126 if (inited) return SB_ALREADYINIT;
127
128// from nstest if(DdeInitialize(&CDDEObject::m_dwidInst, NstestDdeCallBack, APPCLASS_STANDARD, 0ul))
129 if (DdeInitialize (&idInst, (PFNCALLBACK) DdeCallback,
130 APPCLASS_STANDARD, 0)
131// CBF_FAIL_EXECUTES | CBF_SKIP_ALLNOTIFICATIONS, 0)
132 != DMLERR_NO_ERROR) {
133 freestrings();
134 inited = 0;
135 return SB_DDE_FAILINIT;
136 }
137
138 getbrowserinfo();
139
140 inited = 1;
141 return SB_NOERROR;
142}
143
144
145// returns SB_NOERROR on success,
146// SB_DDE_FAILDEINIT of failure
147int deinitstartbrowser () {
148 // make sure this has been inited
149 // not being inited here, however, is not
150 // important
151 if (!inited) return SB_NOERROR;
152 inited = 0;
153
154 freestrings();
155
156 if (DdeUninitialize(idInst)) return SB_NOERROR;
157
158 return SB_DDE_FAILDEINIT;
159}
160
161
162// startbrowser will try to start a browser (or connect to a running browser).
163//
164// Returns SB_NOERROR on success,
165// SB_NOTINITED, SB_FAIL_BADFORMAT, SB_FAIL_NOTFOUND, SB_FAIL_NORESOURCES, or
166// SB_FAILCONNECTBROWSER on failure
167int startbrowser (char *url, char *browser_exe, char *startdir) {
168 char newcommand[SB_COMLEN];
169 int usingnetscape = 0;
170 int usingiexplore = 0;
171
172 if (!inited) return SB_NOTINITED; // has to be inited first
173
174 // find out which browser we are dealing with
175 strcpy (newcommand, browser_exe);
176 _strlwr (newcommand);
177 if (strstr (newcommand, "netscape.exe") != NULL) {
178 // netscape
179 usingnetscape = 1;
180 } else if (strstr (newcommand, "iexplore.exe") != NULL) {
181 // internet explorer
182 usingiexplore = 1;
183 }
184
185 // only try to communicate with a running browser if a startdir
186 // is not specified
187 if (startdir == NULL) {
188 if (usingnetscape && hszNetscapeName != NULL &&
189 openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
190 return SB_NOERROR;
191 }
192 if (usingiexplore && hszIExploreName != NULL &&
193 openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
194 return SB_NOERROR;
195 }
196 }
197
198 strcpy (newcommand, browser_exe);
199
200 // don't put the url on the command line for internet explorer
201 if (usingiexplore) {
202 strcat (newcommand, " -nohome");
203
204 } else {
205 // will have to put the url on the command line
206 strcat (newcommand, " \"");
207 strcat (newcommand, url);
208 strcat (newcommand, "\"");
209 }
210
211 // change the working directory (if needed)
212 char cwd[1024];
213 cwd[0] = '\0';
214 if (startdir != NULL) {
215 _getcwd(cwd, 1024);
216 _chdir(startdir);
217 }
218
219 // attempt to start the browser
220 int res = WinExec(newcommand, SW_SHOW);
221
222 // change the working directory back (if needed)
223 if (startdir != NULL) _chdir(cwd);
224
225 if (res == ERROR_BAD_FORMAT) return SB_FAIL_BADFORMAT;
226 if ((res == ERROR_FILE_NOT_FOUND) || (res == ERROR_PATH_NOT_FOUND))
227 return SB_FAIL_NOTFOUND;
228 if (res <= 31) return SB_FAIL_NORESOURCES;
229
230 numbrowserstarted++;
231
232 // if we aren't using iexplore then the url was
233 // put on the command line and there is nothing left to do
234 if (!usingiexplore) return SB_NOERROR;
235
236 // connect to the browser and get it to open a page,
237 // time out after 1 minute
238 DWORD lastcheck = GetTickCount ();
239 while (DiffTickCounts (lastcheck, GetTickCount()) <= 60000) {
240 if (usingnetscape && hszNetscapeName != NULL &&
241 openurl(hszNetscapeName, hszActivateTopic, hszOpenURLTopic, url)) {
242 return SB_NOERROR;
243 }
244 if (usingiexplore && hszIExploreName != NULL &&
245 openurl(hszIExploreName, hszActivateTopic, hszOpenURLTopic, url)) {
246 return SB_NOERROR;
247 }
248 }
249
250 // timed out trying to connect to the browser
251 return SB_FAILCONNECTBROWSER;
252}
253
254
255
256// returns SB_NOERROR on success,
257// SB_NOTINITED, SB_FAILCONNECTBROWSER on failure
258int browserrunning (char *browser_exe) {
259 char newcommand[SB_COMLEN];
260 if (!inited) return SB_NOTINITED; // has to be inited first
261
262 strcpy (newcommand, browser_exe);
263 _strlwr (newcommand);
264
265 if ((strstr (newcommand, "netscape.exe") != NULL) && (hszNetscapeName != NULL) &&
266 tryconnect(hszNetscapeName, hszOpenURLTopic)) {
267 // succeeded connecting to netscape
268 return SB_NOERROR;
269
270 } else if ((strstr (newcommand, "iexplore.exe") != NULL) && (hszIExploreName != NULL) &&
271 tryconnect(hszIExploreName, hszOpenURLTopic)) {
272 // succeeded connecting to internet explorer
273 return SB_NOERROR;
274 }
275
276 return SB_FAILCONNECTBROWSER;
277}
Note: See TracBrowser for help on using the repository browser.