1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialup.cpp
3 // Purpose: MSW implementation of network/dialup classes and functions
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_DIALUP_MANAGER
28 #include "wx/dialup.h"
36 #include "wx/module.h"
39 #include "wx/generic/choicdgg.h"
41 #include "wx/msw/private.h"
42 #include "wx/msw/private/hiddenwin.h"
43 #include "wx/dynlib.h"
45 wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED
, wxDialUpEvent
);
46 wxDEFINE_EVENT( wxEVT_DIALUP_DISCONNECTED
, wxDialUpEvent
);
48 // Doesn't yet compile under VC++ 4, BC++, Watcom C++,
50 #if (!defined(__BORLANDC__) || (__BORLANDC__>=0x550)) && \
51 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
52 !defined(__GNUWIN32_OLD__) && \
53 !defined(__WINE__) && \
54 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
62 #ifndef INTERNET_CONNECTION_LAN
63 #define INTERNET_CONNECTION_LAN 2
65 #ifndef INTERNET_CONNECTION_PROXY
66 #define INTERNET_CONNECTION_PROXY 4
70 wxMSWDIALUP_WNDCLASSNAME
= wxT("_wxDialUpManager_Internal_Class");
71 static const wxChar
*gs_classForDialUpWindow
= NULL
;
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
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)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
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...
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
);
111 static const wxChar gs_funcSuffix
= wxT('A');
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
);
133 static const wxChar gs_funcSuffix
= wxT('W');
134 #endif // ASCII/Unicode
136 // structure passed to the secondary thread
137 struct WXDLLEXPORT wxRasThreadData
144 dialUpManager
= NULL
;
153 CloseHandle(hEventQuit
);
156 CloseHandle(hEventRas
);
159 HWND hWnd
; // window to send notifications to
160 HANDLE hEventRas
, // automatic event which RAS signals when status changes
161 hEventQuit
; // manual event which we signal when we terminate
163 class WXDLLIMPEXP_FWD_CORE wxDialUpManagerMSW
*dialUpManager
; // the owner
166 // ----------------------------------------------------------------------------
167 // wxDialUpManager class for MSW
168 // ----------------------------------------------------------------------------
170 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
174 wxDialUpManagerMSW();
175 virtual ~wxDialUpManagerMSW();
177 // implement base class pure virtuals
178 virtual bool IsOk() const;
179 virtual size_t GetISPNames(wxArrayString
& names
) const;
180 virtual bool Dial(const wxString
& nameOfISP
,
181 const wxString
& username
,
182 const wxString
& password
,
184 virtual bool IsDialing() const;
185 virtual bool CancelDialing();
186 virtual bool HangUp();
187 virtual bool IsAlwaysOnline() const;
188 virtual bool IsOnline() const;
189 virtual void SetOnlineStatus(bool isOnline
= true);
190 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
191 virtual void DisableAutoCheckOnlineStatus();
192 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
193 virtual void SetConnectCommand(const wxString
& commandDial
,
194 const wxString
& commandHangup
);
197 void CheckRasStatus();
199 // for wxRasStatusWindowProc
200 void OnConnectStatusChange();
201 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
204 static HWND
GetRasWindow() { return ms_hwndRas
; }
205 static void ResetRasWindow() { ms_hwndRas
= NULL
; }
206 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
209 // return the error string for the given RAS error code
210 static wxString
GetErrorString(DWORD error
);
212 // find the (first) handle of the active connection
213 static HRASCONN
FindActiveConnection();
215 // notify the application about status change
216 void NotifyApp(bool connected
, bool fromOurselves
= false) const;
218 // destroy the thread data and the thread itself
219 void CleanUpThreadData();
221 // number of times EnableAutoCheckOnlineStatus() had been called minus the
222 // number of times DisableAutoCheckOnlineStatus() had been called
223 int m_autoCheckLevel
;
225 // timer used for polling RAS status
226 class WXDLLEXPORT RasTimer
: public wxTimer
229 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
230 { m_dialUpManager
= dialUpManager
; }
232 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
235 wxDialUpManagerMSW
*m_dialUpManager
;
237 wxDECLARE_NO_COPY_CLASS(RasTimer
);
238 } m_timerStatusPolling
;
240 // thread handle for the thread sitting on connection change event
243 // data used by this thread and our hidden window to send messages between
245 wxRasThreadData
*m_data
;
247 // the handle of rasapi32.dll when it's loaded
248 wxDynamicLibrary m_dllRas
;
250 // the hidden window we use for passing messages between threads
251 static HWND ms_hwndRas
;
253 // the handle of the connection we initiated or 0 if none
254 static HRASCONN ms_hRasConnection
;
256 // the pointers to RAS functions
257 static RASDIAL ms_pfnRasDial
;
258 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
259 static RASENUMENTRIES ms_pfnRasEnumEntries
;
260 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
261 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
262 static RASHANGUP ms_pfnRasHangUp
;
263 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
264 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
265 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
266 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
267 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
268 static RASENUMDEVICES ms_pfnRasEnumDevices
;
269 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
270 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
271 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
272 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
273 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
274 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
276 // this function is not supported by Win95
277 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
279 // if this flag is different from -1, it overrides IsOnline()
280 static int ms_userSpecifiedOnlineStatus
;
282 // this flag tells us if we're online
283 static int ms_isConnected
;
285 // this flag tells us whether a call to RasDial() is in progress
286 static wxDialUpManagerMSW
*ms_dialer
;
288 wxDECLARE_NO_COPY_CLASS(wxDialUpManagerMSW
);
291 // module to destroy helper window created by wxDialUpManagerMSW
292 class wxDialUpManagerModule
: public wxModule
295 bool OnInit() { return true; }
298 HWND hwnd
= wxDialUpManagerMSW::GetRasWindow();
301 ::DestroyWindow(hwnd
);
302 wxDialUpManagerMSW::ResetRasWindow();
305 if ( gs_classForDialUpWindow
)
307 ::UnregisterClass(wxMSWDIALUP_WNDCLASSNAME
, wxGetInstance());
308 gs_classForDialUpWindow
= NULL
;
313 DECLARE_DYNAMIC_CLASS(wxDialUpManagerModule
)
316 IMPLEMENT_DYNAMIC_CLASS(wxDialUpManagerModule
, wxModule
)
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
323 WPARAM wParam
, LPARAM lParam
);
325 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
327 static void WINAPI
wxRasDialFunc(UINT unMsg
,
328 RASCONNSTATE rasconnstate
,
331 // ============================================================================
333 // ============================================================================
335 // ----------------------------------------------------------------------------
336 // init the static variables
337 // ----------------------------------------------------------------------------
339 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
341 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
343 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
344 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
345 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
346 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
347 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
348 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
349 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
350 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
351 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
352 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
353 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
354 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
355 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
356 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
357 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
358 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
359 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
360 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
361 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
363 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
364 int wxDialUpManagerMSW::ms_isConnected
= -1;
365 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
367 // ----------------------------------------------------------------------------
368 // ctor and dtor: the dynamic linking happens here
369 // ----------------------------------------------------------------------------
371 // the static creator function is implemented here
372 wxDialUpManager
*wxDialUpManager::Create()
374 return new wxDialUpManagerMSW
;
378 // warning about "'this' : used in base member initializer list" - so what?
379 #pragma warning(disable:4355)
382 wxDialUpManagerMSW::wxDialUpManagerMSW()
383 : m_timerStatusPolling(this),
384 m_dllRas(wxT("RASAPI32"))
386 // initialize our data
387 m_autoCheckLevel
= 0;
389 m_data
= new wxRasThreadData
;
391 if ( !m_dllRas
.IsLoaded() )
393 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
395 else if ( !ms_pfnRasDial
)
397 // resolve the functions we need
399 // this will contain the name of the function we failed to resolve
401 const char *funcName
= NULL
;
403 // get the function from rasapi32.dll and abort if it's not found
404 #define RESOLVE_RAS_FUNCTION(type, name) \
405 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(wxT(#name)) \
407 if ( !ms_pfn##name ) \
413 // a variant of above macro which doesn't abort if the function is
414 // not found in the DLL
415 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
416 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(wxT(#name)) \
419 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
420 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
421 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
422 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
423 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
424 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
425 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
427 // suppress error messages about missing (non essential) functions
431 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
432 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
433 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
434 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
435 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
436 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
437 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
438 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
439 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
440 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
441 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
442 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
445 // keep your preprocessor name space clean
446 #undef RESOLVE_RAS_FUNCTION
447 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
452 wxLogError(_("The version of remote access service (RAS) installed "
453 "on this machine is too old, please upgrade (the "
454 "following required function is missing: %s)."),
461 // enable auto check by default
462 EnableAutoCheckOnlineStatus(0);
465 wxDialUpManagerMSW::~wxDialUpManagerMSW()
470 // ----------------------------------------------------------------------------
472 // ----------------------------------------------------------------------------
474 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
476 wxChar buffer
[512]; // this should be more than enough according to MS docs
477 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
480 case ERROR_INVALID_PARAMETER
:
481 // this was a standard Win32 error probably
482 return wxString(wxSysErrorMsg(error
));
487 _("Failed to retrieve text of RAS error message"));
490 msg
.Printf(_("unknown error (error code %08x)."), error
);
495 // we want the error message to start from a lower case letter
496 buffer
[0] = (wxChar
)wxTolower(buffer
[0]);
498 return wxString(buffer
);
502 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
504 // enumerate connections
505 DWORD cbBuf
= sizeof(RASCONN
);
506 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
513 lpRasConn
->dwSize
= sizeof(RASCONN
);
515 DWORD nConnections
= 0;
516 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
518 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
520 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
522 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
524 LPRASCONN lpRasConnOld
= lpRasConn
;
525 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
534 else if ( dwRet
== 0 )
542 wxLogError(_("Cannot find active dialup connection: %s"),
543 GetErrorString(dwRet
).c_str());
550 switch ( nConnections
)
558 // more than 1 connection - we don't know what to do with this
559 // case, so give a warning but continue (taking the first
560 // connection) - the warning is really needed because this function
561 // is used, for example, to select the connection to hang up and so
562 // we may hang up the wrong connection here...
563 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
567 // exactly 1 connection, great
568 hrasconn
= lpRasConn
->hrasconn
;
576 void wxDialUpManagerMSW::CleanUpThreadData()
580 if ( !SetEvent(m_data
->hEventQuit
) )
582 wxLogLastError(wxT("SetEvent(RasThreadQuit)"));
584 else // sent quit request to the background thread
586 // the thread still needs m_data so we can't free it here, rather
587 // let the thread do it itself
591 CloseHandle(m_hThread
);
599 // ----------------------------------------------------------------------------
601 // ----------------------------------------------------------------------------
603 void wxDialUpManagerMSW::CheckRasStatus()
605 // use int, not bool to compare with -1
606 int isConnected
= FindActiveConnection() != 0;
607 if ( isConnected
!= ms_isConnected
)
609 if ( ms_isConnected
!= -1 )
611 // notify the program
612 NotifyApp(isConnected
!= 0);
614 // else: it's the first time we're called, just update the flag
616 ms_isConnected
= isConnected
;
620 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
622 wxDialUpEvent
event(connected
, fromOurselves
);
623 (void)wxTheApp
->ProcessEvent(event
);
626 // this function is called whenever the status of any RAS connection on this
627 // machine changes by RAS itself
628 void wxDialUpManagerMSW::OnConnectStatusChange()
630 // we know that status changed, but we don't know whether we're connected
631 // or not - so find it out
635 // this function is called by our callback which we give to RasDial() when
636 // calling it asynchronously
637 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
642 // this probably means that CancelDialing() was called and we get
643 // "disconnected" notification
647 // we're only interested in 2 events: connected and disconnected
650 wxLogError(_("Failed to establish dialup connection: %s"),
651 GetErrorString(dwError
).c_str());
653 // we should still call RasHangUp() if we got a non 0 connection
654 if ( ms_hRasConnection
)
656 ms_pfnRasHangUp(ms_hRasConnection
);
657 ms_hRasConnection
= 0;
662 NotifyApp(false /* !connected */, true /* we dialed ourselves */);
664 else if ( rasconnstate
== RASCS_Connected
)
666 ms_isConnected
= true;
669 NotifyApp(true /* connected */, true /* we dialed ourselves */);
673 // ----------------------------------------------------------------------------
674 // implementation of wxDialUpManager functions
675 // ----------------------------------------------------------------------------
677 bool wxDialUpManagerMSW::IsOk() const
679 return m_dllRas
.IsLoaded();
682 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
685 DWORD size
= sizeof(RASENTRYNAME
);
686 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
687 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
693 dwRet
= ms_pfnRasEnumEntries
696 NULL
, // default phone book (or all)
697 rasEntries
, // [out] buffer for the entries
698 &size
, // [in/out] size of the buffer
699 &nEntries
// [out] number of entries fetched
702 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
704 // reallocate the buffer
705 void *n
= realloc(rasEntries
, size
);
711 rasEntries
= (RASENTRYNAME
*)n
;
713 else if ( dwRet
!= 0 )
715 // some other error - abort
716 wxLogError(_("Failed to get ISP names: %s"),
717 GetErrorString(dwRet
).c_str());
724 while ( dwRet
!= 0 );
728 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
730 names
.Add(rasEntries
[n
].szEntryName
);
735 // return the number of entries
736 return names
.GetCount();
739 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
740 const wxString
& username
,
741 const wxString
& password
,
744 // check preconditions
745 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
747 if ( ms_hRasConnection
)
749 wxFAIL_MSG(wxT("there is already an active connection"));
754 // get the default ISP if none given
755 wxString
entryName(nameOfISP
);
759 size_t count
= GetISPNames(names
);
763 // no known ISPs, abort
764 wxLogError(_("Failed to connect: no ISP to dial."));
769 // only one ISP, choose it
770 entryName
= names
[0u];
774 // several ISPs, let the user choose
776 wxString
*strings
= new wxString
[count
];
777 for ( size_t i
= 0; i
< count
; i
++ )
779 strings
[i
] = names
[i
];
782 entryName
= wxGetSingleChoice
784 _("Choose ISP to dial"),
785 _("Please choose which ISP do you want to connect to"),
801 RASDIALPARAMS rasDialParams
;
802 rasDialParams
.dwSize
= sizeof(rasDialParams
);
803 wxStrlcpy(rasDialParams
.szEntryName
, entryName
.c_str(), RAS_MaxEntryName
);
805 // do we have the username and password?
806 if ( !username
|| !password
)
809 DWORD dwRet
= ms_pfnRasGetEntryDialParams
811 NULL
, // default phonebook
812 &rasDialParams
, // [in/out] the params of this entry
813 &gotPassword
// [out] did we get password?
818 wxLogError(_("Failed to connect: missing username/password."));
825 wxStrlcpy(rasDialParams
.szUserName
, username
.c_str(), UNLEN
);
826 wxStrlcpy(rasDialParams
.szPassword
, password
.c_str(), PWLEN
);
829 // default values for other fields
830 rasDialParams
.szPhoneNumber
[0] = '\0';
831 rasDialParams
.szCallbackNumber
[0] = '\0';
833 rasDialParams
.szDomain
[0] = '*';
834 rasDialParams
.szDomain
[1] = '\0';
836 // apparently, this is not really necessary - passing NULL instead of the
837 // phone book has the same effect
840 if ( wxGetOsVersion() == wxWINDOWS_NT
)
842 // first get the length
843 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
846 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
848 wxLogSysError(_("Cannot find the location of address book file"));
851 phoneBook
.UngetWriteBuf();
853 // this is the default phone book
854 phoneBook
<< "\\ras\\rasphone.pbk";
858 // TODO may be we should disable auto check while async dialing is in
863 DWORD dwRet
= ms_pfnRasDial
865 NULL
, // no extended features
866 NULL
, // default phone book file (NT only)
868 0, // use callback for notifications
869 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
870 : 0, // no notifications, sync operation
876 // can't pass a wxWCharBuffer through ( ... )
879 wxLogError(_("Failed to initiate dialup connection: %s"),
880 GetErrorString(dwRet
).c_str());
884 wxLogError(_("Failed to establish dialup connection: %s"),
885 GetErrorString(dwRet
).c_str());
888 // we should still call RasHangUp() if we got a non 0 connection
889 if ( ms_hRasConnection
)
891 ms_pfnRasHangUp(ms_hRasConnection
);
892 ms_hRasConnection
= 0;
900 // for async dialing, we're not yet connected
903 ms_isConnected
= true;
909 bool wxDialUpManagerMSW::IsDialing() const
911 return GetDialer() != NULL
;
914 bool wxDialUpManagerMSW::CancelDialing()
922 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
929 bool wxDialUpManagerMSW::HangUp()
931 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
933 // we may terminate either the connection we initiated or another one which
936 if ( ms_hRasConnection
)
938 hRasConn
= ms_hRasConnection
;
940 ms_hRasConnection
= 0;
944 hRasConn
= FindActiveConnection();
949 wxLogError(_("Cannot hang up - no active dialup connection."));
954 // note that it's not an error if the connection had been already
956 const DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
957 if ( dwRet
!= 0 && dwRet
!= ERROR_NO_CONNECTION
)
959 wxLogError(_("Failed to terminate the dialup connection: %s"),
960 GetErrorString(dwRet
).c_str());
963 ms_isConnected
= false;
968 bool wxDialUpManagerMSW::IsAlwaysOnline() const
970 // assume no permanent connection by default
971 bool isAlwaysOnline
= false;
973 // try to use WinInet functions
975 // NB: we could probably use wxDynamicLibrary here just as well,
976 // but we allow multiple instances of wxDialUpManagerMSW so
977 // we might as well use the ref counted version here too.
979 wxDynamicLibrary
hDll(wxT("WININET"));
980 if ( hDll
.IsLoaded() )
982 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
983 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
985 #define RESOLVE_FUNCTION(type, name) \
986 pfn##name = (type)hDll.GetSymbol(wxT(#name))
988 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
990 if ( pfnInternetGetConnectedState
)
993 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
995 // there is some connection to the net, see of which type
996 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
997 INTERNET_CONNECTION_PROXY
)) != 0;
999 //else: no Internet connection at all
1003 return isAlwaysOnline
;
1006 bool wxDialUpManagerMSW::IsOnline() const
1008 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
1010 if ( IsAlwaysOnline() )
1016 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1018 // user specified flag overrides our logic
1019 return ms_userSpecifiedOnlineStatus
!= 0;
1023 // return true if there is at least one active connection
1024 return FindActiveConnection() != 0;
1028 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1030 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1032 ms_userSpecifiedOnlineStatus
= isOnline
;
1035 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1037 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
1039 if ( m_autoCheckLevel
++ )
1045 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1049 // we're running under NT 4.0, Windows 98 or later and can use
1050 // RasConnectionNotification() to be notified by a secondary thread
1052 // first, see if we don't have this thread already running
1053 if ( m_hThread
!= 0 )
1055 if ( ::ResumeThread(m_hThread
) != (DWORD
)-1 )
1058 // we're leaving a zombie thread... but what else can we do?
1059 wxLogLastError(wxT("ResumeThread(RasThread)"));
1065 // create all the stuff we need to be notified about RAS connection
1070 // first create an event to wait on
1071 m_data
->hEventRas
= ::CreateEvent
1073 NULL
, // security attribute (default)
1074 FALSE
, // manual reset (no, it is automatic)
1075 FALSE
, // initial state (not signaled)
1078 if ( !m_data
->hEventRas
)
1080 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1088 // create the event we use to quit the thread: using a manual event
1089 // here avoids problems with missing the event if wxDialUpManagerMSW
1090 // is created and destroyed immediately, before wxRasStatusWindowProc
1091 // starts waiting on the event
1092 m_data
->hEventQuit
= ::CreateEvent
1094 NULL
, // default security
1095 TRUE
, // manual event
1096 FALSE
, // initially non signalled
1099 if ( !m_data
->hEventQuit
)
1101 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1103 CleanUpThreadData();
1109 if ( ok
&& !ms_hwndRas
)
1111 // create a hidden window to receive notification about connections
1113 ms_hwndRas
= wxCreateHiddenWindow
1115 &gs_classForDialUpWindow
,
1116 wxMSWDIALUP_WNDCLASSNAME
,
1117 wxRasStatusWindowProc
1121 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1123 CleanUpThreadData();
1129 m_data
->hWnd
= ms_hwndRas
;
1133 // start the secondary thread
1134 m_data
->dialUpManager
= this;
1137 m_hThread
= CreateThread
1141 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1149 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1151 CleanUpThreadData();
1157 // start receiving RAS notifications
1158 DWORD dwRet
= ms_pfnRasConnectionNotification
1160 (HRASCONN
)INVALID_HANDLE_VALUE
,
1162 3 /* RASCN_Connection | RASCN_Disconnection */
1167 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1168 GetErrorString(dwRet
).c_str());
1170 CleanUpThreadData();
1178 // we're running under Windows 95 and have to poll ourselves
1179 // (or, alternatively, the code above for NT/98 failed)
1180 m_timerStatusPolling
.Stop();
1181 if ( nSeconds
== 0 )
1186 m_timerStatusPolling
.Start(nSeconds
* 1000);
1191 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1193 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1195 if ( --m_autoCheckLevel
!= 0 )
1203 // we have running secondary thread, it's just enough to suspend it
1204 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1206 wxLogLastError(wxT("SuspendThread(RasThread)"));
1211 // even simpler - just stop the timer
1212 m_timerStatusPolling
.Stop();
1216 // ----------------------------------------------------------------------------
1217 // stubs which don't do anything in MSW version
1218 // ----------------------------------------------------------------------------
1220 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1223 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1225 // nothing to do - we don't use this
1228 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1229 const wxString
& WXUNUSED(hangup
))
1231 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1233 // nothing to do - we don't use this
1236 // ----------------------------------------------------------------------------
1238 // ----------------------------------------------------------------------------
1240 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1243 handles
[0] = data
->hEventRas
;
1244 handles
[1] = data
->hEventQuit
;
1249 DWORD dwRet
= ::WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1254 // RAS connection status changed
1255 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1259 case WAIT_OBJECT_0
+ 1:
1264 wxFAIL_MSG( wxT("unexpected return of WaitForMultipleObjects()") );
1268 // using wxLogLastError() from here is dangerous: we risk to
1269 // deadlock the main thread if wxLog sends output to GUI
1270 DWORD err
= GetLastError();
1271 wxMessageOutputDebug dbg
;
1274 wxT("WaitForMultipleObjects(RasMonitor) failed: 0x%08lx (%s)"),
1279 // no sense in continuing, who knows if the handles we're
1280 // waiting for even exist yet...
1285 // we don't need it any more now and if this thread ran, it is our
1286 // responsibility to free the data
1292 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1293 WPARAM wParam
, LPARAM lParam
)
1297 case wxWM_RAS_STATUS_CHANGED
:
1299 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1300 data
->dialUpManager
->OnConnectStatusChange();
1304 case wxWM_RAS_DIALING_PROGRESS
:
1306 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1308 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1313 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1319 static void WINAPI
wxRasDialFunc(UINT
WXUNUSED(unMsg
),
1320 RASCONNSTATE rasconnstate
,
1323 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1325 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1327 SendMessage(wxDialUpManagerMSW::GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1328 rasconnstate
, dwError
);
1331 #endif // __BORLANDC__
1333 #endif // wxUSE_DIALUP_MANAGER