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