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
41 #include "wx/msw/private.h" // must be before #include "dynlib.h"
43 #if !wxUSE_DYNLIB_CLASS
44 #error You need wxUSE_DYNLIB_CLASS to be 1 to compile dialup.cpp.
47 #include "wx/dynlib.h"
49 #include "wx/dialup.h"
51 // Doesn't yet compile under VC++ 4, BC++, mingw, Watcom C++: no wininet.h
52 #if !defined(__BORLANDC__) && !defined(__GNUWIN32_OLD__) && !defined(__GNUWIN32__) && !defined(__WATCOMC__) && ! (defined(__VISUALC__) && (__VISUALC__ < 1020))
60 #ifndef INTERNET_CONNECTION_LAN
61 #define INTERNET_CONNECTION_LAN 2
63 #ifndef INTERNET_CONNECTION_PROXY
64 #define INTERNET_CONNECTION_PROXY 4
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // this message is sent by the secondary thread when RAS status changes
72 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
73 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // the signatures of RAS functions: all this is quite heavy, but we must do it
80 // to allow running wxWin programs on machine which don't have RAS installed
81 // (this does exist) - if we link with rasapi32.lib, the program will fail on
82 // startup because of the missing DLL...
85 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
86 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
87 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
88 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
89 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
90 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
91 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
92 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
93 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
94 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
95 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
96 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
97 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
98 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
99 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
100 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
101 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
102 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
103 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
105 static const wxChar gs_funcSuffix
= _T('A');
107 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
108 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
109 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
110 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
111 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
112 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
113 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
114 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
115 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
116 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
117 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
118 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
119 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
120 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
121 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
122 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
123 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
124 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
125 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
127 static const wxChar gs_funcSuffix
= _T('W');
128 #endif // ASCII/Unicode
130 // structure passed to the secondary thread
131 struct WXDLLEXPORT wxRasThreadData
136 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
137 dialUpManager
= NULL
;
140 HWND hWnd
; // window to send notifications to
141 HANDLE hEventRas
, // event which RAS signals when status changes
142 hEventQuit
; // event which we signal when we terminate
144 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
147 // ----------------------------------------------------------------------------
148 // wxDialUpManager class for MSW
149 // ----------------------------------------------------------------------------
151 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
155 wxDialUpManagerMSW();
156 virtual ~wxDialUpManagerMSW();
158 // implement base class pure virtuals
159 virtual bool IsOk() const;
160 virtual size_t GetISPNames(wxArrayString
& names
) const;
161 virtual bool Dial(const wxString
& nameOfISP
,
162 const wxString
& username
,
163 const wxString
& password
,
165 virtual bool IsDialing() const;
166 virtual bool CancelDialing();
167 virtual bool HangUp();
168 virtual bool IsAlwaysOnline() const;
169 virtual bool IsOnline() const;
170 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
171 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
172 virtual void DisableAutoCheckOnlineStatus();
173 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
174 virtual void SetConnectCommand(const wxString
& commandDial
,
175 const wxString
& commandHangup
);
178 void CheckRasStatus();
180 // for wxRasStatusWindowProc
181 void OnConnectStatusChange();
182 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
185 static HWND
GetRasWindow() { return ms_hwndRas
; }
186 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
189 // return the error string for the given RAS error code
190 static wxString
GetErrorString(DWORD error
);
192 // find the (first) handle of the active connection
193 static HRASCONN
FindActiveConnection();
195 // notify the application about status change
196 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
198 // destroy the thread data and the thread itself
199 void CleanUpThreadData();
201 // timer used for polling RAS status
202 class WXDLLEXPORT RasTimer
: public wxTimer
205 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
206 { m_dialUpManager
= dialUpManager
; }
208 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
211 wxDialUpManagerMSW
*m_dialUpManager
;
212 } m_timerStatusPolling
;
214 // thread handle for the thread sitting on connection change event
217 // data used by this thread and our hidden window to send messages between
219 wxRasThreadData m_data
;
221 // the hidden window we use for passing messages between threads
222 static HWND ms_hwndRas
;
224 // the handle of the connection we initiated or 0 if none
225 static HRASCONN ms_hRasConnection
;
227 // the use count of rasapi32.dll
228 static int ms_nDllCount
;
230 // the handle of rasapi32.dll when it's loaded
231 static wxDllType ms_dllRas
;
233 // the pointers to RAS functions
234 static RASDIAL ms_pfnRasDial
;
235 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
236 static RASENUMENTRIES ms_pfnRasEnumEntries
;
237 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
238 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
239 static RASHANGUP ms_pfnRasHangUp
;
240 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
241 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
242 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
243 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
244 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
245 static RASENUMDEVICES ms_pfnRasEnumDevices
;
246 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
247 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
248 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
249 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
250 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
251 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
253 // this function is not supported by Win95
254 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
256 // if this flag is different from -1, it overrides IsOnline()
257 static int ms_userSpecifiedOnlineStatus
;
259 // this flag tells us if we're online
260 static int ms_isConnected
;
262 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
264 static int ms_isAlwaysOnline
;
266 // this flag tells us whether a call to RasDial() is in progress
267 static wxDialUpManagerMSW
*ms_dialer
;
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
275 WPARAM wParam
, LPARAM lParam
);
277 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
279 static void WINAPI
wxRasDialFunc(UINT unMsg
,
280 RASCONNSTATE rasconnstate
,
283 // ============================================================================
285 // ============================================================================
287 // ----------------------------------------------------------------------------
288 // init the static variables
289 // ----------------------------------------------------------------------------
291 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
293 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
295 int wxDialUpManagerMSW::ms_nDllCount
= 0;
296 wxDllType
wxDialUpManagerMSW::ms_dllRas
= 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 int wxDialUpManagerMSW::ms_isAlwaysOnline
= -1;
321 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
323 // ----------------------------------------------------------------------------
324 // ctor and dtor: the dynamic linking happens here
325 // ----------------------------------------------------------------------------
327 // the static creator function is implemented here
328 wxDialUpManager
*wxDialUpManager::Create()
330 return new wxDialUpManagerMSW
;
334 // warning about "'this' : used in base member initializer list" - so what?
335 #pragma warning(disable:4355)
338 wxDialUpManagerMSW::wxDialUpManagerMSW()
339 : m_timerStatusPolling(this)
341 // initialize our data
344 if ( !ms_nDllCount
++ )
346 // load the RAS library
347 ms_dllRas
= wxDllLoader::LoadLibrary("RASAPI32");
350 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
354 // resolve the functions we need
356 // this will contain the name of the function we failed to resolve
358 const char *funcName
= NULL
;
360 // get the function from rasapi32.dll and abort if it's not found
361 #define RESOLVE_RAS_FUNCTION(type, name) \
362 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
363 wxString(_T(#name)) + gs_funcSuffix); \
364 if ( !ms_pfn##name ) \
370 // a variant of above macro which doesn't abort if the function is
371 // not found in the DLL
372 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
373 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
374 wxString(_T(#name)) + gs_funcSuffix);
376 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
377 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
378 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
379 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
380 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
381 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
382 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
384 // suppress wxDllLoader messages about missing (non essential)
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
);
417 wxDllLoader::UnloadLibrary(ms_dllRas
);
426 // enable auto check by default
427 EnableAutoCheckOnlineStatus(0);
430 wxDialUpManagerMSW::~wxDialUpManagerMSW()
434 if ( !--ms_nDllCount
)
436 // unload the RAS library
437 wxDllLoader::UnloadLibrary(ms_dllRas
);
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
448 wxChar buffer
[512]; // this should be more than enough according to MS docs
449 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
452 case ERROR_INVALID_PARAMETER
:
453 // this was a standard Win32 error probably
454 return wxString(wxSysErrorMsg(error
));
459 _("Failed to retrieve text of RAS error message"));
462 msg
.Printf(_("unknown error (error code %08x)."), error
);
467 // we want the error message to start from a lower case letter
468 buffer
[0] = wxTolower(buffer
[0]);
470 return wxString(buffer
);
474 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
476 // enumerate connections
477 DWORD cbBuf
= sizeof(RASCONN
);
478 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
485 lpRasConn
->dwSize
= sizeof(RASCONN
);
487 DWORD nConnections
= 0;
488 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
490 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
492 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
494 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
496 LPRASCONN lpRasConnOld
= lpRasConn
;
497 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
506 else if ( dwRet
== 0 )
514 wxLogError(_("Cannot find active dialup connection: %s"),
515 GetErrorString(dwRet
));
522 switch ( nConnections
)
530 // more than 1 connection - we don't know what to do with this
531 // case, so give a warning but continue (taking the first
532 // connection) - the warning is really needed because this function
533 // is used, for example, to select the connection to hang up and so
534 // we may hang up the wrong connection here...
535 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
539 // exactly 1 connection, great
540 hrasconn
= lpRasConn
->hrasconn
;
548 void wxDialUpManagerMSW::CleanUpThreadData()
552 if ( !SetEvent(m_data
.hEventQuit
) )
554 wxLogLastError("SetEvent(RasThreadQuit)");
557 CloseHandle(m_hThread
);
564 DestroyWindow(m_data
.hWnd
);
569 if ( m_data
.hEventQuit
)
571 CloseHandle(m_data
.hEventQuit
);
573 m_data
.hEventQuit
= 0;
576 if ( m_data
.hEventRas
)
578 CloseHandle(m_data
.hEventRas
);
580 m_data
.hEventRas
= 0;
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 void wxDialUpManagerMSW::CheckRasStatus()
590 // use int, not bool to compare with -1
591 int isConnected
= FindActiveConnection() != 0;
592 if ( isConnected
!= ms_isConnected
)
594 if ( ms_isConnected
!= -1 )
596 // notify the program
597 NotifyApp(isConnected
!= 0);
599 // else: it's the first time we're called, just update the flag
601 ms_isConnected
= isConnected
;
605 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
607 wxDialUpEvent
event(connected
, fromOurselves
);
608 (void)wxTheApp
->ProcessEvent(event
);
611 // this function is called whenever the status of any RAS connection on this
612 // machine changes by RAS itself
613 void wxDialUpManagerMSW::OnConnectStatusChange()
615 // we know that status changed, but we don't know whether we're connected
616 // or not - so find it out
620 // this function is called by our callback which we give to RasDial() when
621 // calling it asynchronously
622 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
627 // this probably means that CancelDialing() was called and we get
628 // "disconnected" notification
632 // we're only interested in 2 events: connected and disconnected
635 wxLogError(_("Failed to establish dialup connection: %s"),
636 GetErrorString(dwError
));
638 // we should still call RasHangUp() if we got a non 0 connection
639 if ( ms_hRasConnection
)
641 ms_pfnRasHangUp(ms_hRasConnection
);
642 ms_hRasConnection
= 0;
647 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
649 else if ( rasconnstate
== RASCS_Connected
)
651 ms_isConnected
= TRUE
;
654 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
658 // ----------------------------------------------------------------------------
659 // implementation of wxDialUpManager functions
660 // ----------------------------------------------------------------------------
662 bool wxDialUpManagerMSW::IsOk() const
664 return ms_dllRas
!= 0;
667 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
670 DWORD size
= sizeof(RASENTRYNAME
);
671 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
672 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
678 dwRet
= ms_pfnRasEnumEntries
681 NULL
, // default phone book (or all)
682 rasEntries
, // [out] buffer for the entries
683 &size
, // [in/out] size of the buffer
684 &nEntries
// [out] number of entries fetched
687 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
689 // reallocate the buffer
690 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
692 else if ( dwRet
!= 0 )
694 // some other error - abort
695 wxLogError(_("Failed to get ISP names: %s"), GetErrorString(dwRet
));
702 while ( dwRet
!= 0 );
706 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
708 names
.Add(rasEntries
[n
].szEntryName
);
713 // return the number of entries
714 return names
.GetCount();
717 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
718 const wxString
& username
,
719 const wxString
& password
,
722 // check preconditions
723 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
725 if ( ms_hRasConnection
)
727 wxFAIL_MSG(wxT("there is already an active connection"));
732 // get the default ISP if none given
733 wxString
entryName(nameOfISP
);
737 size_t count
= GetISPNames(names
);
741 // no known ISPs, abort
742 wxLogError(_("Failed to connect: no ISP to dial."));
747 // only one ISP, choose it
748 entryName
= names
[0u];
752 // several ISPs, let the user choose
754 wxString
*strings
= new wxString
[count
];
755 for ( size_t i
= 0; i
< count
; i
++ )
757 strings
[i
] = names
[i
];
760 entryName
= wxGetSingleChoice
762 _("Choose ISP to dial"),
763 _("Please choose which ISP do you want to connect to"),
779 RASDIALPARAMS rasDialParams
;
780 rasDialParams
.dwSize
= sizeof(rasDialParams
);
781 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
783 // do we have the username and password?
784 if ( !username
|| !password
)
787 DWORD dwRet
= ms_pfnRasGetEntryDialParams
789 NULL
, // default phonebook
790 &rasDialParams
, // [in/out] the params of this entry
791 &gotPassword
// [out] did we get password?
796 wxLogError(_("Failed to connect: missing username/password."));
803 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
804 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
807 // default values for other fields
808 rasDialParams
.szPhoneNumber
[0] = '\0';
809 rasDialParams
.szCallbackNumber
[0] = '\0';
810 rasDialParams
.szCallbackNumber
[0] = '\0';
812 rasDialParams
.szDomain
[0] = '*';
813 rasDialParams
.szDomain
[1] = '\0';
815 // apparently, this is not really necessary - passing NULL instead of the
816 // phone book has the same effect
819 if ( wxGetOsVersion() == wxWINDOWS_NT
)
821 // first get the length
822 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
825 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
827 wxLogSysError(_("Cannot find the location of address book file"));
830 phoneBook
.UngetWriteBuf();
832 // this is the default phone book
833 phoneBook
<< "\\ras\\rasphone.pbk";
837 // TODO may be we should disable auto check while async dialing is in
842 DWORD dwRet
= ms_pfnRasDial
844 (LPRASDIALEXTENSIONS
)NULL
, // no extended features
845 NULL
, // default phone book file (NT only)
847 0, // use callback for notifications
848 async
? wxRasDialFunc
// the callback
849 : 0, // no notifications - sync operation
855 wxLogError(_("Failed to %s dialup connection: %s"),
856 async
? _("initiate") : _("establish"),
857 GetErrorString(dwRet
));
859 // we should still call RasHangUp() if we got a non 0 connection
860 if ( ms_hRasConnection
)
862 ms_pfnRasHangUp(ms_hRasConnection
);
863 ms_hRasConnection
= 0;
871 // for async dialing, we're not yet connected
874 ms_isConnected
= TRUE
;
880 bool wxDialUpManagerMSW::IsDialing() const
882 return GetDialer() != NULL
;
885 bool wxDialUpManagerMSW::CancelDialing()
893 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
900 bool wxDialUpManagerMSW::HangUp()
902 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
904 // we may terminate either the connection we initiated or another one which
907 if ( ms_hRasConnection
)
909 hRasConn
= ms_hRasConnection
;
911 ms_hRasConnection
= 0;
915 hRasConn
= FindActiveConnection();
920 wxLogError(_("Cannot hang up - no active dialup connection."));
925 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
928 wxLogError(_("Failed to terminate the dialup connection: %s"),
929 GetErrorString(dwRet
));
932 ms_isConnected
= FALSE
;
937 bool wxDialUpManagerMSW::IsAlwaysOnline() const
939 // we cache the result (presumably this won't change while the program is
941 if ( ms_isAlwaysOnline
!= -1 )
943 return ms_isAlwaysOnline
!= 0;
946 // try to use WinInet function first
948 wxDllType hDll
= wxDllLoader::LoadLibrary(_T("WININET"), &ok
);
951 typedef BOOL (*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
952 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
954 #define RESOLVE_FUNCTION(type, name) \
955 pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name))
957 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
959 if ( pfnInternetGetConnectedState
)
962 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
964 // there is some connection to the net, see of which type
965 ms_isAlwaysOnline
= (flags
& INTERNET_CONNECTION_LAN
!= 0) ||
966 (flags
& INTERNET_CONNECTION_PROXY
!= 0);
970 // no Internet connection at all
971 ms_isAlwaysOnline
= FALSE
;
975 wxDllLoader::UnloadLibrary(hDll
);
978 // did we succeed with WinInet? if not, try something else
979 if ( ms_isAlwaysOnline
== -1 )
983 // definitely no permanent connection because we are not connected
985 ms_isAlwaysOnline
= FALSE
;
989 // of course, having a modem doesn't prevent us from having a
990 // permanent connection as well, but we have to guess somehow and
991 // it's probably more common that a system connected via a modem
992 // doesn't have any other net access, so:
993 ms_isAlwaysOnline
= FALSE
;
997 wxASSERT_MSG( ms_isAlwaysOnline
!= -1, wxT("logic error") );
999 return ms_isAlwaysOnline
!= 0;
1002 bool wxDialUpManagerMSW::IsOnline() const
1004 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1006 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1008 // user specified flag overrides our logic
1009 return ms_userSpecifiedOnlineStatus
!= 0;
1013 // return TRUE if there is at least one active connection
1014 return FindActiveConnection() != 0;
1018 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1020 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1022 ms_userSpecifiedOnlineStatus
= isOnline
;
1025 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1027 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1029 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1033 // we're running under NT 4.0, Windows 98 or later and can use
1034 // RasConnectionNotification() to be notified by a secondary thread
1036 // first, see if we don't have this thread already running
1037 if ( m_hThread
!= 0 )
1039 DWORD dwSuspendCount
= 2;
1040 while ( dwSuspendCount
> 1 )
1042 dwSuspendCount
= ResumeThread(m_hThread
);
1043 if ( dwSuspendCount
== (DWORD
)-1 )
1045 wxLogLastError(wxT("ResumeThread(RasThread)"));
1058 // create all the stuff we need to be notified about RAS connection
1063 // first create an event to wait on
1064 m_data
.hEventRas
= CreateEvent
1066 NULL
, // security attribute (default)
1067 FALSE
, // manual reset (not)
1068 FALSE
, // initial state (not signaled)
1071 if ( !m_data
.hEventRas
)
1073 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1081 // create the event we use to quit the thread
1082 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1083 if ( !m_data
.hEventQuit
)
1085 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1087 CleanUpThreadData();
1093 if ( ok
&& !ms_hwndRas
)
1095 // create a hidden window to receive notification about connections
1097 extern const wxChar
*wxPanelClassName
;
1098 ms_hwndRas
= ::CreateWindow(wxPanelClassName
, NULL
,
1101 (HMENU
)NULL
, wxGetInstance(), 0);
1104 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1106 CleanUpThreadData();
1112 FARPROC windowProc
= MakeProcInstance
1114 (FARPROC
)wxRasStatusWindowProc
,
1118 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1121 m_data
.hWnd
= ms_hwndRas
;
1125 // start the secondary thread
1126 m_data
.dialUpManager
= this;
1129 m_hThread
= CreateThread
1133 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1141 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1143 CleanUpThreadData();
1149 // start receiving RAS notifications
1150 DWORD dwRet
= ms_pfnRasConnectionNotification
1152 (HRASCONN
)INVALID_HANDLE_VALUE
,
1154 3 /* RASCN_Connection | RASCN_Disconnection */
1159 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1160 GetErrorString(dwRet
));
1162 CleanUpThreadData();
1170 // we're running under Windows 95 and have to poll ourselves
1171 // (or, alternatively, the code above for NT/98 failed)
1172 m_timerStatusPolling
.Stop();
1173 if ( nSeconds
== 0 )
1178 m_timerStatusPolling
.Start(nSeconds
* 1000);
1183 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1185 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1189 // we have running secondary thread, it's just enough to suspend it
1190 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1192 wxLogLastError(wxT("SuspendThread(RasThread)"));
1197 // even simpler - just stop the timer
1198 m_timerStatusPolling
.Stop();
1202 // ----------------------------------------------------------------------------
1203 // stubs which don't do anything in MSW version
1204 // ----------------------------------------------------------------------------
1206 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1209 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1211 // nothing to do - we don't use this
1214 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1215 const wxString
& WXUNUSED(hangup
))
1217 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1219 // nothing to do - we don't use this
1222 // ----------------------------------------------------------------------------
1224 // ----------------------------------------------------------------------------
1226 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1229 handles
[0] = data
->hEventRas
;
1230 handles
[1] = data
->hEventQuit
;
1235 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1240 // RAS connection status changed
1241 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1245 case WAIT_OBJECT_0
+ 1:
1250 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1258 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1259 WPARAM wParam
, LPARAM lParam
)
1261 if ( message
== wxWM_RAS_STATUS_CHANGED
)
1263 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1264 data
->dialUpManager
->OnConnectStatusChange();
1266 else if ( message
== wxWM_RAS_DIALING_PROGRESS
)
1268 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1270 dialUpManager
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1276 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1277 RASCONNSTATE rasconnstate
,
1280 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1282 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1284 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1285 rasconnstate
, dwError
);
1290 #endif // wxUSE_DIALUP_MANAGER