1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dialup.cpp
3 // Purpose: MSW implementation of network/dialup classes and functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
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
33 #if wxUSE_DIALUP_MANAGER
41 #include "wx/msw/private.h" // must be before #include "dynlib.h"
43 #if !wxUSE_DYNLIB_CLASS
44 #error You need wxUSE_DYNLIB_CLASS to be 1 to compile dialup.cpp.
47 #include "wx/dynlib.h"
49 #include "wx/dialup.h"
51 // Doesn't yet compile under VC++ 4, BC++, mingw, Watcom C++: no wininet.h
52 #if !defined(__BORLANDC__) && !defined(__GNUWIN32_OLD__) && !defined(__GNUWIN32__) && !defined(__WATCOMC__) && ! (defined(__VISUALC__) && (__VISUALC__ < 1020))
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // this message is sent by the secondary thread when RAS status changes
64 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
65 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // the signatures of RAS functions: all this is quite heavy, but we must do it
72 // to allow running wxWin programs on machine which don't have RAS installed
73 // (this does exist) - if we link with rasapi32.lib, the program will fail on
74 // startup because of the missing DLL...
77 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
78 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
79 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
80 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
81 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
82 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
83 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
84 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
85 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
86 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
87 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
88 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
89 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
90 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
91 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
92 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
93 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
94 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
95 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
97 static const char gs_funcSuffix
= 'A';
99 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
100 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
101 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
102 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
103 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
104 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
105 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
106 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
107 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
108 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
109 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
110 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
111 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
112 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
113 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
114 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
115 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
116 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
117 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
119 static const char gs_funcSuffix
= 'W';
120 #endif // ASCII/Unicode
122 // structure passed to the secondary thread
123 struct wxRasThreadData
128 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
129 dialUpManager
= NULL
;
132 HWND hWnd
; // window to send notifications to
133 HANDLE hEventRas
, // event which RAS signals when status changes
134 hEventQuit
; // event which we signal when we terminate
136 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
139 // ----------------------------------------------------------------------------
140 // wxDialUpManager class for MSW
141 // ----------------------------------------------------------------------------
143 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
147 wxDialUpManagerMSW();
148 virtual ~wxDialUpManagerMSW();
150 // implement base class pure virtuals
151 virtual bool IsOk() const;
152 virtual size_t GetISPNames(wxArrayString
& names
) const;
153 virtual bool Dial(const wxString
& nameOfISP
,
154 const wxString
& username
,
155 const wxString
& password
,
157 virtual bool IsDialing() const;
158 virtual bool CancelDialing();
159 virtual bool HangUp();
160 virtual bool IsAlwaysOnline() const;
161 virtual bool IsOnline() const;
162 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
163 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
164 virtual void DisableAutoCheckOnlineStatus();
165 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
166 virtual void SetConnectCommand(const wxString
& commandDial
,
167 const wxString
& commandHangup
);
170 void CheckRasStatus();
172 // for wxRasStatusWindowProc
173 void OnConnectStatusChange();
174 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
177 static HWND
GetRasWindow() { return ms_hwndRas
; }
178 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
181 // return the error string for the given RAS error code
182 static wxString
GetErrorString(DWORD error
);
184 // find the (first) handle of the active connection
185 static HRASCONN
FindActiveConnection();
187 // notify the application about status change
188 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
190 // destroy the thread data and the thread itself
191 void CleanUpThreadData();
193 // timer used for polling RAS status
194 class RasTimer
: public wxTimer
197 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
198 { m_dialUpManager
= dialUpManager
; }
200 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
203 wxDialUpManagerMSW
*m_dialUpManager
;
204 } m_timerStatusPolling
;
206 // thread handle for the thread sitting on connection change event
209 // data used by this thread and our hidden window to send messages between
211 wxRasThreadData m_data
;
213 // the hidden window we use for passing messages between threads
214 static HWND ms_hwndRas
;
216 // the handle of the connection we initiated or 0 if none
217 static HRASCONN ms_hRasConnection
;
219 // the use count of rasapi32.dll
220 static int ms_nDllCount
;
222 // the handle of rasapi32.dll when it's loaded
223 static wxDllType ms_dllRas
;
225 // the pointers to RAS functions
226 static RASDIAL ms_pfnRasDial
;
227 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
228 static RASENUMENTRIES ms_pfnRasEnumEntries
;
229 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
230 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
231 static RASHANGUP ms_pfnRasHangUp
;
232 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
233 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
234 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
235 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
236 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
237 static RASENUMDEVICES ms_pfnRasEnumDevices
;
238 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
239 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
240 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
241 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
242 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
243 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
245 // this function is not supported by Win95
246 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
248 // if this flag is different from -1, it overrides IsOnline()
249 static int ms_userSpecifiedOnlineStatus
;
251 // this flag tells us if we're online
252 static int ms_isConnected
;
254 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
256 static int ms_isAlwaysOnline
;
258 // this flag tells us whether a call to RasDial() is in progress
259 static wxDialUpManagerMSW
*ms_dialer
;
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
267 WPARAM wParam
, LPARAM lParam
);
269 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
271 static void WINAPI
wxRasDialFunc(UINT unMsg
,
272 RASCONNSTATE rasconnstate
,
275 // ============================================================================
277 // ============================================================================
279 // ----------------------------------------------------------------------------
280 // init the static variables
281 // ----------------------------------------------------------------------------
283 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
285 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
287 int wxDialUpManagerMSW::ms_nDllCount
= 0;
288 wxDllType
wxDialUpManagerMSW::ms_dllRas
= 0;
290 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
291 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
292 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
293 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
294 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
295 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
296 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
297 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
298 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
299 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
300 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
301 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
302 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
303 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
304 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
305 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
306 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
307 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
308 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
310 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
311 int wxDialUpManagerMSW::ms_isConnected
= -1;
312 int wxDialUpManagerMSW::ms_isAlwaysOnline
= -1;
313 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
315 // ----------------------------------------------------------------------------
316 // ctor and dtor: the dynamic linking happens here
317 // ----------------------------------------------------------------------------
319 // the static creator function is implemented here
320 wxDialUpManager
*wxDialUpManager::Create()
322 return new wxDialUpManagerMSW
;
326 // warning about "'this' : used in base member initializer list" - so what?
327 #pragma warning(disable:4355)
330 wxDialUpManagerMSW::wxDialUpManagerMSW()
331 : m_timerStatusPolling(this)
333 // initialize our data
336 if ( !ms_nDllCount
++ )
338 // load the RAS library
339 ms_dllRas
= wxDllLoader::LoadLibrary("RASAPI32");
342 wxLogError(_("Dial up functions are unavailable because the "
343 "remote access service (RAS) is not installed "
344 "on this machine. Please install it."));
348 // resolve the functions we need
350 // this will contain the name of the function we failed to resolve
352 const char *funcName
= NULL
;
354 // get the function from rasapi32.dll and abort if it's not found
355 #define RESOLVE_RAS_FUNCTION(type, name) \
356 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
357 wxString(_T(#name)) + gs_funcSuffix); \
358 if ( !ms_pfn##name ) \
364 // a variant of above macro which doesn't abort if the function is
365 // not found in the DLL
366 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
367 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
368 wxString(_T(#name)) + gs_funcSuffix);
370 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
371 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
372 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
373 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
374 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
375 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
376 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
378 // suppress wxDllLoader messages about missing (non essential)
383 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
384 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
385 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
386 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
387 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
388 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
389 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
390 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
391 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
392 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
393 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
394 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
397 // keep your preprocessor name space clean
398 #undef RESOLVE_RAS_FUNCTION
399 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
404 wxLogError(_("The version of remote access service (RAS) "
405 "installed on this machine is too old, please "
406 "upgrade (the following required function is "
407 "missing: %s)."), funcName
);
409 wxDllLoader::UnloadLibrary(ms_dllRas
);
418 // enable auto check by default
419 EnableAutoCheckOnlineStatus(0);
422 wxDialUpManagerMSW::~wxDialUpManagerMSW()
426 if ( !--ms_nDllCount
)
428 // unload the RAS library
429 wxDllLoader::UnloadLibrary(ms_dllRas
);
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
440 wxChar buffer
[512]; // this should be more than enough according to MS docs
441 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
444 case ERROR_INVALID_PARAMETER
:
445 // this was a standard Win32 error probably
446 return wxString(wxSysErrorMsg(error
));
450 wxLogSysError(dwRet
, _("Failed to retrieve text of RAS "
454 msg
.Printf(_("unknown error (error code %08x)."), error
);
459 // we want the error message to start from a lower case letter
460 buffer
[0] = wxTolower(buffer
[0]);
462 return wxString(buffer
);
466 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
468 // enumerate connections
469 DWORD cbBuf
= sizeof(RASCONN
);
470 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
477 lpRasConn
->dwSize
= sizeof(RASCONN
);
479 DWORD nConnections
= 0;
480 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
482 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
484 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
486 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
488 LPRASCONN lpRasConnOld
= lpRasConn
;
489 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
498 else if ( dwRet
== 0 )
506 wxLogError(_("Cannot find active dialup connection: %s"),
507 GetErrorString(dwRet
));
514 switch ( nConnections
)
522 // more than 1 connection - we don't know what to do with this
523 // case, so give a warning but continue (taking the first
524 // connection) - the warning is really needed because this function
525 // is used, for example, to select the connection to hang up and so
526 // we may hang up the wrong connection here...
527 wxLogWarning(_("Several active dialup connections found, "
528 "choosing one randomly."));
532 // exactly 1 connection, great
533 hrasconn
= lpRasConn
->hrasconn
;
541 void wxDialUpManagerMSW::CleanUpThreadData()
545 if ( !SetEvent(m_data
.hEventQuit
) )
547 wxLogLastError("SetEvent(RasThreadQuit)");
550 CloseHandle(m_hThread
);
557 DestroyWindow(m_data
.hWnd
);
562 if ( m_data
.hEventQuit
)
564 CloseHandle(m_data
.hEventQuit
);
566 m_data
.hEventQuit
= 0;
569 if ( m_data
.hEventRas
)
571 CloseHandle(m_data
.hEventRas
);
573 m_data
.hEventRas
= 0;
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 void wxDialUpManagerMSW::CheckRasStatus()
583 // use int, not bool to compare with -1
584 int isConnected
= FindActiveConnection() != 0;
585 if ( isConnected
!= ms_isConnected
)
587 if ( ms_isConnected
!= -1 )
589 // notify the program
590 NotifyApp(isConnected
!= 0);
592 // else: it's the first time we're called, just update the flag
594 ms_isConnected
= isConnected
;
598 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
600 wxDialUpEvent
event(connected
, fromOurselves
);
601 (void)wxTheApp
->ProcessEvent(event
);
604 // this function is called whenever the status of any RAS connection on this
605 // machine changes by RAS itself
606 void wxDialUpManagerMSW::OnConnectStatusChange()
608 // we know that status changed, but we don't know whether we're connected
609 // or not - so find it out
613 // this function is called by our callback which we give to RasDial() when
614 // calling it asynchronously
615 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
620 // this probably means that CancelDialing() was called and we get
621 // "disconnected" notification
625 // we're only interested in 2 events: connected and disconnected
628 wxLogError(_("Failed to establish dialup connection: %s"),
629 GetErrorString(dwError
));
631 // we should still call RasHangUp() if we got a non 0 connection
632 if ( ms_hRasConnection
)
634 ms_pfnRasHangUp(ms_hRasConnection
);
635 ms_hRasConnection
= 0;
640 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
642 else if ( rasconnstate
== RASCS_Connected
)
644 ms_isConnected
= TRUE
;
647 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
651 // ----------------------------------------------------------------------------
652 // implementation of wxDialUpManager functions
653 // ----------------------------------------------------------------------------
655 bool wxDialUpManagerMSW::IsOk() const
657 return ms_dllRas
!= 0;
660 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
663 DWORD size
= sizeof(RASENTRYNAME
);
664 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
665 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
671 dwRet
= ms_pfnRasEnumEntries
674 NULL
, // default phone book (or all)
675 rasEntries
, // [out] buffer for the entries
676 &size
, // [in/out] size of the buffer
677 &nEntries
// [out] number of entries fetched
680 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
682 // reallocate the buffer
683 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
685 else if ( dwRet
!= 0 )
687 // some other error - abort
688 wxLogError(_("Failed to get ISP names: %s"), GetErrorString(dwRet
));
695 while ( dwRet
!= 0 );
699 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
701 names
.Add(rasEntries
[n
].szEntryName
);
706 // return the number of entries
707 return names
.GetCount();
710 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
711 const wxString
& username
,
712 const wxString
& password
,
715 // check preconditions
716 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
718 if ( ms_hRasConnection
)
720 wxFAIL_MSG(wxT("there is already an active connection"));
725 // get the default ISP if none given
726 wxString
entryName(nameOfISP
);
730 size_t count
= GetISPNames(names
);
734 // no known ISPs, abort
735 wxLogError(_("Failed to connect: no ISP to dial."));
740 // only one ISP, choose it
741 entryName
= names
[0u];
745 // several ISPs, let the user choose
747 wxString
*strings
= new wxString
[count
];
748 for ( size_t i
= 0; i
< count
; i
++ )
750 strings
[i
] = names
[i
];
753 entryName
= wxGetSingleChoice
755 _("Choose ISP to dial"),
756 _("Please choose which ISP do you want to "
773 RASDIALPARAMS rasDialParams
;
774 rasDialParams
.dwSize
= sizeof(rasDialParams
);
775 strncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
777 // do we have the username and password?
778 if ( !username
|| !password
)
781 DWORD dwRet
= ms_pfnRasGetEntryDialParams
783 NULL
, // default phonebook
784 &rasDialParams
, // [in/out] the params of this entry
785 &gotPassword
// [out] did we get password?
790 wxLogError(_("Failed to connect: missing username/password."));
797 strncpy(rasDialParams
.szUserName
, username
, UNLEN
);
798 strncpy(rasDialParams
.szPassword
, password
, PWLEN
);
801 // default values for other fields
802 rasDialParams
.szPhoneNumber
[0] = '\0';
803 rasDialParams
.szCallbackNumber
[0] = '\0';
804 rasDialParams
.szCallbackNumber
[0] = '\0';
806 rasDialParams
.szDomain
[0] = '*';
807 rasDialParams
.szDomain
[1] = '\0';
809 // apparently, this is not really necessary - passing NULL instead of the
810 // phone book has the same effect
813 if ( wxGetOsVersion() == wxWINDOWS_NT
)
815 // first get the length
816 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
819 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
821 wxLogSysError(_("Cannot find the location of address book file"));
824 phoneBook
.UngetWriteBuf();
826 // this is the default phone book
827 phoneBook
<< "\\ras\\rasphone.pbk";
831 // TODO may be we should disable auto check while async dialing is in
836 DWORD dwRet
= ms_pfnRasDial
838 (LPRASDIALEXTENSIONS
)NULL
, // no extended features
839 NULL
, // default phone book file (NT only)
841 0, // use callback for notifications
842 async
? wxRasDialFunc
// the callback
843 : 0, // no notifications - sync operation
849 wxLogError(_("Failed to %s dialup connection: %s"),
850 async
? _("initiate") : _("establish"),
851 GetErrorString(dwRet
));
853 // we should still call RasHangUp() if we got a non 0 connection
854 if ( ms_hRasConnection
)
856 ms_pfnRasHangUp(ms_hRasConnection
);
857 ms_hRasConnection
= 0;
865 // for async dialing, we're not yet connected
868 ms_isConnected
= TRUE
;
874 bool wxDialUpManagerMSW::IsDialing() const
876 return GetDialer() != NULL
;
879 bool wxDialUpManagerMSW::CancelDialing()
887 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
894 bool wxDialUpManagerMSW::HangUp()
896 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
898 // we may terminate either the connection we initiated or another one which
901 if ( ms_hRasConnection
)
903 hRasConn
= ms_hRasConnection
;
905 ms_hRasConnection
= 0;
909 hRasConn
= FindActiveConnection();
914 wxLogError(_("Cannot hang up - no active dialup connection."));
919 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
922 wxLogError(_("Failed to terminate the dialup connection: %s"),
923 GetErrorString(dwRet
));
926 ms_isConnected
= FALSE
;
931 bool wxDialUpManagerMSW::IsAlwaysOnline() const
933 // we cache the result (presumably this won't change while the program is
935 if ( ms_isAlwaysOnline
!= -1 )
937 return ms_isAlwaysOnline
!= 0;
940 // try to use WinInet function first
942 wxDllType hDll
= wxDllLoader::LoadLibrary(_T("WININET"), &ok
);
945 typedef BOOL (*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
946 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
948 #define RESOLVE_FUNCTION(type, name) \
949 pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name))
951 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
953 if ( pfnInternetGetConnectedState
)
956 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
958 // there is some connection to the net, see of which type
959 ms_isAlwaysOnline
= (flags
& INTERNET_CONNECTION_LAN
!= 0) ||
960 (flags
& INTERNET_CONNECTION_PROXY
!= 0);
962 wxLogMessage("InternetGetConnectedState() returned TRUE, "
963 "flags = %08x", flags
);
967 // no Internet connection at all
968 ms_isAlwaysOnline
= FALSE
;
972 wxDllLoader::UnloadLibrary(hDll
);
975 // did we succeed with WinInet? if not, try something else
976 if ( ms_isAlwaysOnline
== -1 )
980 // definitely no permanent connection because we are not connected
982 ms_isAlwaysOnline
= FALSE
;
986 // of course, having a modem doesn't prevent us from having a
987 // permanent connection as well, but we have to guess somehow and
988 // it's probably more common that a system connected via a modem
989 // doesn't have any other net access, so:
990 ms_isAlwaysOnline
= FALSE
;
994 wxASSERT_MSG( ms_isAlwaysOnline
!= -1, wxT("logic error") );
996 return ms_isAlwaysOnline
!= 0;
999 bool wxDialUpManagerMSW::IsOnline() const
1001 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1003 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1005 // user specified flag overrides our logic
1006 return ms_userSpecifiedOnlineStatus
!= 0;
1010 // return TRUE if there is at least one active connection
1011 return FindActiveConnection() != 0;
1015 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1017 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1019 ms_userSpecifiedOnlineStatus
= isOnline
;
1022 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1024 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1026 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1030 // we're running under NT 4.0, Windows 98 or later and can use
1031 // RasConnectionNotification() to be notified by a secondary thread
1033 // first, see if we don't have this thread already running
1034 if ( m_hThread
!= 0 )
1036 DWORD dwSuspendCount
= 2;
1037 while ( dwSuspendCount
> 1 )
1039 dwSuspendCount
= ResumeThread(m_hThread
);
1040 if ( dwSuspendCount
== (DWORD
)-1 )
1042 wxLogLastError("ResumeThread(RasThread)");
1055 // create all the stuff we need to be notified about RAS connection
1060 // first create an event to wait on
1061 m_data
.hEventRas
= CreateEvent
1063 NULL
, // security attribute (default)
1064 FALSE
, // manual reset (not)
1065 FALSE
, // initial state (not signaled)
1068 if ( !m_data
.hEventRas
)
1070 wxLogLastError("CreateEvent(RasStatus)");
1078 // create the event we use to quit the thread
1079 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1080 if ( !m_data
.hEventQuit
)
1082 wxLogLastError("CreateEvent(RasThreadQuit)");
1084 CleanUpThreadData();
1090 if ( ok
&& !ms_hwndRas
)
1092 // create a hidden window to receive notification about connections
1094 extern const wxChar
*wxPanelClassName
;
1095 ms_hwndRas
= ::CreateWindow(wxPanelClassName
, NULL
,
1098 (HMENU
)NULL
, wxGetInstance(), 0);
1101 wxLogLastError("CreateWindow(RasHiddenWindow)");
1103 CleanUpThreadData();
1109 FARPROC windowProc
= MakeProcInstance
1111 (FARPROC
)wxRasStatusWindowProc
,
1115 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1118 m_data
.hWnd
= ms_hwndRas
;
1122 // start the secondary thread
1123 m_data
.dialUpManager
= this;
1126 m_hThread
= CreateThread
1130 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1138 wxLogLastError("CreateThread(RasStatusThread)");
1140 CleanUpThreadData();
1146 // start receiving RAS notifications
1147 DWORD dwRet
= ms_pfnRasConnectionNotification
1149 (HRASCONN
)INVALID_HANDLE_VALUE
,
1151 3 /* RASCN_Connection | RASCN_Disconnection */
1156 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1157 GetErrorString(dwRet
));
1159 CleanUpThreadData();
1167 // we're running under Windows 95 and have to poll ourselves
1168 // (or, alternatively, the code above for NT/98 failed)
1169 m_timerStatusPolling
.Stop();
1170 if ( nSeconds
== 0 )
1175 m_timerStatusPolling
.Start(nSeconds
* 1000);
1180 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1182 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1186 // we have running secondary thread, it's just enough to suspend it
1187 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1189 wxLogLastError("SuspendThread(RasThread)");
1194 // even simpler - just stop the timer
1195 m_timerStatusPolling
.Stop();
1199 // ----------------------------------------------------------------------------
1200 // stubs which don't do anything in MSW version
1201 // ----------------------------------------------------------------------------
1203 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1206 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1208 // nothing to do - we don't use this
1211 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1212 const wxString
& WXUNUSED(hangup
))
1214 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1216 // nothing to do - we don't use this
1219 // ----------------------------------------------------------------------------
1221 // ----------------------------------------------------------------------------
1223 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1226 handles
[0] = data
->hEventRas
;
1227 handles
[1] = data
->hEventQuit
;
1232 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1237 // RAS connection status changed
1238 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1242 case WAIT_OBJECT_0
+ 1:
1247 wxLogLastError("WaitForMultipleObjects(RasMonitor)");
1255 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1256 WPARAM wParam
, LPARAM lParam
)
1258 if ( message
== wxWM_RAS_STATUS_CHANGED
)
1260 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1261 data
->dialUpManager
->OnConnectStatusChange();
1263 else if ( message
== wxWM_RAS_DIALING_PROGRESS
)
1265 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1267 dialUpManager
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1273 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1274 RASCONNSTATE rasconnstate
,
1277 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1279 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1281 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1282 rasconnstate
, dwError
);
1287 #endif // wxUSE_DIALUP_MANAGER