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__) && \
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
142 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
143 dialUpManager
= NULL
;
146 HWND hWnd
; // window to send notifications to
147 HANDLE hEventRas
, // event which RAS signals when status changes
148 hEventQuit
; // event which we signal when we terminate
150 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
153 // ----------------------------------------------------------------------------
154 // wxDialUpManager class for MSW
155 // ----------------------------------------------------------------------------
157 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
161 wxDialUpManagerMSW();
162 virtual ~wxDialUpManagerMSW();
164 // implement base class pure virtuals
165 virtual bool IsOk() const;
166 virtual size_t GetISPNames(wxArrayString
& names
) const;
167 virtual bool Dial(const wxString
& nameOfISP
,
168 const wxString
& username
,
169 const wxString
& password
,
171 virtual bool IsDialing() const;
172 virtual bool CancelDialing();
173 virtual bool HangUp();
174 virtual bool IsAlwaysOnline() const;
175 virtual bool IsOnline() const;
176 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
177 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
178 virtual void DisableAutoCheckOnlineStatus();
179 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
180 virtual void SetConnectCommand(const wxString
& commandDial
,
181 const wxString
& commandHangup
);
184 void CheckRasStatus();
186 // for wxRasStatusWindowProc
187 void OnConnectStatusChange();
188 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
191 static HWND
GetRasWindow() { return ms_hwndRas
; }
192 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
195 // return the error string for the given RAS error code
196 static wxString
GetErrorString(DWORD error
);
198 // find the (first) handle of the active connection
199 static HRASCONN
FindActiveConnection();
201 // notify the application about status change
202 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
204 // destroy the thread data and the thread itself
205 void CleanUpThreadData();
207 // timer used for polling RAS status
208 class WXDLLEXPORT RasTimer
: public wxTimer
211 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
212 { m_dialUpManager
= dialUpManager
; }
214 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
217 wxDialUpManagerMSW
*m_dialUpManager
;
218 } m_timerStatusPolling
;
220 // thread handle for the thread sitting on connection change event
223 // data used by this thread and our hidden window to send messages between
225 wxRasThreadData m_data
;
227 // the handle of rasapi32.dll when it's loaded
228 wxDynamicLibrary m_dllRas
;
230 // the hidden window we use for passing messages between threads
231 static HWND ms_hwndRas
;
233 // the handle of the connection we initiated or 0 if none
234 static HRASCONN ms_hRasConnection
;
236 // the pointers to RAS functions
237 static RASDIAL ms_pfnRasDial
;
238 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
239 static RASENUMENTRIES ms_pfnRasEnumEntries
;
240 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
241 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
242 static RASHANGUP ms_pfnRasHangUp
;
243 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
244 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
245 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
246 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
247 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
248 static RASENUMDEVICES ms_pfnRasEnumDevices
;
249 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
250 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
251 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
252 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
253 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
254 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
256 // this function is not supported by Win95
257 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
259 // if this flag is different from -1, it overrides IsOnline()
260 static int ms_userSpecifiedOnlineStatus
;
262 // this flag tells us if we're online
263 static int ms_isConnected
;
265 // this flag tells us whether a call to RasDial() is in progress
266 static wxDialUpManagerMSW
*ms_dialer
;
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
274 WPARAM wParam
, LPARAM lParam
);
276 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
278 static void WINAPI
wxRasDialFunc(UINT unMsg
,
279 RASCONNSTATE rasconnstate
,
282 // ============================================================================
284 // ============================================================================
286 // ----------------------------------------------------------------------------
287 // init the static variables
288 // ----------------------------------------------------------------------------
290 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
292 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
294 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
295 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
296 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
297 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
298 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
299 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
300 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
301 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
302 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
303 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
304 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
305 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
306 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
307 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
308 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
309 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
310 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
311 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
312 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
314 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
315 int wxDialUpManagerMSW::ms_isConnected
= -1;
316 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
318 // ----------------------------------------------------------------------------
319 // ctor and dtor: the dynamic linking happens here
320 // ----------------------------------------------------------------------------
322 // the static creator function is implemented here
323 wxDialUpManager
*wxDialUpManager::Create()
325 return new wxDialUpManagerMSW
;
329 // warning about "'this' : used in base member initializer list" - so what?
330 #pragma warning(disable:4355)
333 wxDialUpManagerMSW::wxDialUpManagerMSW()
334 : m_timerStatusPolling(this),
335 m_dllRas(_T("RASAPI32"))
337 // initialize our data
340 if ( !m_dllRas
.IsLoaded() )
342 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
344 else if ( !ms_pfnRasDial
)
346 // resolve the functions we need
348 // this will contain the name of the function we failed to resolve
350 const char *funcName
= NULL
;
352 // get the function from rasapi32.dll and abort if it's not found
353 #define RESOLVE_RAS_FUNCTION(type, name) \
354 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
356 if ( !ms_pfn##name ) \
362 // a variant of above macro which doesn't abort if the function is
363 // not found in the DLL
364 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
365 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
368 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
369 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
370 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
371 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
372 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
373 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
374 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
376 // suppress error messages about missing (non essential) functions
380 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
381 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
382 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
383 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
384 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
385 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
386 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
387 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
388 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
389 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
390 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
391 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
394 // keep your preprocessor name space clean
395 #undef RESOLVE_RAS_FUNCTION
396 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
401 static const wxChar
*msg
= wxTRANSLATE(
402 "The version of remote access service (RAS) installed on this machine is too\
403 old, please upgrade (the following required function is missing: %s)."
406 wxLogError(wxGetTranslation(msg
), funcName
);
412 // enable auto check by default
413 EnableAutoCheckOnlineStatus(0);
416 wxDialUpManagerMSW::~wxDialUpManagerMSW()
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
427 wxChar buffer
[512]; // this should be more than enough according to MS docs
428 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
431 case ERROR_INVALID_PARAMETER
:
432 // this was a standard Win32 error probably
433 return wxString(wxSysErrorMsg(error
));
438 _("Failed to retrieve text of RAS error message"));
441 msg
.Printf(_("unknown error (error code %08x)."), error
);
446 // we want the error message to start from a lower case letter
447 buffer
[0] = wxTolower(buffer
[0]);
449 return wxString(buffer
);
453 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
455 // enumerate connections
456 DWORD cbBuf
= sizeof(RASCONN
);
457 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
464 lpRasConn
->dwSize
= sizeof(RASCONN
);
466 DWORD nConnections
= 0;
467 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
469 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
471 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
473 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
475 LPRASCONN lpRasConnOld
= lpRasConn
;
476 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
485 else if ( dwRet
== 0 )
493 wxLogError(_("Cannot find active dialup connection: %s"),
494 GetErrorString(dwRet
).c_str());
501 switch ( nConnections
)
509 // more than 1 connection - we don't know what to do with this
510 // case, so give a warning but continue (taking the first
511 // connection) - the warning is really needed because this function
512 // is used, for example, to select the connection to hang up and so
513 // we may hang up the wrong connection here...
514 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
518 // exactly 1 connection, great
519 hrasconn
= lpRasConn
->hrasconn
;
527 void wxDialUpManagerMSW::CleanUpThreadData()
531 if ( !SetEvent(m_data
.hEventQuit
) )
533 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
536 CloseHandle(m_hThread
);
543 DestroyWindow(m_data
.hWnd
);
548 if ( m_data
.hEventQuit
)
550 CloseHandle(m_data
.hEventQuit
);
552 m_data
.hEventQuit
= 0;
555 if ( m_data
.hEventRas
)
557 CloseHandle(m_data
.hEventRas
);
559 m_data
.hEventRas
= 0;
563 // ----------------------------------------------------------------------------
565 // ----------------------------------------------------------------------------
567 void wxDialUpManagerMSW::CheckRasStatus()
569 // use int, not bool to compare with -1
570 int isConnected
= FindActiveConnection() != 0;
571 if ( isConnected
!= ms_isConnected
)
573 if ( ms_isConnected
!= -1 )
575 // notify the program
576 NotifyApp(isConnected
!= 0);
578 // else: it's the first time we're called, just update the flag
580 ms_isConnected
= isConnected
;
584 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
586 wxDialUpEvent
event(connected
, fromOurselves
);
587 (void)wxTheApp
->ProcessEvent(event
);
590 // this function is called whenever the status of any RAS connection on this
591 // machine changes by RAS itself
592 void wxDialUpManagerMSW::OnConnectStatusChange()
594 // we know that status changed, but we don't know whether we're connected
595 // or not - so find it out
599 // this function is called by our callback which we give to RasDial() when
600 // calling it asynchronously
601 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
606 // this probably means that CancelDialing() was called and we get
607 // "disconnected" notification
611 // we're only interested in 2 events: connected and disconnected
614 wxLogError(_("Failed to establish dialup connection: %s"),
615 GetErrorString(dwError
).c_str());
617 // we should still call RasHangUp() if we got a non 0 connection
618 if ( ms_hRasConnection
)
620 ms_pfnRasHangUp(ms_hRasConnection
);
621 ms_hRasConnection
= 0;
626 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
628 else if ( rasconnstate
== RASCS_Connected
)
630 ms_isConnected
= TRUE
;
633 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
637 // ----------------------------------------------------------------------------
638 // implementation of wxDialUpManager functions
639 // ----------------------------------------------------------------------------
641 bool wxDialUpManagerMSW::IsOk() const
643 return m_dllRas
.IsLoaded();
646 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
649 DWORD size
= sizeof(RASENTRYNAME
);
650 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
651 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
657 dwRet
= ms_pfnRasEnumEntries
660 NULL
, // default phone book (or all)
661 rasEntries
, // [out] buffer for the entries
662 &size
, // [in/out] size of the buffer
663 &nEntries
// [out] number of entries fetched
666 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
668 // reallocate the buffer
669 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
671 else if ( dwRet
!= 0 )
673 // some other error - abort
674 wxLogError(_("Failed to get ISP names: %s"),
675 GetErrorString(dwRet
).c_str());
682 while ( dwRet
!= 0 );
686 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
688 names
.Add(rasEntries
[n
].szEntryName
);
693 // return the number of entries
694 return names
.GetCount();
697 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
698 const wxString
& username
,
699 const wxString
& password
,
702 // check preconditions
703 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
705 if ( ms_hRasConnection
)
707 wxFAIL_MSG(wxT("there is already an active connection"));
712 // get the default ISP if none given
713 wxString
entryName(nameOfISP
);
717 size_t count
= GetISPNames(names
);
721 // no known ISPs, abort
722 wxLogError(_("Failed to connect: no ISP to dial."));
727 // only one ISP, choose it
728 entryName
= names
[0u];
732 // several ISPs, let the user choose
734 wxString
*strings
= new wxString
[count
];
735 for ( size_t i
= 0; i
< count
; i
++ )
737 strings
[i
] = names
[i
];
740 entryName
= wxGetSingleChoice
742 _("Choose ISP to dial"),
743 _("Please choose which ISP do you want to connect to"),
759 RASDIALPARAMS rasDialParams
;
760 rasDialParams
.dwSize
= sizeof(rasDialParams
);
761 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
763 // do we have the username and password?
764 if ( !username
|| !password
)
767 DWORD dwRet
= ms_pfnRasGetEntryDialParams
769 NULL
, // default phonebook
770 &rasDialParams
, // [in/out] the params of this entry
771 &gotPassword
// [out] did we get password?
776 wxLogError(_("Failed to connect: missing username/password."));
783 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
784 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
787 // default values for other fields
788 rasDialParams
.szPhoneNumber
[0] = '\0';
789 rasDialParams
.szCallbackNumber
[0] = '\0';
790 rasDialParams
.szCallbackNumber
[0] = '\0';
792 rasDialParams
.szDomain
[0] = '*';
793 rasDialParams
.szDomain
[1] = '\0';
795 // apparently, this is not really necessary - passing NULL instead of the
796 // phone book has the same effect
799 if ( wxGetOsVersion() == wxWINDOWS_NT
)
801 // first get the length
802 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
805 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
807 wxLogSysError(_("Cannot find the location of address book file"));
810 phoneBook
.UngetWriteBuf();
812 // this is the default phone book
813 phoneBook
<< "\\ras\\rasphone.pbk";
817 // TODO may be we should disable auto check while async dialing is in
822 DWORD dwRet
= ms_pfnRasDial
824 NULL
, // no extended features
825 NULL
, // default phone book file (NT only)
827 0, // use callback for notifications
828 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
829 : 0, // no notifications, sync operation
835 // can't pass a wxWCharBuffer through ( ... )
836 wxLogError(_("Failed to %s dialup connection: %s"),
837 wxString(async
? _("initiate") : _("establish")).c_str(),
838 GetErrorString(dwRet
).c_str());
840 // we should still call RasHangUp() if we got a non 0 connection
841 if ( ms_hRasConnection
)
843 ms_pfnRasHangUp(ms_hRasConnection
);
844 ms_hRasConnection
= 0;
852 // for async dialing, we're not yet connected
855 ms_isConnected
= TRUE
;
861 bool wxDialUpManagerMSW::IsDialing() const
863 return GetDialer() != NULL
;
866 bool wxDialUpManagerMSW::CancelDialing()
874 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
881 bool wxDialUpManagerMSW::HangUp()
883 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
885 // we may terminate either the connection we initiated or another one which
888 if ( ms_hRasConnection
)
890 hRasConn
= ms_hRasConnection
;
892 ms_hRasConnection
= 0;
896 hRasConn
= FindActiveConnection();
901 wxLogError(_("Cannot hang up - no active dialup connection."));
906 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
909 wxLogError(_("Failed to terminate the dialup connection: %s"),
910 GetErrorString(dwRet
).c_str());
913 ms_isConnected
= FALSE
;
918 bool wxDialUpManagerMSW::IsAlwaysOnline() const
920 // assume no permanent connection by default
921 bool isAlwaysOnline
= FALSE
;
923 // try to use WinInet functions
925 // NB: we could probably use wxDynamicLibrary here just as well,
926 // but we allow multiple instances of wxDialUpManagerMSW so
927 // we might as well use the ref counted version here too.
929 wxDynamicLibrary
hDll(_T("WININET"));
930 if ( hDll
.IsLoaded() )
932 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
933 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
935 #define RESOLVE_FUNCTION(type, name) \
936 pfn##name = (type)hDll.GetSymbol(_T(#name))
938 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
940 if ( pfnInternetGetConnectedState
)
943 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
945 // there is some connection to the net, see of which type
946 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
947 INTERNET_CONNECTION_PROXY
)) != 0;
949 //else: no Internet connection at all
953 return isAlwaysOnline
;
956 bool wxDialUpManagerMSW::IsOnline() const
958 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
960 if ( ms_userSpecifiedOnlineStatus
!= -1 )
962 // user specified flag overrides our logic
963 return ms_userSpecifiedOnlineStatus
!= 0;
967 // return TRUE if there is at least one active connection
968 return FindActiveConnection() != 0;
972 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
974 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
976 ms_userSpecifiedOnlineStatus
= isOnline
;
979 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
981 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
983 bool ok
= ms_pfnRasConnectionNotification
!= 0;
987 // we're running under NT 4.0, Windows 98 or later and can use
988 // RasConnectionNotification() to be notified by a secondary thread
990 // first, see if we don't have this thread already running
991 if ( m_hThread
!= 0 )
993 DWORD dwSuspendCount
= 2;
994 while ( dwSuspendCount
> 1 )
996 dwSuspendCount
= ResumeThread(m_hThread
);
997 if ( dwSuspendCount
== (DWORD
)-1 )
999 wxLogLastError(wxT("ResumeThread(RasThread)"));
1012 // create all the stuff we need to be notified about RAS connection
1017 // first create an event to wait on
1018 m_data
.hEventRas
= CreateEvent
1020 NULL
, // security attribute (default)
1021 FALSE
, // manual reset (not)
1022 FALSE
, // initial state (not signaled)
1025 if ( !m_data
.hEventRas
)
1027 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1035 // create the event we use to quit the thread
1036 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1037 if ( !m_data
.hEventQuit
)
1039 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1041 CleanUpThreadData();
1047 if ( ok
&& !ms_hwndRas
)
1049 // create a hidden window to receive notification about connections
1051 extern const wxChar
*wxCanvasClassName
;
1052 ms_hwndRas
= ::CreateWindow(wxCanvasClassName
, NULL
,
1055 (HMENU
)NULL
, wxGetInstance(), 0);
1058 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1060 CleanUpThreadData();
1066 FARPROC windowProc
= MakeProcInstance
1068 (FARPROC
)wxRasStatusWindowProc
,
1072 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1075 m_data
.hWnd
= ms_hwndRas
;
1079 // start the secondary thread
1080 m_data
.dialUpManager
= this;
1083 m_hThread
= CreateThread
1087 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1095 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1097 CleanUpThreadData();
1103 // start receiving RAS notifications
1104 DWORD dwRet
= ms_pfnRasConnectionNotification
1106 (HRASCONN
)INVALID_HANDLE_VALUE
,
1108 3 /* RASCN_Connection | RASCN_Disconnection */
1113 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1114 GetErrorString(dwRet
).c_str());
1116 CleanUpThreadData();
1124 // we're running under Windows 95 and have to poll ourselves
1125 // (or, alternatively, the code above for NT/98 failed)
1126 m_timerStatusPolling
.Stop();
1127 if ( nSeconds
== 0 )
1132 m_timerStatusPolling
.Start(nSeconds
* 1000);
1137 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1139 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1143 // we have running secondary thread, it's just enough to suspend it
1144 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1146 wxLogLastError(wxT("SuspendThread(RasThread)"));
1151 // even simpler - just stop the timer
1152 m_timerStatusPolling
.Stop();
1156 // ----------------------------------------------------------------------------
1157 // stubs which don't do anything in MSW version
1158 // ----------------------------------------------------------------------------
1160 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1163 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1165 // nothing to do - we don't use this
1168 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1169 const wxString
& WXUNUSED(hangup
))
1171 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1173 // nothing to do - we don't use this
1176 // ----------------------------------------------------------------------------
1178 // ----------------------------------------------------------------------------
1180 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1183 handles
[0] = data
->hEventRas
;
1184 handles
[1] = data
->hEventQuit
;
1189 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1194 // RAS connection status changed
1195 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1199 case WAIT_OBJECT_0
+ 1:
1204 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1212 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1213 WPARAM wParam
, LPARAM lParam
)
1217 case wxWM_RAS_STATUS_CHANGED
:
1219 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1220 data
->dialUpManager
->OnConnectStatusChange();
1224 case wxWM_RAS_DIALING_PROGRESS
:
1226 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1228 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1233 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1239 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1240 RASCONNSTATE rasconnstate
,
1243 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1245 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1247 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1248 rasconnstate
, dwError
);
1251 #endif // __BORLANDC__
1253 #endif // wxUSE_DIALUP_MANAGER