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
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
;
219 DECLARE_NO_COPY_CLASS(RasTimer
)
220 } m_timerStatusPolling
;
222 // thread handle for the thread sitting on connection change event
225 // data used by this thread and our hidden window to send messages between
227 wxRasThreadData m_data
;
229 // the handle of rasapi32.dll when it's loaded
230 wxDynamicLibrary m_dllRas
;
232 // the hidden window we use for passing messages between threads
233 static HWND ms_hwndRas
;
235 // the handle of the connection we initiated or 0 if none
236 static HRASCONN ms_hRasConnection
;
238 // the pointers to RAS functions
239 static RASDIAL ms_pfnRasDial
;
240 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
241 static RASENUMENTRIES ms_pfnRasEnumEntries
;
242 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
243 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
244 static RASHANGUP ms_pfnRasHangUp
;
245 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
246 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
247 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
248 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
249 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
250 static RASENUMDEVICES ms_pfnRasEnumDevices
;
251 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
252 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
253 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
254 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
255 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
256 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
258 // this function is not supported by Win95
259 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
261 // if this flag is different from -1, it overrides IsOnline()
262 static int ms_userSpecifiedOnlineStatus
;
264 // this flag tells us if we're online
265 static int ms_isConnected
;
267 // this flag tells us whether a call to RasDial() is in progress
268 static wxDialUpManagerMSW
*ms_dialer
;
270 DECLARE_NO_COPY_CLASS(wxDialUpManagerMSW
)
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
278 WPARAM wParam
, LPARAM lParam
);
280 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
282 static void WINAPI
wxRasDialFunc(UINT unMsg
,
283 RASCONNSTATE rasconnstate
,
286 // ============================================================================
288 // ============================================================================
290 // ----------------------------------------------------------------------------
291 // init the static variables
292 // ----------------------------------------------------------------------------
294 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
296 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
298 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
299 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
300 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
301 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
302 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
303 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
304 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
305 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
306 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
307 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
308 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
309 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
310 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
311 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
312 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
313 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
314 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
315 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
316 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
318 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
319 int wxDialUpManagerMSW::ms_isConnected
= -1;
320 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
322 // ----------------------------------------------------------------------------
323 // ctor and dtor: the dynamic linking happens here
324 // ----------------------------------------------------------------------------
326 // the static creator function is implemented here
327 wxDialUpManager
*wxDialUpManager::Create()
329 return new wxDialUpManagerMSW
;
333 // warning about "'this' : used in base member initializer list" - so what?
334 #pragma warning(disable:4355)
337 wxDialUpManagerMSW::wxDialUpManagerMSW()
338 : m_timerStatusPolling(this),
339 m_dllRas(_T("RASAPI32"))
341 // initialize our data
344 if ( !m_dllRas
.IsLoaded() )
346 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
348 else if ( !ms_pfnRasDial
)
350 // resolve the functions we need
352 // this will contain the name of the function we failed to resolve
354 const char *funcName
= NULL
;
356 // get the function from rasapi32.dll and abort if it's not found
357 #define RESOLVE_RAS_FUNCTION(type, name) \
358 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
360 if ( !ms_pfn##name ) \
366 // a variant of above macro which doesn't abort if the function is
367 // not found in the DLL
368 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
369 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
372 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
373 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
374 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
375 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
376 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
377 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
378 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
380 // suppress error messages about missing (non essential) functions
384 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
385 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
386 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
387 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
388 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
389 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
390 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
391 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
392 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
393 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
394 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
395 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
398 // keep your preprocessor name space clean
399 #undef RESOLVE_RAS_FUNCTION
400 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
405 static const wxChar
*msg
= wxTRANSLATE(
406 "The version of remote access service (RAS) installed on this machine is too\
407 old, please upgrade (the following required function is missing: %s)."
410 wxLogError(wxGetTranslation(msg
), funcName
);
416 // enable auto check by default
417 EnableAutoCheckOnlineStatus(0);
420 wxDialUpManagerMSW::~wxDialUpManagerMSW()
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
431 wxChar buffer
[512]; // this should be more than enough according to MS docs
432 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
435 case ERROR_INVALID_PARAMETER
:
436 // this was a standard Win32 error probably
437 return wxString(wxSysErrorMsg(error
));
442 _("Failed to retrieve text of RAS error message"));
445 msg
.Printf(_("unknown error (error code %08x)."), error
);
450 // we want the error message to start from a lower case letter
451 buffer
[0] = wxTolower(buffer
[0]);
453 return wxString(buffer
);
457 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
459 // enumerate connections
460 DWORD cbBuf
= sizeof(RASCONN
);
461 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
468 lpRasConn
->dwSize
= sizeof(RASCONN
);
470 DWORD nConnections
= 0;
471 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
473 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
475 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
477 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
479 LPRASCONN lpRasConnOld
= lpRasConn
;
480 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
489 else if ( dwRet
== 0 )
497 wxLogError(_("Cannot find active dialup connection: %s"),
498 GetErrorString(dwRet
).c_str());
505 switch ( nConnections
)
513 // more than 1 connection - we don't know what to do with this
514 // case, so give a warning but continue (taking the first
515 // connection) - the warning is really needed because this function
516 // is used, for example, to select the connection to hang up and so
517 // we may hang up the wrong connection here...
518 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
522 // exactly 1 connection, great
523 hrasconn
= lpRasConn
->hrasconn
;
531 void wxDialUpManagerMSW::CleanUpThreadData()
535 if ( !SetEvent(m_data
.hEventQuit
) )
537 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
540 CloseHandle(m_hThread
);
547 DestroyWindow(m_data
.hWnd
);
552 if ( m_data
.hEventQuit
)
554 CloseHandle(m_data
.hEventQuit
);
556 m_data
.hEventQuit
= 0;
559 if ( m_data
.hEventRas
)
561 CloseHandle(m_data
.hEventRas
);
563 m_data
.hEventRas
= 0;
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 void wxDialUpManagerMSW::CheckRasStatus()
573 // use int, not bool to compare with -1
574 int isConnected
= FindActiveConnection() != 0;
575 if ( isConnected
!= ms_isConnected
)
577 if ( ms_isConnected
!= -1 )
579 // notify the program
580 NotifyApp(isConnected
!= 0);
582 // else: it's the first time we're called, just update the flag
584 ms_isConnected
= isConnected
;
588 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
590 wxDialUpEvent
event(connected
, fromOurselves
);
591 (void)wxTheApp
->ProcessEvent(event
);
594 // this function is called whenever the status of any RAS connection on this
595 // machine changes by RAS itself
596 void wxDialUpManagerMSW::OnConnectStatusChange()
598 // we know that status changed, but we don't know whether we're connected
599 // or not - so find it out
603 // this function is called by our callback which we give to RasDial() when
604 // calling it asynchronously
605 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
610 // this probably means that CancelDialing() was called and we get
611 // "disconnected" notification
615 // we're only interested in 2 events: connected and disconnected
618 wxLogError(_("Failed to establish dialup connection: %s"),
619 GetErrorString(dwError
).c_str());
621 // we should still call RasHangUp() if we got a non 0 connection
622 if ( ms_hRasConnection
)
624 ms_pfnRasHangUp(ms_hRasConnection
);
625 ms_hRasConnection
= 0;
630 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
632 else if ( rasconnstate
== RASCS_Connected
)
634 ms_isConnected
= TRUE
;
637 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
641 // ----------------------------------------------------------------------------
642 // implementation of wxDialUpManager functions
643 // ----------------------------------------------------------------------------
645 bool wxDialUpManagerMSW::IsOk() const
647 return m_dllRas
.IsLoaded();
650 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
653 DWORD size
= sizeof(RASENTRYNAME
);
654 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
655 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
661 dwRet
= ms_pfnRasEnumEntries
664 NULL
, // default phone book (or all)
665 rasEntries
, // [out] buffer for the entries
666 &size
, // [in/out] size of the buffer
667 &nEntries
// [out] number of entries fetched
670 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
672 // reallocate the buffer
673 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
675 else if ( dwRet
!= 0 )
677 // some other error - abort
678 wxLogError(_("Failed to get ISP names: %s"),
679 GetErrorString(dwRet
).c_str());
686 while ( dwRet
!= 0 );
690 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
692 names
.Add(rasEntries
[n
].szEntryName
);
697 // return the number of entries
698 return names
.GetCount();
701 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
702 const wxString
& username
,
703 const wxString
& password
,
706 // check preconditions
707 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
709 if ( ms_hRasConnection
)
711 wxFAIL_MSG(wxT("there is already an active connection"));
716 // get the default ISP if none given
717 wxString
entryName(nameOfISP
);
721 size_t count
= GetISPNames(names
);
725 // no known ISPs, abort
726 wxLogError(_("Failed to connect: no ISP to dial."));
731 // only one ISP, choose it
732 entryName
= names
[0u];
736 // several ISPs, let the user choose
738 wxString
*strings
= new wxString
[count
];
739 for ( size_t i
= 0; i
< count
; i
++ )
741 strings
[i
] = names
[i
];
744 entryName
= wxGetSingleChoice
746 _("Choose ISP to dial"),
747 _("Please choose which ISP do you want to connect to"),
763 RASDIALPARAMS rasDialParams
;
764 rasDialParams
.dwSize
= sizeof(rasDialParams
);
765 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
767 // do we have the username and password?
768 if ( !username
|| !password
)
771 DWORD dwRet
= ms_pfnRasGetEntryDialParams
773 NULL
, // default phonebook
774 &rasDialParams
, // [in/out] the params of this entry
775 &gotPassword
// [out] did we get password?
780 wxLogError(_("Failed to connect: missing username/password."));
787 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
788 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
791 // default values for other fields
792 rasDialParams
.szPhoneNumber
[0] = '\0';
793 rasDialParams
.szCallbackNumber
[0] = '\0';
794 rasDialParams
.szCallbackNumber
[0] = '\0';
796 rasDialParams
.szDomain
[0] = '*';
797 rasDialParams
.szDomain
[1] = '\0';
799 // apparently, this is not really necessary - passing NULL instead of the
800 // phone book has the same effect
803 if ( wxGetOsVersion() == wxWINDOWS_NT
)
805 // first get the length
806 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
809 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
811 wxLogSysError(_("Cannot find the location of address book file"));
814 phoneBook
.UngetWriteBuf();
816 // this is the default phone book
817 phoneBook
<< "\\ras\\rasphone.pbk";
821 // TODO may be we should disable auto check while async dialing is in
826 DWORD dwRet
= ms_pfnRasDial
828 NULL
, // no extended features
829 NULL
, // default phone book file (NT only)
831 0, // use callback for notifications
832 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
833 : 0, // no notifications, sync operation
839 // can't pass a wxWCharBuffer through ( ... )
840 wxLogError(_("Failed to %s dialup connection: %s"),
841 wxString(async
? _("initiate") : _("establish")).c_str(),
842 GetErrorString(dwRet
).c_str());
844 // we should still call RasHangUp() if we got a non 0 connection
845 if ( ms_hRasConnection
)
847 ms_pfnRasHangUp(ms_hRasConnection
);
848 ms_hRasConnection
= 0;
856 // for async dialing, we're not yet connected
859 ms_isConnected
= TRUE
;
865 bool wxDialUpManagerMSW::IsDialing() const
867 return GetDialer() != NULL
;
870 bool wxDialUpManagerMSW::CancelDialing()
878 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
885 bool wxDialUpManagerMSW::HangUp()
887 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
889 // we may terminate either the connection we initiated or another one which
892 if ( ms_hRasConnection
)
894 hRasConn
= ms_hRasConnection
;
896 ms_hRasConnection
= 0;
900 hRasConn
= FindActiveConnection();
905 wxLogError(_("Cannot hang up - no active dialup connection."));
910 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
913 wxLogError(_("Failed to terminate the dialup connection: %s"),
914 GetErrorString(dwRet
).c_str());
917 ms_isConnected
= FALSE
;
922 bool wxDialUpManagerMSW::IsAlwaysOnline() const
924 // assume no permanent connection by default
925 bool isAlwaysOnline
= FALSE
;
927 // try to use WinInet functions
929 // NB: we could probably use wxDynamicLibrary here just as well,
930 // but we allow multiple instances of wxDialUpManagerMSW so
931 // we might as well use the ref counted version here too.
933 wxDynamicLibrary
hDll(_T("WININET"));
934 if ( hDll
.IsLoaded() )
936 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
937 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
939 #define RESOLVE_FUNCTION(type, name) \
940 pfn##name = (type)hDll.GetSymbol(_T(#name))
942 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
944 if ( pfnInternetGetConnectedState
)
947 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
949 // there is some connection to the net, see of which type
950 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
951 INTERNET_CONNECTION_PROXY
)) != 0;
953 //else: no Internet connection at all
957 return isAlwaysOnline
;
960 bool wxDialUpManagerMSW::IsOnline() const
962 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
964 if ( ms_userSpecifiedOnlineStatus
!= -1 )
966 // user specified flag overrides our logic
967 return ms_userSpecifiedOnlineStatus
!= 0;
971 // return TRUE if there is at least one active connection
972 return FindActiveConnection() != 0;
976 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
978 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
980 ms_userSpecifiedOnlineStatus
= isOnline
;
983 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
985 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
987 bool ok
= ms_pfnRasConnectionNotification
!= 0;
991 // we're running under NT 4.0, Windows 98 or later and can use
992 // RasConnectionNotification() to be notified by a secondary thread
994 // first, see if we don't have this thread already running
995 if ( m_hThread
!= 0 )
997 DWORD dwSuspendCount
= 2;
998 while ( dwSuspendCount
> 1 )
1000 dwSuspendCount
= ResumeThread(m_hThread
);
1001 if ( dwSuspendCount
== (DWORD
)-1 )
1003 wxLogLastError(wxT("ResumeThread(RasThread)"));
1016 // create all the stuff we need to be notified about RAS connection
1021 // first create an event to wait on
1022 m_data
.hEventRas
= CreateEvent
1024 NULL
, // security attribute (default)
1025 FALSE
, // manual reset (not)
1026 FALSE
, // initial state (not signaled)
1029 if ( !m_data
.hEventRas
)
1031 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1039 // create the event we use to quit the thread
1040 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1041 if ( !m_data
.hEventQuit
)
1043 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1045 CleanUpThreadData();
1051 if ( ok
&& !ms_hwndRas
)
1053 // create a hidden window to receive notification about connections
1055 extern const wxChar
*wxCanvasClassName
;
1056 ms_hwndRas
= ::CreateWindow(wxCanvasClassName
, NULL
,
1059 (HMENU
)NULL
, wxGetInstance(), 0);
1062 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1064 CleanUpThreadData();
1070 FARPROC windowProc
= MakeProcInstance
1072 (FARPROC
)wxRasStatusWindowProc
,
1076 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1079 m_data
.hWnd
= ms_hwndRas
;
1083 // start the secondary thread
1084 m_data
.dialUpManager
= this;
1087 m_hThread
= CreateThread
1091 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1099 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1101 CleanUpThreadData();
1107 // start receiving RAS notifications
1108 DWORD dwRet
= ms_pfnRasConnectionNotification
1110 (HRASCONN
)INVALID_HANDLE_VALUE
,
1112 3 /* RASCN_Connection | RASCN_Disconnection */
1117 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1118 GetErrorString(dwRet
).c_str());
1120 CleanUpThreadData();
1128 // we're running under Windows 95 and have to poll ourselves
1129 // (or, alternatively, the code above for NT/98 failed)
1130 m_timerStatusPolling
.Stop();
1131 if ( nSeconds
== 0 )
1136 m_timerStatusPolling
.Start(nSeconds
* 1000);
1141 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1143 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1147 // we have running secondary thread, it's just enough to suspend it
1148 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1150 wxLogLastError(wxT("SuspendThread(RasThread)"));
1155 // even simpler - just stop the timer
1156 m_timerStatusPolling
.Stop();
1160 // ----------------------------------------------------------------------------
1161 // stubs which don't do anything in MSW version
1162 // ----------------------------------------------------------------------------
1164 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1167 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1169 // nothing to do - we don't use this
1172 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1173 const wxString
& WXUNUSED(hangup
))
1175 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1177 // nothing to do - we don't use this
1180 // ----------------------------------------------------------------------------
1182 // ----------------------------------------------------------------------------
1184 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1187 handles
[0] = data
->hEventRas
;
1188 handles
[1] = data
->hEventQuit
;
1193 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1198 // RAS connection status changed
1199 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1203 case WAIT_OBJECT_0
+ 1:
1208 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1216 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1217 WPARAM wParam
, LPARAM lParam
)
1221 case wxWM_RAS_STATUS_CHANGED
:
1223 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1224 data
->dialUpManager
->OnConnectStatusChange();
1228 case wxWM_RAS_DIALING_PROGRESS
:
1230 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1232 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1237 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1243 static void WINAPI
wxRasDialFunc(UINT
WXUNUSED(unMsg
),
1244 RASCONNSTATE rasconnstate
,
1247 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1249 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1251 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1252 rasconnstate
, dwError
);
1255 #endif // __BORLANDC__
1257 #endif // wxUSE_DIALUP_MANAGER