applied patch 439186 (mingw and VC5 fixes)
[wxWidgets.git] / src / msw / dialup.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dialup.cpp
3 // Purpose: MSW implementation of network/dialup classes and functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 07.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // these functions require Win32
28 #if defined(__WIN16__) && wxUSE_DIALUP_MANAGER
29 #undef wxUSE_DIALUP_MANAGER
30 #define wxUSE_DIALUP_MANAGER 0
31 #endif // wxUSE_DIALUP_MANAGER && Win16
32
33 #if wxUSE_DIALUP_MANAGER
34
35 #ifndef WX_PRECOMP
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #include "wx/event.h"
39 #endif
40
41 #include "wx/timer.h"
42 #include "wx/app.h"
43 #include "wx/generic/choicdgg.h"
44
45 #include "wx/msw/private.h" // must be before #include "dynlib.h"
46
47 #if !wxUSE_DYNLIB_CLASS
48 #error You need wxUSE_DYNLIB_CLASS to be 1 to compile dialup.cpp.
49 #endif
50
51 #include "wx/dynlib.h"
52
53 #include "wx/dialup.h"
54
55 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED)
56 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED)
57
58 // Doesn't yet compile under VC++ 4, BC++, Watcom C++: no wininet.h
59 #if !defined(__BORLANDC__) && \
60 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
61 !defined(__GNUWIN32_OLD__) && \
62 !defined(__WATCOMC__) && \
63 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
64
65 #include <ras.h>
66 #include <raserror.h>
67
68 #include <wininet.h>
69
70 // Not in VC++ 5
71 #ifndef INTERNET_CONNECTION_LAN
72 #define INTERNET_CONNECTION_LAN 2
73 #endif
74 #ifndef INTERNET_CONNECTION_PROXY
75 #define INTERNET_CONNECTION_PROXY 4
76 #endif
77
78 // ----------------------------------------------------------------------------
79 // constants
80 // ----------------------------------------------------------------------------
81
82 // this message is sent by the secondary thread when RAS status changes
83 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
84 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
85
86 // ----------------------------------------------------------------------------
87 // types
88 // ----------------------------------------------------------------------------
89
90 // the signatures of RAS functions: all this is quite heavy, but we must do it
91 // to allow running wxWin programs on machine which don't have RAS installed
92 // (this does exist) - if we link with rasapi32.lib, the program will fail on
93 // startup because of the missing DLL...
94
95 #ifndef UNICODE
96 typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCSTR, LPRASDIALPARAMSA, DWORD, LPVOID, LPHRASCONN );
97 typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNA, LPDWORD, LPDWORD );
98 typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCSTR, LPCSTR, LPRASENTRYNAMEA, LPDWORD, LPDWORD );
99 typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSA );
100 typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPSTR, DWORD );
101 typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN );
102 typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD );
103 typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCSTR );
104 typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCSTR, LPCSTR );
105 typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, BOOL );
106 typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, LPBOOL );
107 typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOA, LPDWORD, LPDWORD );
108 typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOA, LPDWORD );
109 typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, LPDWORD, LPBYTE, LPDWORD );
110 typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, DWORD, LPBYTE, DWORD );
111 typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCSTR, LPCSTR, LPCSTR );
112 typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCSTR, LPCSTR );
113 typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCSTR, LPCSTR );
114 typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD );
115
116 static const wxChar gs_funcSuffix = _T('A');
117 #else // Unicode
118 typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCWSTR, LPRASDIALPARAMSW, DWORD, LPVOID, LPHRASCONN );
119 typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNW, LPDWORD, LPDWORD );
120 typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCWSTR, LPCWSTR, LPRASENTRYNAMEW, LPDWORD, LPDWORD );
121 typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSW );
122 typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPWSTR, DWORD );
123 typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN );
124 typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD );
125 typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCWSTR );
126 typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCWSTR, LPCWSTR );
127 typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, BOOL );
128 typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, LPBOOL );
129 typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOW, LPDWORD, LPDWORD );
130 typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOW, LPDWORD );
131 typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, LPDWORD, LPBYTE, LPDWORD );
132 typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, DWORD, LPBYTE, DWORD );
133 typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCWSTR, LPCWSTR, LPCWSTR );
134 typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCWSTR, LPCWSTR );
135 typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCWSTR, LPCWSTR );
136 typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD );
137
138 static const wxChar gs_funcSuffix = _T('W');
139 #endif // ASCII/Unicode
140
141 // structure passed to the secondary thread
142 struct WXDLLEXPORT wxRasThreadData
143 {
144 wxRasThreadData()
145 {
146 hWnd = 0;
147 hEventRas = hEventQuit = INVALID_HANDLE_VALUE;
148 dialUpManager = NULL;
149 }
150
151 HWND hWnd; // window to send notifications to
152 HANDLE hEventRas, // event which RAS signals when status changes
153 hEventQuit; // event which we signal when we terminate
154
155 class WXDLLEXPORT wxDialUpManagerMSW *dialUpManager; // the owner
156 };
157
158 // ----------------------------------------------------------------------------
159 // wxDialUpManager class for MSW
160 // ----------------------------------------------------------------------------
161
162 class WXDLLEXPORT wxDialUpManagerMSW : public wxDialUpManager
163 {
164 public:
165 // ctor & dtor
166 wxDialUpManagerMSW();
167 virtual ~wxDialUpManagerMSW();
168
169 // implement base class pure virtuals
170 virtual bool IsOk() const;
171 virtual size_t GetISPNames(wxArrayString& names) const;
172 virtual bool Dial(const wxString& nameOfISP,
173 const wxString& username,
174 const wxString& password,
175 bool async);
176 virtual bool IsDialing() const;
177 virtual bool CancelDialing();
178 virtual bool HangUp();
179 virtual bool IsAlwaysOnline() const;
180 virtual bool IsOnline() const;
181 virtual void SetOnlineStatus(bool isOnline = TRUE);
182 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);
183 virtual void DisableAutoCheckOnlineStatus();
184 virtual void SetWellKnownHost(const wxString& hostname, int port);
185 virtual void SetConnectCommand(const wxString& commandDial,
186 const wxString& commandHangup);
187
188 // for RasTimer
189 void CheckRasStatus();
190
191 // for wxRasStatusWindowProc
192 void OnConnectStatusChange();
193 void OnDialProgress(RASCONNSTATE rasconnstate, DWORD dwError);
194
195 // for wxRasDialFunc
196 static HWND GetRasWindow() { return ms_hwndRas; }
197 static wxDialUpManagerMSW *GetDialer() { return ms_dialer; }
198
199 private:
200 // return the error string for the given RAS error code
201 static wxString GetErrorString(DWORD error);
202
203 // find the (first) handle of the active connection
204 static HRASCONN FindActiveConnection();
205
206 // notify the application about status change
207 void NotifyApp(bool connected, bool fromOurselves = FALSE) const;
208
209 // destroy the thread data and the thread itself
210 void CleanUpThreadData();
211
212 // timer used for polling RAS status
213 class WXDLLEXPORT RasTimer : public wxTimer
214 {
215 public:
216 RasTimer(wxDialUpManagerMSW *dialUpManager)
217 { m_dialUpManager = dialUpManager; }
218
219 virtual void Notify() { m_dialUpManager->CheckRasStatus(); }
220
221 private:
222 wxDialUpManagerMSW *m_dialUpManager;
223 } m_timerStatusPolling;
224
225 // thread handle for the thread sitting on connection change event
226 HANDLE m_hThread;
227
228 // data used by this thread and our hidden window to send messages between
229 // each other
230 wxRasThreadData m_data;
231
232 // the hidden window we use for passing messages between threads
233 static HWND ms_hwndRas;
234
235 // the handle of the connection we initiated or 0 if none
236 static HRASCONN ms_hRasConnection;
237
238 // the use count of rasapi32.dll
239 static int ms_nDllCount;
240
241 // the handle of rasapi32.dll when it's loaded
242 static wxDllType ms_dllRas;
243
244 // the pointers to RAS functions
245 static RASDIAL ms_pfnRasDial;
246 static RASENUMCONNECTIONS ms_pfnRasEnumConnections;
247 static RASENUMENTRIES ms_pfnRasEnumEntries;
248 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus;
249 static RASGETERRORSTRING ms_pfnRasGetErrorString;
250 static RASHANGUP ms_pfnRasHangUp;
251 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo;
252 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry;
253 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry;
254 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams;
255 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams;
256 static RASENUMDEVICES ms_pfnRasEnumDevices;
257 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo;
258 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties;
259 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties;
260 static RASRENAMEENTRY ms_pfnRasRenameEntry;
261 static RASDELETEENTRY ms_pfnRasDeleteEntry;
262 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName;
263
264 // this function is not supported by Win95
265 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification;
266
267 // if this flag is different from -1, it overrides IsOnline()
268 static int ms_userSpecifiedOnlineStatus;
269
270 // this flag tells us if we're online
271 static int ms_isConnected;
272
273 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
274 // called yet)
275 static int ms_isAlwaysOnline;
276
277 // this flag tells us whether a call to RasDial() is in progress
278 static wxDialUpManagerMSW *ms_dialer;
279 };
280
281 // ----------------------------------------------------------------------------
282 // private functions
283 // ----------------------------------------------------------------------------
284
285 static LRESULT WINAPI wxRasStatusWindowProc(HWND hWnd, UINT message,
286 WPARAM wParam, LPARAM lParam);
287
288 static DWORD wxRasMonitorThread(wxRasThreadData *data);
289
290 static void WINAPI wxRasDialFunc(UINT unMsg,
291 RASCONNSTATE rasconnstate,
292 DWORD dwError);
293
294 // ============================================================================
295 // implementation
296 // ============================================================================
297
298 // ----------------------------------------------------------------------------
299 // init the static variables
300 // ----------------------------------------------------------------------------
301
302 HRASCONN wxDialUpManagerMSW::ms_hRasConnection = 0;
303
304 HWND wxDialUpManagerMSW::ms_hwndRas = 0;
305
306 int wxDialUpManagerMSW::ms_nDllCount = 0;
307 wxDllType wxDialUpManagerMSW::ms_dllRas = 0;
308
309 RASDIAL wxDialUpManagerMSW::ms_pfnRasDial = 0;
310 RASENUMCONNECTIONS wxDialUpManagerMSW::ms_pfnRasEnumConnections = 0;
311 RASENUMENTRIES wxDialUpManagerMSW::ms_pfnRasEnumEntries = 0;
312 RASGETCONNECTSTATUS wxDialUpManagerMSW::ms_pfnRasGetConnectStatus = 0;
313 RASGETERRORSTRING wxDialUpManagerMSW::ms_pfnRasGetErrorString = 0;
314 RASHANGUP wxDialUpManagerMSW::ms_pfnRasHangUp = 0;
315 RASGETPROJECTIONINFO wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo = 0;
316 RASCREATEPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry = 0;
317 RASEDITPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry = 0;
318 RASSETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams = 0;
319 RASGETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams = 0;
320 RASENUMDEVICES wxDialUpManagerMSW::ms_pfnRasEnumDevices = 0;
321 RASGETCOUNTRYINFO wxDialUpManagerMSW::ms_pfnRasGetCountryInfo = 0;
322 RASGETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasGetEntryProperties = 0;
323 RASSETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasSetEntryProperties = 0;
324 RASRENAMEENTRY wxDialUpManagerMSW::ms_pfnRasRenameEntry = 0;
325 RASDELETEENTRY wxDialUpManagerMSW::ms_pfnRasDeleteEntry = 0;
326 RASVALIDATEENTRYNAME wxDialUpManagerMSW::ms_pfnRasValidateEntryName = 0;
327 RASCONNECTIONNOTIFICATION wxDialUpManagerMSW::ms_pfnRasConnectionNotification = 0;
328
329 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus = -1;
330 int wxDialUpManagerMSW::ms_isConnected = -1;
331 int wxDialUpManagerMSW::ms_isAlwaysOnline = -1;
332 wxDialUpManagerMSW *wxDialUpManagerMSW::ms_dialer = NULL;
333
334 // ----------------------------------------------------------------------------
335 // ctor and dtor: the dynamic linking happens here
336 // ----------------------------------------------------------------------------
337
338 // the static creator function is implemented here
339 wxDialUpManager *wxDialUpManager::Create()
340 {
341 return new wxDialUpManagerMSW;
342 }
343
344 #ifdef __VISUALC__
345 // warning about "'this' : used in base member initializer list" - so what?
346 #pragma warning(disable:4355)
347 #endif // VC++
348
349 wxDialUpManagerMSW::wxDialUpManagerMSW()
350 : m_timerStatusPolling(this)
351 {
352 // initialize our data
353 m_hThread = 0;
354
355 if ( !ms_nDllCount++ )
356 {
357 // load the RAS library
358 ms_dllRas = wxDllLoader::LoadLibrary("RASAPI32");
359 if ( !ms_dllRas )
360 {
361 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
362 }
363 else
364 {
365 // resolve the functions we need
366
367 // this will contain the name of the function we failed to resolve
368 // if any at the end
369 const char *funcName = NULL;
370
371 // get the function from rasapi32.dll and abort if it's not found
372 #define RESOLVE_RAS_FUNCTION(type, name) \
373 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
374 wxString(_T(#name)) + gs_funcSuffix); \
375 if ( !ms_pfn##name ) \
376 { \
377 funcName = #name; \
378 goto exit; \
379 }
380
381 // a variant of above macro which doesn't abort if the function is
382 // not found in the DLL
383 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
384 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
385 wxString(_T(#name)) + gs_funcSuffix);
386
387 RESOLVE_RAS_FUNCTION(RASDIAL, RasDial);
388 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS, RasEnumConnections);
389 RESOLVE_RAS_FUNCTION(RASENUMENTRIES, RasEnumEntries);
390 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS, RasGetConnectStatus);
391 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING, RasGetErrorString);
392 RESOLVE_RAS_FUNCTION(RASHANGUP, RasHangUp);
393 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS, RasGetEntryDialParams);
394
395 // suppress wxDllLoader messages about missing (non essential)
396 // functions
397 {
398 wxLogNull noLog;
399
400 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO, RasGetProjectionInfo);
401 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY, RasCreatePhonebookEntry);
402 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY, RasEditPhonebookEntry);
403 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS, RasSetEntryDialParams);
404 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES, RasGetEntryProperties);
405 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES, RasSetEntryProperties);
406 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY, RasRenameEntry);
407 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY, RasDeleteEntry);
408 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME, RasValidateEntryName);
409 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO, RasGetCountryInfo);
410 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES, RasEnumDevices);
411 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION, RasConnectionNotification);
412 }
413
414 // keep your preprocessor name space clean
415 #undef RESOLVE_RAS_FUNCTION
416 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
417
418 exit:
419 if ( funcName )
420 {
421 static const wxChar *msg = wxTRANSLATE(
422 "The version of remote access service (RAS) installed on this machine is too\
423 old, please upgrade (the following required function is missing: %s)."
424 );
425
426 wxLogError(wxGetTranslation(msg), funcName);
427
428 wxDllLoader::UnloadLibrary(ms_dllRas);
429 ms_dllRas = 0;
430 ms_nDllCount = 0;
431
432 return;
433 }
434 }
435 }
436
437 // enable auto check by default
438 EnableAutoCheckOnlineStatus(0);
439 }
440
441 wxDialUpManagerMSW::~wxDialUpManagerMSW()
442 {
443 CleanUpThreadData();
444
445 if ( !--ms_nDllCount )
446 {
447 // unload the RAS library
448 wxDllLoader::UnloadLibrary(ms_dllRas);
449 ms_dllRas = 0;
450 }
451 }
452
453 // ----------------------------------------------------------------------------
454 // helper functions
455 // ----------------------------------------------------------------------------
456
457 wxString wxDialUpManagerMSW::GetErrorString(DWORD error)
458 {
459 wxChar buffer[512]; // this should be more than enough according to MS docs
460 DWORD dwRet = ms_pfnRasGetErrorString(error, buffer, WXSIZEOF(buffer));
461 switch ( dwRet )
462 {
463 case ERROR_INVALID_PARAMETER:
464 // this was a standard Win32 error probably
465 return wxString(wxSysErrorMsg(error));
466
467 default:
468 {
469 wxLogSysError(dwRet,
470 _("Failed to retrieve text of RAS error message"));
471
472 wxString msg;
473 msg.Printf(_("unknown error (error code %08x)."), error);
474 return msg;
475 }
476
477 case 0:
478 // we want the error message to start from a lower case letter
479 buffer[0] = wxTolower(buffer[0]);
480
481 return wxString(buffer);
482 }
483 }
484
485 HRASCONN wxDialUpManagerMSW::FindActiveConnection()
486 {
487 // enumerate connections
488 DWORD cbBuf = sizeof(RASCONN);
489 LPRASCONN lpRasConn = (LPRASCONN)malloc(cbBuf);
490 if ( !lpRasConn )
491 {
492 // out of memory
493 return 0;
494 }
495
496 lpRasConn->dwSize = sizeof(RASCONN);
497
498 DWORD nConnections = 0;
499 DWORD dwRet = ERROR_BUFFER_TOO_SMALL;
500
501 while ( dwRet == ERROR_BUFFER_TOO_SMALL )
502 {
503 dwRet = ms_pfnRasEnumConnections(lpRasConn, &cbBuf, &nConnections);
504
505 if ( dwRet == ERROR_BUFFER_TOO_SMALL )
506 {
507 LPRASCONN lpRasConnOld = lpRasConn;
508 lpRasConn = (LPRASCONN)realloc(lpRasConn, cbBuf);
509 if ( !lpRasConn )
510 {
511 // out of memory
512 free(lpRasConnOld);
513
514 return 0;
515 }
516 }
517 else if ( dwRet == 0 )
518 {
519 // ok, success
520 break;
521 }
522 else
523 {
524 // an error occured
525 wxLogError(_("Cannot find active dialup connection: %s"),
526 GetErrorString(dwRet).c_str());
527 return 0;
528 }
529 }
530
531 HRASCONN hrasconn;
532
533 switch ( nConnections )
534 {
535 case 0:
536 // no connections
537 hrasconn = 0;
538 break;
539
540 default:
541 // more than 1 connection - we don't know what to do with this
542 // case, so give a warning but continue (taking the first
543 // connection) - the warning is really needed because this function
544 // is used, for example, to select the connection to hang up and so
545 // we may hang up the wrong connection here...
546 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
547 // fall through
548
549 case 1:
550 // exactly 1 connection, great
551 hrasconn = lpRasConn->hrasconn;
552 }
553
554 free(lpRasConn);
555
556 return hrasconn;
557 }
558
559 void wxDialUpManagerMSW::CleanUpThreadData()
560 {
561 if ( m_hThread )
562 {
563 if ( !SetEvent(m_data.hEventQuit) )
564 {
565 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
566 }
567
568 CloseHandle(m_hThread);
569
570 m_hThread = 0;
571 }
572
573 if ( m_data.hWnd )
574 {
575 DestroyWindow(m_data.hWnd);
576
577 m_data.hWnd = 0;
578 }
579
580 if ( m_data.hEventQuit )
581 {
582 CloseHandle(m_data.hEventQuit);
583
584 m_data.hEventQuit = 0;
585 }
586
587 if ( m_data.hEventRas )
588 {
589 CloseHandle(m_data.hEventRas);
590
591 m_data.hEventRas = 0;
592 }
593 }
594
595 // ----------------------------------------------------------------------------
596 // connection status
597 // ----------------------------------------------------------------------------
598
599 void wxDialUpManagerMSW::CheckRasStatus()
600 {
601 // use int, not bool to compare with -1
602 int isConnected = FindActiveConnection() != 0;
603 if ( isConnected != ms_isConnected )
604 {
605 if ( ms_isConnected != -1 )
606 {
607 // notify the program
608 NotifyApp(isConnected != 0);
609 }
610 // else: it's the first time we're called, just update the flag
611
612 ms_isConnected = isConnected;
613 }
614 }
615
616 void wxDialUpManagerMSW::NotifyApp(bool connected, bool fromOurselves) const
617 {
618 wxDialUpEvent event(connected, fromOurselves);
619 (void)wxTheApp->ProcessEvent(event);
620 }
621
622 // this function is called whenever the status of any RAS connection on this
623 // machine changes by RAS itself
624 void wxDialUpManagerMSW::OnConnectStatusChange()
625 {
626 // we know that status changed, but we don't know whether we're connected
627 // or not - so find it out
628 CheckRasStatus();
629 }
630
631 // this function is called by our callback which we give to RasDial() when
632 // calling it asynchronously
633 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate,
634 DWORD dwError)
635 {
636 if ( !GetDialer() )
637 {
638 // this probably means that CancelDialing() was called and we get
639 // "disconnected" notification
640 return;
641 }
642
643 // we're only interested in 2 events: connected and disconnected
644 if ( dwError )
645 {
646 wxLogError(_("Failed to establish dialup connection: %s"),
647 GetErrorString(dwError).c_str());
648
649 // we should still call RasHangUp() if we got a non 0 connection
650 if ( ms_hRasConnection )
651 {
652 ms_pfnRasHangUp(ms_hRasConnection);
653 ms_hRasConnection = 0;
654 }
655
656 ms_dialer = NULL;
657
658 NotifyApp(FALSE /* !connected */, TRUE /* we dialed ourselves */);
659 }
660 else if ( rasconnstate == RASCS_Connected )
661 {
662 ms_isConnected = TRUE;
663 ms_dialer = NULL;
664
665 NotifyApp(TRUE /* connected */, TRUE /* we dialed ourselves */);
666 }
667 }
668
669 // ----------------------------------------------------------------------------
670 // implementation of wxDialUpManager functions
671 // ----------------------------------------------------------------------------
672
673 bool wxDialUpManagerMSW::IsOk() const
674 {
675 return ms_dllRas != 0;
676 }
677
678 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString& names) const
679 {
680 // fetch the entries
681 DWORD size = sizeof(RASENTRYNAME);
682 RASENTRYNAME *rasEntries = (RASENTRYNAME *)malloc(size);
683 rasEntries->dwSize = sizeof(RASENTRYNAME);
684
685 DWORD nEntries;
686 DWORD dwRet;
687 do
688 {
689 dwRet = ms_pfnRasEnumEntries
690 (
691 NULL, // reserved
692 NULL, // default phone book (or all)
693 rasEntries, // [out] buffer for the entries
694 &size, // [in/out] size of the buffer
695 &nEntries // [out] number of entries fetched
696 );
697
698 if ( dwRet == ERROR_BUFFER_TOO_SMALL )
699 {
700 // reallocate the buffer
701 rasEntries = (RASENTRYNAME *)realloc(rasEntries, size);
702 }
703 else if ( dwRet != 0 )
704 {
705 // some other error - abort
706 wxLogError(_("Failed to get ISP names: %s"),
707 GetErrorString(dwRet).c_str());
708
709 free(rasEntries);
710
711 return 0u;
712 }
713 }
714 while ( dwRet != 0 );
715
716 // process them
717 names.Empty();
718 for ( size_t n = 0; n < (size_t)nEntries; n++ )
719 {
720 names.Add(rasEntries[n].szEntryName);
721 }
722
723 free(rasEntries);
724
725 // return the number of entries
726 return names.GetCount();
727 }
728
729 bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP,
730 const wxString& username,
731 const wxString& password,
732 bool async)
733 {
734 // check preconditions
735 wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") );
736
737 if ( ms_hRasConnection )
738 {
739 wxFAIL_MSG(wxT("there is already an active connection"));
740
741 return TRUE;
742 }
743
744 // get the default ISP if none given
745 wxString entryName(nameOfISP);
746 if ( !entryName )
747 {
748 wxArrayString names;
749 size_t count = GetISPNames(names);
750 switch ( count )
751 {
752 case 0:
753 // no known ISPs, abort
754 wxLogError(_("Failed to connect: no ISP to dial."));
755
756 return FALSE;
757
758 case 1:
759 // only one ISP, choose it
760 entryName = names[0u];
761 break;
762
763 default:
764 // several ISPs, let the user choose
765 {
766 wxString *strings = new wxString[count];
767 for ( size_t i = 0; i < count; i++ )
768 {
769 strings[i] = names[i];
770 }
771
772 entryName = wxGetSingleChoice
773 (
774 _("Choose ISP to dial"),
775 _("Please choose which ISP do you want to connect to"),
776 count,
777 strings
778 );
779
780 delete [] strings;
781
782 if ( !entryName )
783 {
784 // cancelled by user
785 return FALSE;
786 }
787 }
788 }
789 }
790
791 RASDIALPARAMS rasDialParams;
792 rasDialParams.dwSize = sizeof(rasDialParams);
793 wxStrncpy(rasDialParams.szEntryName, entryName, RAS_MaxEntryName);
794
795 // do we have the username and password?
796 if ( !username || !password )
797 {
798 BOOL gotPassword;
799 DWORD dwRet = ms_pfnRasGetEntryDialParams
800 (
801 NULL, // default phonebook
802 &rasDialParams, // [in/out] the params of this entry
803 &gotPassword // [out] did we get password?
804 );
805
806 if ( dwRet != 0 )
807 {
808 wxLogError(_("Failed to connect: missing username/password."));
809
810 return FALSE;
811 }
812 }
813 else
814 {
815 wxStrncpy(rasDialParams.szUserName, username, UNLEN);
816 wxStrncpy(rasDialParams.szPassword, password, PWLEN);
817 }
818
819 // default values for other fields
820 rasDialParams.szPhoneNumber[0] = '\0';
821 rasDialParams.szCallbackNumber[0] = '\0';
822 rasDialParams.szCallbackNumber[0] = '\0';
823
824 rasDialParams.szDomain[0] = '*';
825 rasDialParams.szDomain[1] = '\0';
826
827 // apparently, this is not really necessary - passing NULL instead of the
828 // phone book has the same effect
829 #if 0
830 wxString phoneBook;
831 if ( wxGetOsVersion() == wxWINDOWS_NT )
832 {
833 // first get the length
834 UINT nLen = ::GetSystemDirectory(NULL, 0);
835 nLen++;
836
837 if ( !::GetSystemDirectory(phoneBook.GetWriteBuf(nLen), nLen) )
838 {
839 wxLogSysError(_("Cannot find the location of address book file"));
840 }
841
842 phoneBook.UngetWriteBuf();
843
844 // this is the default phone book
845 phoneBook << "\\ras\\rasphone.pbk";
846 }
847 #endif // 0
848
849 // TODO may be we should disable auto check while async dialing is in
850 // progress?
851
852 ms_dialer = this;
853
854 DWORD dwRet = ms_pfnRasDial
855 (
856 (LPRASDIALEXTENSIONS)NULL, // no extended features
857 NULL, // default phone book file (NT only)
858 &rasDialParams,
859 0, // use callback for notifications
860 async ? wxRasDialFunc // the callback
861 : 0, // no notifications - sync operation
862 &ms_hRasConnection
863 );
864
865 if ( dwRet != 0 )
866 {
867 wxLogError(_("Failed to %s dialup connection: %s"),
868 wxString(async ? _("initiate") : _("establish")).c_str(),
869 GetErrorString(dwRet).c_str());
870
871 // we should still call RasHangUp() if we got a non 0 connection
872 if ( ms_hRasConnection )
873 {
874 ms_pfnRasHangUp(ms_hRasConnection);
875 ms_hRasConnection = 0;
876 }
877
878 ms_dialer = NULL;
879
880 return FALSE;
881 }
882
883 // for async dialing, we're not yet connected
884 if ( !async )
885 {
886 ms_isConnected = TRUE;
887 }
888
889 return TRUE;
890 }
891
892 bool wxDialUpManagerMSW::IsDialing() const
893 {
894 return GetDialer() != NULL;
895 }
896
897 bool wxDialUpManagerMSW::CancelDialing()
898 {
899 if ( !GetDialer() )
900 {
901 // silently ignore
902 return FALSE;
903 }
904
905 wxASSERT_MSG( ms_hRasConnection, wxT("dialing but no connection?") );
906
907 ms_dialer = NULL;
908
909 return HangUp();
910 }
911
912 bool wxDialUpManagerMSW::HangUp()
913 {
914 wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") );
915
916 // we may terminate either the connection we initiated or another one which
917 // is active now
918 HRASCONN hRasConn;
919 if ( ms_hRasConnection )
920 {
921 hRasConn = ms_hRasConnection;
922
923 ms_hRasConnection = 0;
924 }
925 else
926 {
927 hRasConn = FindActiveConnection();
928 }
929
930 if ( !hRasConn )
931 {
932 wxLogError(_("Cannot hang up - no active dialup connection."));
933
934 return FALSE;
935 }
936
937 DWORD dwRet = ms_pfnRasHangUp(hRasConn);
938 if ( dwRet != 0 )
939 {
940 wxLogError(_("Failed to terminate the dialup connection: %s"),
941 GetErrorString(dwRet).c_str());
942 }
943
944 ms_isConnected = FALSE;
945
946 return TRUE;
947 }
948
949 bool wxDialUpManagerMSW::IsAlwaysOnline() const
950 {
951 // we cache the result (presumably this won't change while the program is
952 // running!)
953 if ( ms_isAlwaysOnline != -1 )
954 {
955 return ms_isAlwaysOnline != 0;
956 }
957
958 // try to use WinInet function first
959 bool ok;
960 wxDllType hDll = wxDllLoader::LoadLibrary(_T("WININET"), &ok);
961 if ( ok )
962 {
963 typedef BOOL (*INTERNETGETCONNECTEDSTATE)(LPDWORD, DWORD);
964 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState;
965
966 #define RESOLVE_FUNCTION(type, name) \
967 pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name))
968
969 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE, InternetGetConnectedState);
970
971 if ( pfnInternetGetConnectedState )
972 {
973 DWORD flags = 0;
974 if ( pfnInternetGetConnectedState(&flags, 0 /* reserved */) )
975 {
976 // there is some connection to the net, see of which type
977 ms_isAlwaysOnline = (flags & INTERNET_CONNECTION_LAN != 0) ||
978 (flags & INTERNET_CONNECTION_PROXY != 0);
979 }
980 else
981 {
982 // no Internet connection at all
983 ms_isAlwaysOnline = FALSE;
984 }
985 }
986
987 wxDllLoader::UnloadLibrary(hDll);
988 }
989
990 // did we succeed with WinInet? if not, try something else
991 if ( ms_isAlwaysOnline == -1 )
992 {
993 if ( !IsOnline() )
994 {
995 // definitely no permanent connection because we are not connected
996 // now
997 ms_isAlwaysOnline = FALSE;
998 }
999 else
1000 {
1001 // of course, having a modem doesn't prevent us from having a
1002 // permanent connection as well, but we have to guess somehow and
1003 // it's probably more common that a system connected via a modem
1004 // doesn't have any other net access, so:
1005 ms_isAlwaysOnline = FALSE;
1006 }
1007 }
1008
1009 wxASSERT_MSG( ms_isAlwaysOnline != -1, wxT("logic error") );
1010
1011 return ms_isAlwaysOnline != 0;
1012 }
1013
1014 bool wxDialUpManagerMSW::IsOnline() const
1015 {
1016 wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") );
1017
1018 if ( ms_userSpecifiedOnlineStatus != -1 )
1019 {
1020 // user specified flag overrides our logic
1021 return ms_userSpecifiedOnlineStatus != 0;
1022 }
1023 else
1024 {
1025 // return TRUE if there is at least one active connection
1026 return FindActiveConnection() != 0;
1027 }
1028 }
1029
1030 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline)
1031 {
1032 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1033
1034 ms_userSpecifiedOnlineStatus = isOnline;
1035 }
1036
1037 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds)
1038 {
1039 wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") );
1040
1041 bool ok = ms_pfnRasConnectionNotification != 0;
1042
1043 if ( ok )
1044 {
1045 // we're running under NT 4.0, Windows 98 or later and can use
1046 // RasConnectionNotification() to be notified by a secondary thread
1047
1048 // first, see if we don't have this thread already running
1049 if ( m_hThread != 0 )
1050 {
1051 DWORD dwSuspendCount = 2;
1052 while ( dwSuspendCount > 1 )
1053 {
1054 dwSuspendCount = ResumeThread(m_hThread);
1055 if ( dwSuspendCount == (DWORD)-1 )
1056 {
1057 wxLogLastError(wxT("ResumeThread(RasThread)"));
1058
1059 ok = FALSE;
1060 }
1061 }
1062
1063 if ( ok )
1064 {
1065 return TRUE;
1066 }
1067 }
1068 }
1069
1070 // create all the stuff we need to be notified about RAS connection
1071 // status change
1072
1073 if ( ok )
1074 {
1075 // first create an event to wait on
1076 m_data.hEventRas = CreateEvent
1077 (
1078 NULL, // security attribute (default)
1079 FALSE, // manual reset (not)
1080 FALSE, // initial state (not signaled)
1081 NULL // name (no)
1082 );
1083 if ( !m_data.hEventRas )
1084 {
1085 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1086
1087 ok = FALSE;
1088 }
1089 }
1090
1091 if ( ok )
1092 {
1093 // create the event we use to quit the thread
1094 m_data.hEventQuit = CreateEvent(NULL, FALSE, FALSE, NULL);
1095 if ( !m_data.hEventQuit )
1096 {
1097 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1098
1099 CleanUpThreadData();
1100
1101 ok = FALSE;
1102 }
1103 }
1104
1105 if ( ok && !ms_hwndRas )
1106 {
1107 // create a hidden window to receive notification about connections
1108 // status change
1109 extern const wxChar *wxPanelClassName;
1110 ms_hwndRas = ::CreateWindow(wxPanelClassName, NULL,
1111 0, 0, 0, 0,
1112 0, NULL,
1113 (HMENU)NULL, wxGetInstance(), 0);
1114 if ( !ms_hwndRas )
1115 {
1116 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1117
1118 CleanUpThreadData();
1119
1120 ok = FALSE;
1121 }
1122
1123 // and subclass it
1124 FARPROC windowProc = MakeProcInstance
1125 (
1126 (FARPROC)wxRasStatusWindowProc,
1127 wxGetInstance()
1128 );
1129
1130 ::SetWindowLong(ms_hwndRas, GWL_WNDPROC, (LONG) windowProc);
1131 }
1132
1133 m_data.hWnd = ms_hwndRas;
1134
1135 if ( ok )
1136 {
1137 // start the secondary thread
1138 m_data.dialUpManager = this;
1139
1140 DWORD tid;
1141 m_hThread = CreateThread
1142 (
1143 NULL,
1144 0,
1145 (LPTHREAD_START_ROUTINE)wxRasMonitorThread,
1146 (void *)&m_data,
1147 0,
1148 &tid
1149 );
1150
1151 if ( !m_hThread )
1152 {
1153 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1154
1155 CleanUpThreadData();
1156 }
1157 }
1158
1159 if ( ok )
1160 {
1161 // start receiving RAS notifications
1162 DWORD dwRet = ms_pfnRasConnectionNotification
1163 (
1164 (HRASCONN)INVALID_HANDLE_VALUE,
1165 m_data.hEventRas,
1166 3 /* RASCN_Connection | RASCN_Disconnection */
1167 );
1168
1169 if ( dwRet != 0 )
1170 {
1171 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1172 GetErrorString(dwRet).c_str());
1173
1174 CleanUpThreadData();
1175 }
1176 else
1177 {
1178 return TRUE;
1179 }
1180 }
1181
1182 // we're running under Windows 95 and have to poll ourselves
1183 // (or, alternatively, the code above for NT/98 failed)
1184 m_timerStatusPolling.Stop();
1185 if ( nSeconds == 0 )
1186 {
1187 // default value
1188 nSeconds = 60;
1189 }
1190 m_timerStatusPolling.Start(nSeconds * 1000);
1191
1192 return TRUE;
1193 }
1194
1195 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1196 {
1197 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1198
1199 if ( m_hThread )
1200 {
1201 // we have running secondary thread, it's just enough to suspend it
1202 if ( SuspendThread(m_hThread) == (DWORD)-1 )
1203 {
1204 wxLogLastError(wxT("SuspendThread(RasThread)"));
1205 }
1206 }
1207 else
1208 {
1209 // even simpler - just stop the timer
1210 m_timerStatusPolling.Stop();
1211 }
1212 }
1213
1214 // ----------------------------------------------------------------------------
1215 // stubs which don't do anything in MSW version
1216 // ----------------------------------------------------------------------------
1217
1218 void wxDialUpManagerMSW::SetWellKnownHost(const wxString& WXUNUSED(hostname),
1219 int WXUNUSED(port))
1220 {
1221 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1222
1223 // nothing to do - we don't use this
1224 }
1225
1226 void wxDialUpManagerMSW::SetConnectCommand(const wxString& WXUNUSED(dial),
1227 const wxString& WXUNUSED(hangup))
1228 {
1229 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1230
1231 // nothing to do - we don't use this
1232 }
1233
1234 // ----------------------------------------------------------------------------
1235 // callbacks
1236 // ----------------------------------------------------------------------------
1237
1238 static DWORD wxRasMonitorThread(wxRasThreadData *data)
1239 {
1240 HANDLE handles[2];
1241 handles[0] = data->hEventRas;
1242 handles[1] = data->hEventQuit;
1243
1244 bool cont = TRUE;
1245 while ( cont )
1246 {
1247 DWORD dwRet = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1248
1249 switch ( dwRet )
1250 {
1251 case WAIT_OBJECT_0:
1252 // RAS connection status changed
1253 SendMessage(data->hWnd, wxWM_RAS_STATUS_CHANGED,
1254 0, (LPARAM)data);
1255 break;
1256
1257 case WAIT_OBJECT_0 + 1:
1258 cont = FALSE;
1259 break;
1260
1261 case WAIT_FAILED:
1262 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1263 break;
1264 }
1265 }
1266
1267 return 0;
1268 }
1269
1270 static LRESULT APIENTRY wxRasStatusWindowProc(HWND hWnd, UINT message,
1271 WPARAM wParam, LPARAM lParam)
1272 {
1273 if ( message == wxWM_RAS_STATUS_CHANGED )
1274 {
1275 wxRasThreadData *data = (wxRasThreadData *)lParam;
1276 data->dialUpManager->OnConnectStatusChange();
1277 }
1278 else if ( message == wxWM_RAS_DIALING_PROGRESS )
1279 {
1280 wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer();
1281
1282 dialUpManager->OnDialProgress((RASCONNSTATE)wParam, lParam);
1283 }
1284
1285 return 0;
1286 }
1287
1288 static void WINAPI wxRasDialFunc(UINT unMsg,
1289 RASCONNSTATE rasconnstate,
1290 DWORD dwError)
1291 {
1292 wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer();
1293
1294 wxCHECK_RET( dialUpManager, wxT("who started to dial then?") );
1295
1296 SendMessage(dialUpManager->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS,
1297 rasconnstate, dwError);
1298 }
1299
1300 #endif
1301 // __BORLANDC__
1302 #endif // wxUSE_DIALUP_MANAGER