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++: no wininet.h
52 #if !defined(__BORLANDC__) && \
53 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
54 !defined(__GNUWIN32_OLD__) && \
55 !defined(__WATCOMC__) && \
56 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
64 #ifndef INTERNET_CONNECTION_LAN
65 #define INTERNET_CONNECTION_LAN 2
67 #ifndef INTERNET_CONNECTION_PROXY
68 #define INTERNET_CONNECTION_PROXY 4
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // this message is sent by the secondary thread when RAS status changes
76 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
77 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // the signatures of RAS functions: all this is quite heavy, but we must do it
84 // to allow running wxWin programs on machine which don't have RAS installed
85 // (this does exist) - if we link with rasapi32.lib, the program will fail on
86 // startup because of the missing DLL...
89 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
90 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
91 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
92 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
93 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
94 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
95 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
96 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
97 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
98 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
99 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
100 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
101 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
102 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
103 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
104 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
105 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
106 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
107 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
109 static const wxChar gs_funcSuffix
= _T('A');
111 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
112 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
113 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
114 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
115 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
116 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
117 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
118 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
119 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
120 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
121 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
122 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
123 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
124 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
125 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
126 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
127 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
128 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
129 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
131 static const wxChar gs_funcSuffix
= _T('W');
132 #endif // ASCII/Unicode
134 // structure passed to the secondary thread
135 struct WXDLLEXPORT wxRasThreadData
140 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
141 dialUpManager
= NULL
;
144 HWND hWnd
; // window to send notifications to
145 HANDLE hEventRas
, // event which RAS signals when status changes
146 hEventQuit
; // event which we signal when we terminate
148 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
151 // ----------------------------------------------------------------------------
152 // wxDialUpManager class for MSW
153 // ----------------------------------------------------------------------------
155 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
159 wxDialUpManagerMSW();
160 virtual ~wxDialUpManagerMSW();
162 // implement base class pure virtuals
163 virtual bool IsOk() const;
164 virtual size_t GetISPNames(wxArrayString
& names
) const;
165 virtual bool Dial(const wxString
& nameOfISP
,
166 const wxString
& username
,
167 const wxString
& password
,
169 virtual bool IsDialing() const;
170 virtual bool CancelDialing();
171 virtual bool HangUp();
172 virtual bool IsAlwaysOnline() const;
173 virtual bool IsOnline() const;
174 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
175 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
176 virtual void DisableAutoCheckOnlineStatus();
177 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
178 virtual void SetConnectCommand(const wxString
& commandDial
,
179 const wxString
& commandHangup
);
182 void CheckRasStatus();
184 // for wxRasStatusWindowProc
185 void OnConnectStatusChange();
186 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
189 static HWND
GetRasWindow() { return ms_hwndRas
; }
190 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
193 // return the error string for the given RAS error code
194 static wxString
GetErrorString(DWORD error
);
196 // find the (first) handle of the active connection
197 static HRASCONN
FindActiveConnection();
199 // notify the application about status change
200 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
202 // destroy the thread data and the thread itself
203 void CleanUpThreadData();
205 // timer used for polling RAS status
206 class WXDLLEXPORT RasTimer
: public wxTimer
209 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
210 { m_dialUpManager
= dialUpManager
; }
212 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
215 wxDialUpManagerMSW
*m_dialUpManager
;
216 } m_timerStatusPolling
;
218 // thread handle for the thread sitting on connection change event
221 // data used by this thread and our hidden window to send messages between
223 wxRasThreadData m_data
;
225 // the handle of rasapi32.dll when it's loaded
226 wxPluginManager m_dllRas
;
228 // the hidden window we use for passing messages between threads
229 static HWND ms_hwndRas
;
231 // the handle of the connection we initiated or 0 if none
232 static HRASCONN ms_hRasConnection
;
234 // FIXME: There is probably no reason these really need to
235 // be static anymore since the dll refcounting is
236 // handled by wxPluginManager now. Whether or not
237 // we still _want_ them to be static is another
240 // the pointers to RAS functions
241 static RASDIAL ms_pfnRasDial
;
242 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
243 static RASENUMENTRIES ms_pfnRasEnumEntries
;
244 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
245 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
246 static RASHANGUP ms_pfnRasHangUp
;
247 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
248 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
249 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
250 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
251 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
252 static RASENUMDEVICES ms_pfnRasEnumDevices
;
253 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
254 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
255 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
256 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
257 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
258 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
260 // this function is not supported by Win95
261 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
263 // if this flag is different from -1, it overrides IsOnline()
264 static int ms_userSpecifiedOnlineStatus
;
266 // this flag tells us if we're online
267 static int ms_isConnected
;
269 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
271 static int ms_isAlwaysOnline
;
273 // this flag tells us whether a call to RasDial() is in progress
274 static wxDialUpManagerMSW
*ms_dialer
;
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
282 WPARAM wParam
, LPARAM lParam
);
284 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
286 static void WINAPI
wxRasDialFunc(UINT unMsg
,
287 RASCONNSTATE rasconnstate
,
290 // ============================================================================
292 // ============================================================================
294 // ----------------------------------------------------------------------------
295 // init the static variables
296 // ----------------------------------------------------------------------------
298 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
300 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
302 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
303 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
304 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
305 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
306 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
307 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
308 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
309 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
310 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
311 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
312 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
313 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
314 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
315 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
316 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
317 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
318 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
319 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
320 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
322 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
323 int wxDialUpManagerMSW::ms_isConnected
= -1;
324 int wxDialUpManagerMSW::ms_isAlwaysOnline
= -1;
325 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
327 // ----------------------------------------------------------------------------
328 // ctor and dtor: the dynamic linking happens here
329 // ----------------------------------------------------------------------------
331 // the static creator function is implemented here
332 wxDialUpManager
*wxDialUpManager::Create()
334 return new wxDialUpManagerMSW
;
338 // warning about "'this' : used in base member initializer list" - so what?
339 #pragma warning(disable:4355)
342 wxDialUpManagerMSW::wxDialUpManagerMSW()
343 : m_timerStatusPolling(this)
344 , m_dllRas(_T("RASAPI32"))
346 // initialize our data
349 if ( !m_dllRas
.IsLoaded() )
351 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
353 else if( ms_pfnRasDial
== 0 )
355 // resolve the functions we need
357 // this will contain the name of the function we failed to resolve
359 const char *funcName
= NULL
;
361 // get the function from rasapi32.dll and abort if it's not found
362 #define RESOLVE_RAS_FUNCTION(type, name) \
363 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
365 if ( !ms_pfn##name ) \
371 // a variant of above macro which doesn't abort if the function is
372 // not found in the DLL
373 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
374 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
377 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
378 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
379 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
380 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
381 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
382 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
383 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
385 // suppress error messages about missing (non essential) functions
389 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
390 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
391 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
392 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
393 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
394 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
395 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
396 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
397 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
398 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
399 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
400 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
403 // keep your preprocessor name space clean
404 #undef RESOLVE_RAS_FUNCTION
405 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
410 static const wxChar
*msg
= wxTRANSLATE(
411 "The version of remote access service (RAS) installed on this machine is too\
412 old, please upgrade (the following required function is missing: %s)."
415 wxLogError(wxGetTranslation(msg
), funcName
);
421 // enable auto check by default
422 EnableAutoCheckOnlineStatus(0);
425 wxDialUpManagerMSW::~wxDialUpManagerMSW()
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
436 wxChar buffer
[512]; // this should be more than enough according to MS docs
437 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
440 case ERROR_INVALID_PARAMETER
:
441 // this was a standard Win32 error probably
442 return wxString(wxSysErrorMsg(error
));
447 _("Failed to retrieve text of RAS error message"));
450 msg
.Printf(_("unknown error (error code %08x)."), error
);
455 // we want the error message to start from a lower case letter
456 buffer
[0] = wxTolower(buffer
[0]);
458 return wxString(buffer
);
462 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
464 // enumerate connections
465 DWORD cbBuf
= sizeof(RASCONN
);
466 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
473 lpRasConn
->dwSize
= sizeof(RASCONN
);
475 DWORD nConnections
= 0;
476 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
478 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
480 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
482 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
484 LPRASCONN lpRasConnOld
= lpRasConn
;
485 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
494 else if ( dwRet
== 0 )
502 wxLogError(_("Cannot find active dialup connection: %s"),
503 GetErrorString(dwRet
).c_str());
510 switch ( nConnections
)
518 // more than 1 connection - we don't know what to do with this
519 // case, so give a warning but continue (taking the first
520 // connection) - the warning is really needed because this function
521 // is used, for example, to select the connection to hang up and so
522 // we may hang up the wrong connection here...
523 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
527 // exactly 1 connection, great
528 hrasconn
= lpRasConn
->hrasconn
;
536 void wxDialUpManagerMSW::CleanUpThreadData()
540 if ( !SetEvent(m_data
.hEventQuit
) )
542 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
545 CloseHandle(m_hThread
);
552 DestroyWindow(m_data
.hWnd
);
557 if ( m_data
.hEventQuit
)
559 CloseHandle(m_data
.hEventQuit
);
561 m_data
.hEventQuit
= 0;
564 if ( m_data
.hEventRas
)
566 CloseHandle(m_data
.hEventRas
);
568 m_data
.hEventRas
= 0;
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
576 void wxDialUpManagerMSW::CheckRasStatus()
578 // use int, not bool to compare with -1
579 int isConnected
= FindActiveConnection() != 0;
580 if ( isConnected
!= ms_isConnected
)
582 if ( ms_isConnected
!= -1 )
584 // notify the program
585 NotifyApp(isConnected
!= 0);
587 // else: it's the first time we're called, just update the flag
589 ms_isConnected
= isConnected
;
593 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
595 wxDialUpEvent
event(connected
, fromOurselves
);
596 (void)wxTheApp
->ProcessEvent(event
);
599 // this function is called whenever the status of any RAS connection on this
600 // machine changes by RAS itself
601 void wxDialUpManagerMSW::OnConnectStatusChange()
603 // we know that status changed, but we don't know whether we're connected
604 // or not - so find it out
608 // this function is called by our callback which we give to RasDial() when
609 // calling it asynchronously
610 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
615 // this probably means that CancelDialing() was called and we get
616 // "disconnected" notification
620 // we're only interested in 2 events: connected and disconnected
623 wxLogError(_("Failed to establish dialup connection: %s"),
624 GetErrorString(dwError
).c_str());
626 // we should still call RasHangUp() if we got a non 0 connection
627 if ( ms_hRasConnection
)
629 ms_pfnRasHangUp(ms_hRasConnection
);
630 ms_hRasConnection
= 0;
635 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
637 else if ( rasconnstate
== RASCS_Connected
)
639 ms_isConnected
= TRUE
;
642 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
646 // ----------------------------------------------------------------------------
647 // implementation of wxDialUpManager functions
648 // ----------------------------------------------------------------------------
650 bool wxDialUpManagerMSW::IsOk() const
652 return m_dllRas
.IsLoaded();
655 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
658 DWORD size
= sizeof(RASENTRYNAME
);
659 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
660 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
666 dwRet
= ms_pfnRasEnumEntries
669 NULL
, // default phone book (or all)
670 rasEntries
, // [out] buffer for the entries
671 &size
, // [in/out] size of the buffer
672 &nEntries
// [out] number of entries fetched
675 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
677 // reallocate the buffer
678 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
680 else if ( dwRet
!= 0 )
682 // some other error - abort
683 wxLogError(_("Failed to get ISP names: %s"),
684 GetErrorString(dwRet
).c_str());
691 while ( dwRet
!= 0 );
695 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
697 names
.Add(rasEntries
[n
].szEntryName
);
702 // return the number of entries
703 return names
.GetCount();
706 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
707 const wxString
& username
,
708 const wxString
& password
,
711 // check preconditions
712 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
714 if ( ms_hRasConnection
)
716 wxFAIL_MSG(wxT("there is already an active connection"));
721 // get the default ISP if none given
722 wxString
entryName(nameOfISP
);
726 size_t count
= GetISPNames(names
);
730 // no known ISPs, abort
731 wxLogError(_("Failed to connect: no ISP to dial."));
736 // only one ISP, choose it
737 entryName
= names
[0u];
741 // several ISPs, let the user choose
743 wxString
*strings
= new wxString
[count
];
744 for ( size_t i
= 0; i
< count
; i
++ )
746 strings
[i
] = names
[i
];
749 entryName
= wxGetSingleChoice
751 _("Choose ISP to dial"),
752 _("Please choose which ISP do you want to connect to"),
768 RASDIALPARAMS rasDialParams
;
769 rasDialParams
.dwSize
= sizeof(rasDialParams
);
770 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
772 // do we have the username and password?
773 if ( !username
|| !password
)
776 DWORD dwRet
= ms_pfnRasGetEntryDialParams
778 NULL
, // default phonebook
779 &rasDialParams
, // [in/out] the params of this entry
780 &gotPassword
// [out] did we get password?
785 wxLogError(_("Failed to connect: missing username/password."));
792 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
793 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
796 // default values for other fields
797 rasDialParams
.szPhoneNumber
[0] = '\0';
798 rasDialParams
.szCallbackNumber
[0] = '\0';
799 rasDialParams
.szCallbackNumber
[0] = '\0';
801 rasDialParams
.szDomain
[0] = '*';
802 rasDialParams
.szDomain
[1] = '\0';
804 // apparently, this is not really necessary - passing NULL instead of the
805 // phone book has the same effect
808 if ( wxGetOsVersion() == wxWINDOWS_NT
)
810 // first get the length
811 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
814 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
816 wxLogSysError(_("Cannot find the location of address book file"));
819 phoneBook
.UngetWriteBuf();
821 // this is the default phone book
822 phoneBook
<< "\\ras\\rasphone.pbk";
826 // TODO may be we should disable auto check while async dialing is in
831 DWORD dwRet
= ms_pfnRasDial
833 (LPRASDIALEXTENSIONS
)NULL
, // no extended features
834 NULL
, // default phone book file (NT only)
836 0, // use callback for notifications
837 async
? wxRasDialFunc
// the callback
838 : 0, // no notifications - sync operation
845 // can't pass a wxWCharBuffer through ( ... )
846 wxLogError(_("Failed to %s dialup connection: %s").data(),
847 async
? _("initiate").data() : _("establish").data(),
848 GetErrorString(dwRet
).c_str());
850 // can't pass a wxWCharBuffer through ( ... )
851 wxLogError(_("Failed to %s dialup connection: %s"),
852 wxString(async
? _("initiate") : _("establish")).c_str(),
853 GetErrorString(dwRet
).c_str());
856 // we should still call RasHangUp() if we got a non 0 connection
857 if ( ms_hRasConnection
)
859 ms_pfnRasHangUp(ms_hRasConnection
);
860 ms_hRasConnection
= 0;
868 // for async dialing, we're not yet connected
871 ms_isConnected
= TRUE
;
877 bool wxDialUpManagerMSW::IsDialing() const
879 return GetDialer() != NULL
;
882 bool wxDialUpManagerMSW::CancelDialing()
890 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
897 bool wxDialUpManagerMSW::HangUp()
899 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
901 // we may terminate either the connection we initiated or another one which
904 if ( ms_hRasConnection
)
906 hRasConn
= ms_hRasConnection
;
908 ms_hRasConnection
= 0;
912 hRasConn
= FindActiveConnection();
917 wxLogError(_("Cannot hang up - no active dialup connection."));
922 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
925 wxLogError(_("Failed to terminate the dialup connection: %s"),
926 GetErrorString(dwRet
).c_str());
929 ms_isConnected
= FALSE
;
934 bool wxDialUpManagerMSW::IsAlwaysOnline() const
936 // we cache the result (presumably this won't change while the program is
938 if ( ms_isAlwaysOnline
!= -1 )
940 return ms_isAlwaysOnline
!= 0;
943 // try to use WinInet function first
945 // NB: we could probably use wxDynamicLibrary here just as well,
946 // but we allow multiple instances of wxDialUpManagerMSW so
947 // we might as well use the ref counted version here too.
949 wxPluginManager
hDll(_T("WININET"));
950 if ( hDll
.IsLoaded() )
952 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
953 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
955 #define RESOLVE_FUNCTION(type, name) \
956 pfn##name = (type)hDll.GetSymbol(_T(#name))
958 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
960 if ( pfnInternetGetConnectedState
)
963 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
965 // there is some connection to the net, see of which type
966 ms_isAlwaysOnline
= (flags
& INTERNET_CONNECTION_LAN
!= 0) ||
967 (flags
& INTERNET_CONNECTION_PROXY
!= 0);
971 // no Internet connection at all
972 ms_isAlwaysOnline
= FALSE
;
977 // did we succeed with WinInet? if not, try something else
978 if ( ms_isAlwaysOnline
== -1 )
982 // definitely no permanent connection because we are not connected
984 ms_isAlwaysOnline
= FALSE
;
988 // of course, having a modem doesn't prevent us from having a
989 // permanent connection as well, but we have to guess somehow and
990 // it's probably more common that a system connected via a modem
991 // doesn't have any other net access, so:
992 ms_isAlwaysOnline
= FALSE
;
996 wxASSERT_MSG( ms_isAlwaysOnline
!= -1, wxT("logic error") );
998 return ms_isAlwaysOnline
!= 0;
1001 bool wxDialUpManagerMSW::IsOnline() const
1003 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1005 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1007 // user specified flag overrides our logic
1008 return ms_userSpecifiedOnlineStatus
!= 0;
1012 // return TRUE if there is at least one active connection
1013 return FindActiveConnection() != 0;
1017 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1019 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1021 ms_userSpecifiedOnlineStatus
= isOnline
;
1024 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1026 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1028 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1032 // we're running under NT 4.0, Windows 98 or later and can use
1033 // RasConnectionNotification() to be notified by a secondary thread
1035 // first, see if we don't have this thread already running
1036 if ( m_hThread
!= 0 )
1038 DWORD dwSuspendCount
= 2;
1039 while ( dwSuspendCount
> 1 )
1041 dwSuspendCount
= ResumeThread(m_hThread
);
1042 if ( dwSuspendCount
== (DWORD
)-1 )
1044 wxLogLastError(wxT("ResumeThread(RasThread)"));
1057 // create all the stuff we need to be notified about RAS connection
1062 // first create an event to wait on
1063 m_data
.hEventRas
= CreateEvent
1065 NULL
, // security attribute (default)
1066 FALSE
, // manual reset (not)
1067 FALSE
, // initial state (not signaled)
1070 if ( !m_data
.hEventRas
)
1072 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1080 // create the event we use to quit the thread
1081 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1082 if ( !m_data
.hEventQuit
)
1084 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1086 CleanUpThreadData();
1092 if ( ok
&& !ms_hwndRas
)
1094 // create a hidden window to receive notification about connections
1096 extern const wxChar
*wxPanelClassName
;
1097 ms_hwndRas
= ::CreateWindow(wxPanelClassName
, NULL
,
1100 (HMENU
)NULL
, wxGetInstance(), 0);
1103 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1105 CleanUpThreadData();
1111 FARPROC windowProc
= MakeProcInstance
1113 (FARPROC
)wxRasStatusWindowProc
,
1117 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1120 m_data
.hWnd
= ms_hwndRas
;
1124 // start the secondary thread
1125 m_data
.dialUpManager
= this;
1128 m_hThread
= CreateThread
1132 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1140 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1142 CleanUpThreadData();
1148 // start receiving RAS notifications
1149 DWORD dwRet
= ms_pfnRasConnectionNotification
1151 (HRASCONN
)INVALID_HANDLE_VALUE
,
1153 3 /* RASCN_Connection | RASCN_Disconnection */
1158 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1159 GetErrorString(dwRet
).c_str());
1161 CleanUpThreadData();
1169 // we're running under Windows 95 and have to poll ourselves
1170 // (or, alternatively, the code above for NT/98 failed)
1171 m_timerStatusPolling
.Stop();
1172 if ( nSeconds
== 0 )
1177 m_timerStatusPolling
.Start(nSeconds
* 1000);
1182 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1184 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1188 // we have running secondary thread, it's just enough to suspend it
1189 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1191 wxLogLastError(wxT("SuspendThread(RasThread)"));
1196 // even simpler - just stop the timer
1197 m_timerStatusPolling
.Stop();
1201 // ----------------------------------------------------------------------------
1202 // stubs which don't do anything in MSW version
1203 // ----------------------------------------------------------------------------
1205 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1208 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1210 // nothing to do - we don't use this
1213 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1214 const wxString
& WXUNUSED(hangup
))
1216 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1218 // nothing to do - we don't use this
1221 // ----------------------------------------------------------------------------
1223 // ----------------------------------------------------------------------------
1225 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1228 handles
[0] = data
->hEventRas
;
1229 handles
[1] = data
->hEventQuit
;
1234 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1239 // RAS connection status changed
1240 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1244 case WAIT_OBJECT_0
+ 1:
1249 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1257 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1258 WPARAM wParam
, LPARAM lParam
)
1260 if ( message
== wxWM_RAS_STATUS_CHANGED
)
1262 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1263 data
->dialUpManager
->OnConnectStatusChange();
1265 else if ( message
== wxWM_RAS_DIALING_PROGRESS
)
1267 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1269 dialUpManager
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1275 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1276 RASCONNSTATE rasconnstate
,
1279 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1281 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1283 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1284 rasconnstate
, dwError
);
1289 #endif // wxUSE_DIALUP_MANAGER