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
43 #include "wx/generic/choicdgg.h"
45 #include "wx/dynlib.h"
46 #include "wx/dialup.h"
48 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED
)
49 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED
)
51 // Doesn't yet compile under VC++ 4, BC++, Watcom C++,
53 #if (!defined(__BORLANDC__) || (__BORLANDC__>=0x550)) && \
54 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
55 !defined(__GNUWIN32_OLD__) && \
56 !defined(__WATCOMC__) && \
57 !defined(__WINE__) && \
58 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
66 #ifndef INTERNET_CONNECTION_LAN
67 #define INTERNET_CONNECTION_LAN 2
69 #ifndef INTERNET_CONNECTION_PROXY
70 #define INTERNET_CONNECTION_PROXY 4
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
= _T('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
= _T('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 WXDLLEXPORT 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 wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
208 // return the error string for the given RAS error code
209 static wxString
GetErrorString(DWORD error
);
211 // find the (first) handle of the active connection
212 static HRASCONN
FindActiveConnection();
214 // notify the application about status change
215 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
217 // destroy the thread data and the thread itself
218 void CleanUpThreadData();
220 // number of times EnableAutoCheckOnlineStatus() had been called minus the
221 // number of times DisableAutoCheckOnlineStatus() had been called
222 int m_autoCheckLevel
;
224 // timer used for polling RAS status
225 class WXDLLEXPORT RasTimer
: public wxTimer
228 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
229 { m_dialUpManager
= dialUpManager
; }
231 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
234 wxDialUpManagerMSW
*m_dialUpManager
;
236 DECLARE_NO_COPY_CLASS(RasTimer
)
237 } m_timerStatusPolling
;
239 // thread handle for the thread sitting on connection change event
242 // data used by this thread and our hidden window to send messages between
244 wxRasThreadData
*m_data
;
246 // the handle of rasapi32.dll when it's loaded
247 wxDynamicLibrary m_dllRas
;
249 // the hidden window we use for passing messages between threads
250 static HWND ms_hwndRas
;
252 // the handle of the connection we initiated or 0 if none
253 static HRASCONN ms_hRasConnection
;
255 // the pointers to RAS functions
256 static RASDIAL ms_pfnRasDial
;
257 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
258 static RASENUMENTRIES ms_pfnRasEnumEntries
;
259 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
260 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
261 static RASHANGUP ms_pfnRasHangUp
;
262 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
263 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
264 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
265 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
266 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
267 static RASENUMDEVICES ms_pfnRasEnumDevices
;
268 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
269 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
270 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
271 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
272 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
273 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
275 // this function is not supported by Win95
276 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
278 // if this flag is different from -1, it overrides IsOnline()
279 static int ms_userSpecifiedOnlineStatus
;
281 // this flag tells us if we're online
282 static int ms_isConnected
;
284 // this flag tells us whether a call to RasDial() is in progress
285 static wxDialUpManagerMSW
*ms_dialer
;
287 DECLARE_NO_COPY_CLASS(wxDialUpManagerMSW
)
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
295 WPARAM wParam
, LPARAM lParam
);
297 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
299 static void WINAPI
wxRasDialFunc(UINT unMsg
,
300 RASCONNSTATE rasconnstate
,
303 // ============================================================================
305 // ============================================================================
307 // ----------------------------------------------------------------------------
308 // init the static variables
309 // ----------------------------------------------------------------------------
311 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
313 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
315 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
316 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
317 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
318 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
319 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
320 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
321 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
322 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
323 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
324 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
325 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
326 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
327 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
328 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
329 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
330 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
331 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
332 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
333 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
335 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
336 int wxDialUpManagerMSW::ms_isConnected
= -1;
337 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
339 // ----------------------------------------------------------------------------
340 // ctor and dtor: the dynamic linking happens here
341 // ----------------------------------------------------------------------------
343 // the static creator function is implemented here
344 wxDialUpManager
*wxDialUpManager::Create()
346 return new wxDialUpManagerMSW
;
350 // warning about "'this' : used in base member initializer list" - so what?
351 #pragma warning(disable:4355)
354 wxDialUpManagerMSW::wxDialUpManagerMSW()
355 : m_timerStatusPolling(this),
356 m_dllRas(_T("RASAPI32"))
358 // initialize our data
359 m_autoCheckLevel
= 0;
361 m_data
= new wxRasThreadData
;
363 if ( !m_dllRas
.IsLoaded() )
365 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
367 else if ( !ms_pfnRasDial
)
369 // resolve the functions we need
371 // this will contain the name of the function we failed to resolve
373 const char *funcName
= NULL
;
375 // get the function from rasapi32.dll and abort if it's not found
376 #define RESOLVE_RAS_FUNCTION(type, name) \
377 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
379 if ( !ms_pfn##name ) \
385 // a variant of above macro which doesn't abort if the function is
386 // not found in the DLL
387 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
388 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
391 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
392 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
393 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
394 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
395 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
396 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
397 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
399 // suppress error messages about missing (non essential) functions
403 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
404 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
405 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
406 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
407 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
408 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
409 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
410 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
411 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
412 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
413 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
414 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
417 // keep your preprocessor name space clean
418 #undef RESOLVE_RAS_FUNCTION
419 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
424 static const wxChar
*msg
= wxTRANSLATE(
425 "The version of remote access service (RAS) installed on this machine is too\
426 old, please upgrade (the following required function is missing: %s)."
429 wxLogError(wxGetTranslation(msg
), funcName
);
435 // enable auto check by default
436 EnableAutoCheckOnlineStatus(0);
439 wxDialUpManagerMSW::~wxDialUpManagerMSW()
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
450 wxChar buffer
[512]; // this should be more than enough according to MS docs
451 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
454 case ERROR_INVALID_PARAMETER
:
455 // this was a standard Win32 error probably
456 return wxString(wxSysErrorMsg(error
));
461 _("Failed to retrieve text of RAS error message"));
464 msg
.Printf(_("unknown error (error code %08x)."), error
);
469 // we want the error message to start from a lower case letter
470 buffer
[0] = wxTolower(buffer
[0]);
472 return wxString(buffer
);
476 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
478 // enumerate connections
479 DWORD cbBuf
= sizeof(RASCONN
);
480 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
487 lpRasConn
->dwSize
= sizeof(RASCONN
);
489 DWORD nConnections
= 0;
490 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
492 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
494 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
496 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
498 LPRASCONN lpRasConnOld
= lpRasConn
;
499 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
508 else if ( dwRet
== 0 )
516 wxLogError(_("Cannot find active dialup connection: %s"),
517 GetErrorString(dwRet
).c_str());
524 switch ( nConnections
)
532 // more than 1 connection - we don't know what to do with this
533 // case, so give a warning but continue (taking the first
534 // connection) - the warning is really needed because this function
535 // is used, for example, to select the connection to hang up and so
536 // we may hang up the wrong connection here...
537 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
541 // exactly 1 connection, great
542 hrasconn
= lpRasConn
->hrasconn
;
550 void wxDialUpManagerMSW::CleanUpThreadData()
554 if ( !SetEvent(m_data
->hEventQuit
) )
556 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
558 else // sent quit request to the background thread
560 // the thread still needs m_data so we can't free it here, rather
561 // let the thread do it itself
565 CloseHandle(m_hThread
);
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
).c_str());
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 m_dllRas
.IsLoaded();
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"),
689 GetErrorString(dwRet
).c_str());
696 while ( dwRet
!= 0 );
700 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
702 names
.Add(rasEntries
[n
].szEntryName
);
707 // return the number of entries
708 return names
.GetCount();
711 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
712 const wxString
& username
,
713 const wxString
& password
,
716 // check preconditions
717 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
719 if ( ms_hRasConnection
)
721 wxFAIL_MSG(wxT("there is already an active connection"));
726 // get the default ISP if none given
727 wxString
entryName(nameOfISP
);
731 size_t count
= GetISPNames(names
);
735 // no known ISPs, abort
736 wxLogError(_("Failed to connect: no ISP to dial."));
741 // only one ISP, choose it
742 entryName
= names
[0u];
746 // several ISPs, let the user choose
748 wxString
*strings
= new wxString
[count
];
749 for ( size_t i
= 0; i
< count
; i
++ )
751 strings
[i
] = names
[i
];
754 entryName
= wxGetSingleChoice
756 _("Choose ISP to dial"),
757 _("Please choose which ISP do you want to connect to"),
773 RASDIALPARAMS rasDialParams
;
774 rasDialParams
.dwSize
= sizeof(rasDialParams
);
775 wxStrncpy(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 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
798 wxStrncpy(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 NULL
, // no extended features
839 NULL
, // default phone book file (NT only)
841 0, // use callback for notifications
842 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
843 : 0, // no notifications, sync operation
849 // can't pass a wxWCharBuffer through ( ... )
850 wxLogError(_("Failed to %s dialup connection: %s"),
851 wxString(async
? _("initiate") : _("establish")).c_str(),
852 GetErrorString(dwRet
).c_str());
854 // we should still call RasHangUp() if we got a non 0 connection
855 if ( ms_hRasConnection
)
857 ms_pfnRasHangUp(ms_hRasConnection
);
858 ms_hRasConnection
= 0;
866 // for async dialing, we're not yet connected
869 ms_isConnected
= TRUE
;
875 bool wxDialUpManagerMSW::IsDialing() const
877 return GetDialer() != NULL
;
880 bool wxDialUpManagerMSW::CancelDialing()
888 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
895 bool wxDialUpManagerMSW::HangUp()
897 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
899 // we may terminate either the connection we initiated or another one which
902 if ( ms_hRasConnection
)
904 hRasConn
= ms_hRasConnection
;
906 ms_hRasConnection
= 0;
910 hRasConn
= FindActiveConnection();
915 wxLogError(_("Cannot hang up - no active dialup connection."));
920 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
923 wxLogError(_("Failed to terminate the dialup connection: %s"),
924 GetErrorString(dwRet
).c_str());
927 ms_isConnected
= FALSE
;
932 bool wxDialUpManagerMSW::IsAlwaysOnline() const
934 // assume no permanent connection by default
935 bool isAlwaysOnline
= FALSE
;
937 // try to use WinInet functions
939 // NB: we could probably use wxDynamicLibrary here just as well,
940 // but we allow multiple instances of wxDialUpManagerMSW so
941 // we might as well use the ref counted version here too.
943 wxDynamicLibrary
hDll(_T("WININET"));
944 if ( hDll
.IsLoaded() )
946 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
947 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
949 #define RESOLVE_FUNCTION(type, name) \
950 pfn##name = (type)hDll.GetSymbol(_T(#name))
952 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
954 if ( pfnInternetGetConnectedState
)
957 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
959 // there is some connection to the net, see of which type
960 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
961 INTERNET_CONNECTION_PROXY
)) != 0;
963 //else: no Internet connection at all
967 return isAlwaysOnline
;
970 bool wxDialUpManagerMSW::IsOnline() const
972 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
974 if ( IsAlwaysOnline() )
980 if ( ms_userSpecifiedOnlineStatus
!= -1 )
982 // user specified flag overrides our logic
983 return ms_userSpecifiedOnlineStatus
!= 0;
987 // return TRUE if there is at least one active connection
988 return FindActiveConnection() != 0;
992 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
994 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
996 ms_userSpecifiedOnlineStatus
= isOnline
;
999 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1001 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1003 if ( m_autoCheckLevel
++ )
1009 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1013 // we're running under NT 4.0, Windows 98 or later and can use
1014 // RasConnectionNotification() to be notified by a secondary thread
1016 // first, see if we don't have this thread already running
1017 if ( m_hThread
!= 0 )
1019 if ( ::ResumeThread(m_hThread
) != (DWORD
)-1 )
1022 // we're leaving a zombie thread... but what else can we do?
1023 wxLogLastError(wxT("ResumeThread(RasThread)"));
1029 // create all the stuff we need to be notified about RAS connection
1034 // first create an event to wait on
1035 m_data
->hEventRas
= CreateEvent
1037 NULL
, // security attribute (default)
1038 FALSE
, // manual reset (no, it is automatic)
1039 FALSE
, // initial state (not signaled)
1042 if ( !m_data
->hEventRas
)
1044 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1052 // create the event we use to quit the thread: using a manual event
1053 // here avoids problems with missing the event if wxDialUpManagerMSW
1054 // is created and destroyed immediately, before wxRasStatusWindowProc
1055 // starts waiting on the event
1056 m_data
->hEventQuit
= CreateEvent
1058 NULL
, // default security
1059 TRUE
, // manual event
1060 FALSE
, // initially non signalled
1063 if ( !m_data
->hEventQuit
)
1065 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1067 CleanUpThreadData();
1073 if ( ok
&& !ms_hwndRas
)
1075 // create a hidden window to receive notification about connections
1077 extern const wxChar
*wxCanvasClassName
;
1078 ms_hwndRas
= ::CreateWindow(wxCanvasClassName
, NULL
,
1081 (HMENU
)NULL
, wxGetInstance(), 0);
1084 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1086 CleanUpThreadData();
1092 wxSetWindowProc(ms_hwndRas
, wxRasStatusWindowProc
);
1095 m_data
->hWnd
= ms_hwndRas
;
1099 // start the secondary thread
1100 m_data
->dialUpManager
= this;
1103 m_hThread
= CreateThread
1107 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1115 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1117 CleanUpThreadData();
1123 // start receiving RAS notifications
1124 DWORD dwRet
= ms_pfnRasConnectionNotification
1126 (HRASCONN
)INVALID_HANDLE_VALUE
,
1128 3 /* RASCN_Connection | RASCN_Disconnection */
1133 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1134 GetErrorString(dwRet
).c_str());
1136 CleanUpThreadData();
1144 // we're running under Windows 95 and have to poll ourselves
1145 // (or, alternatively, the code above for NT/98 failed)
1146 m_timerStatusPolling
.Stop();
1147 if ( nSeconds
== 0 )
1152 m_timerStatusPolling
.Start(nSeconds
* 1000);
1157 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1159 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1161 if ( --m_autoCheckLevel
)
1169 // we have running secondary thread, it's just enough to suspend it
1170 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1172 wxLogLastError(wxT("SuspendThread(RasThread)"));
1177 // even simpler - just stop the timer
1178 m_timerStatusPolling
.Stop();
1182 // ----------------------------------------------------------------------------
1183 // stubs which don't do anything in MSW version
1184 // ----------------------------------------------------------------------------
1186 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1189 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1191 // nothing to do - we don't use this
1194 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1195 const wxString
& WXUNUSED(hangup
))
1197 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1199 // nothing to do - we don't use this
1202 // ----------------------------------------------------------------------------
1204 // ----------------------------------------------------------------------------
1206 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1209 handles
[0] = data
->hEventRas
;
1210 handles
[1] = data
->hEventQuit
;
1215 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1220 // RAS connection status changed
1221 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1225 case WAIT_OBJECT_0
+ 1:
1230 wxFAIL_MSG( _T("unexpected return of WaitForMultipleObjects()") );
1235 // using wxLogLastError() from here is dangerous: we risk to
1236 // deadlock the main thread if wxLog sends output to GUI
1237 DWORD err
= GetLastError();
1238 wxMessageOutputDebug().Printf
1240 wxT("WaitForMultipleObjects(RasMonitor) failed: 0x%08lx (%s)"),
1244 #endif // __WXDEBUG__
1246 // no sense in continuing, who knows if the handles we're
1247 // waiting for even exist yet...
1252 // we don't need it any more now and if this thread ran, it is our
1253 // responsability to free the data
1259 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1260 WPARAM wParam
, LPARAM lParam
)
1264 case wxWM_RAS_STATUS_CHANGED
:
1266 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1267 data
->dialUpManager
->OnConnectStatusChange();
1271 case wxWM_RAS_DIALING_PROGRESS
:
1273 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1275 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1280 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1286 static void WINAPI
wxRasDialFunc(UINT
WXUNUSED(unMsg
),
1287 RASCONNSTATE rasconnstate
,
1290 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1292 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1294 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1295 rasconnstate
, dwError
);
1298 #endif // __BORLANDC__
1300 #endif // wxUSE_DIALUP_MANAGER