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/msw/private.h" // must be before #include "dynlib.h"
47 #if !wxUSE_DYNLIB_CLASS
48 #error You need wxUSE_DYNLIB_CLASS to be 1 to compile dialup.cpp.
51 #include "wx/dynlib.h"
53 #include "wx/dialup.h"
55 // Doesn't yet compile under VC++ 4, BC++, mingw, Watcom C++: no wininet.h
56 #if !defined(__BORLANDC__) && !defined(__GNUWIN32_OLD__) && !defined(__GNUWIN32__) && !defined(__WATCOMC__) && ! (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 hidden window we use for passing messages between threads
226 static HWND ms_hwndRas
;
228 // the handle of the connection we initiated or 0 if none
229 static HRASCONN ms_hRasConnection
;
231 // the use count of rasapi32.dll
232 static int ms_nDllCount
;
234 // the handle of rasapi32.dll when it's loaded
235 static wxDllType ms_dllRas
;
237 // the pointers to RAS functions
238 static RASDIAL ms_pfnRasDial
;
239 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
240 static RASENUMENTRIES ms_pfnRasEnumEntries
;
241 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
242 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
243 static RASHANGUP ms_pfnRasHangUp
;
244 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
245 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
246 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
247 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
248 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
249 static RASENUMDEVICES ms_pfnRasEnumDevices
;
250 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
251 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
252 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
253 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
254 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
255 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
257 // this function is not supported by Win95
258 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
260 // if this flag is different from -1, it overrides IsOnline()
261 static int ms_userSpecifiedOnlineStatus
;
263 // this flag tells us if we're online
264 static int ms_isConnected
;
266 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
268 static int ms_isAlwaysOnline
;
270 // this flag tells us whether a call to RasDial() is in progress
271 static wxDialUpManagerMSW
*ms_dialer
;
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
279 WPARAM wParam
, LPARAM lParam
);
281 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
283 static void WINAPI
wxRasDialFunc(UINT unMsg
,
284 RASCONNSTATE rasconnstate
,
287 // ============================================================================
289 // ============================================================================
291 // ----------------------------------------------------------------------------
292 // init the static variables
293 // ----------------------------------------------------------------------------
295 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
297 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
299 int wxDialUpManagerMSW::ms_nDllCount
= 0;
300 wxDllType
wxDialUpManagerMSW::ms_dllRas
= 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)
345 // initialize our data
348 if ( !ms_nDllCount
++ )
350 // load the RAS library
351 ms_dllRas
= wxDllLoader::LoadLibrary("RASAPI32");
354 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
358 // resolve the functions we need
360 // this will contain the name of the function we failed to resolve
362 const char *funcName
= NULL
;
364 // get the function from rasapi32.dll and abort if it's not found
365 #define RESOLVE_RAS_FUNCTION(type, name) \
366 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
367 wxString(_T(#name)) + gs_funcSuffix); \
368 if ( !ms_pfn##name ) \
374 // a variant of above macro which doesn't abort if the function is
375 // not found in the DLL
376 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
377 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
378 wxString(_T(#name)) + gs_funcSuffix);
380 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
381 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
382 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
383 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
384 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
385 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
386 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
388 // suppress wxDllLoader messages about missing (non essential)
393 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
394 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
395 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
396 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
397 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
398 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
399 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
400 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
401 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
402 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
403 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
404 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
407 // keep your preprocessor name space clean
408 #undef RESOLVE_RAS_FUNCTION
409 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
414 static const wxChar
*msg
= wxTRANSLATE(
415 "The version of remote access service (RAS) installed on this machine is too\
416 old, please upgrade (the following required function is missing: %s)."
419 wxLogError(wxGetTranslation(msg
), funcName
);
421 wxDllLoader::UnloadLibrary(ms_dllRas
);
430 // enable auto check by default
431 EnableAutoCheckOnlineStatus(0);
434 wxDialUpManagerMSW::~wxDialUpManagerMSW()
438 if ( !--ms_nDllCount
)
440 // unload the RAS library
441 wxDllLoader::UnloadLibrary(ms_dllRas
);
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
452 wxChar buffer
[512]; // this should be more than enough according to MS docs
453 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
456 case ERROR_INVALID_PARAMETER
:
457 // this was a standard Win32 error probably
458 return wxString(wxSysErrorMsg(error
));
463 _("Failed to retrieve text of RAS error message"));
466 msg
.Printf(_("unknown error (error code %08x)."), error
);
471 // we want the error message to start from a lower case letter
472 buffer
[0] = wxTolower(buffer
[0]);
474 return wxString(buffer
);
478 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
480 // enumerate connections
481 DWORD cbBuf
= sizeof(RASCONN
);
482 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
489 lpRasConn
->dwSize
= sizeof(RASCONN
);
491 DWORD nConnections
= 0;
492 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
494 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
496 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
498 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
500 LPRASCONN lpRasConnOld
= lpRasConn
;
501 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
510 else if ( dwRet
== 0 )
518 wxLogError(_("Cannot find active dialup connection: %s"),
519 GetErrorString(dwRet
).c_str());
526 switch ( nConnections
)
534 // more than 1 connection - we don't know what to do with this
535 // case, so give a warning but continue (taking the first
536 // connection) - the warning is really needed because this function
537 // is used, for example, to select the connection to hang up and so
538 // we may hang up the wrong connection here...
539 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
543 // exactly 1 connection, great
544 hrasconn
= lpRasConn
->hrasconn
;
552 void wxDialUpManagerMSW::CleanUpThreadData()
556 if ( !SetEvent(m_data
.hEventQuit
) )
558 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
561 CloseHandle(m_hThread
);
568 DestroyWindow(m_data
.hWnd
);
573 if ( m_data
.hEventQuit
)
575 CloseHandle(m_data
.hEventQuit
);
577 m_data
.hEventQuit
= 0;
580 if ( m_data
.hEventRas
)
582 CloseHandle(m_data
.hEventRas
);
584 m_data
.hEventRas
= 0;
588 // ----------------------------------------------------------------------------
590 // ----------------------------------------------------------------------------
592 void wxDialUpManagerMSW::CheckRasStatus()
594 // use int, not bool to compare with -1
595 int isConnected
= FindActiveConnection() != 0;
596 if ( isConnected
!= ms_isConnected
)
598 if ( ms_isConnected
!= -1 )
600 // notify the program
601 NotifyApp(isConnected
!= 0);
603 // else: it's the first time we're called, just update the flag
605 ms_isConnected
= isConnected
;
609 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
611 wxDialUpEvent
event(connected
, fromOurselves
);
612 (void)wxTheApp
->ProcessEvent(event
);
615 // this function is called whenever the status of any RAS connection on this
616 // machine changes by RAS itself
617 void wxDialUpManagerMSW::OnConnectStatusChange()
619 // we know that status changed, but we don't know whether we're connected
620 // or not - so find it out
624 // this function is called by our callback which we give to RasDial() when
625 // calling it asynchronously
626 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
631 // this probably means that CancelDialing() was called and we get
632 // "disconnected" notification
636 // we're only interested in 2 events: connected and disconnected
639 wxLogError(_("Failed to establish dialup connection: %s"),
640 GetErrorString(dwError
).c_str());
642 // we should still call RasHangUp() if we got a non 0 connection
643 if ( ms_hRasConnection
)
645 ms_pfnRasHangUp(ms_hRasConnection
);
646 ms_hRasConnection
= 0;
651 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
653 else if ( rasconnstate
== RASCS_Connected
)
655 ms_isConnected
= TRUE
;
658 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
662 // ----------------------------------------------------------------------------
663 // implementation of wxDialUpManager functions
664 // ----------------------------------------------------------------------------
666 bool wxDialUpManagerMSW::IsOk() const
668 return ms_dllRas
!= 0;
671 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
674 DWORD size
= sizeof(RASENTRYNAME
);
675 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
676 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
682 dwRet
= ms_pfnRasEnumEntries
685 NULL
, // default phone book (or all)
686 rasEntries
, // [out] buffer for the entries
687 &size
, // [in/out] size of the buffer
688 &nEntries
// [out] number of entries fetched
691 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
693 // reallocate the buffer
694 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
696 else if ( dwRet
!= 0 )
698 // some other error - abort
699 wxLogError(_("Failed to get ISP names: %s"),
700 GetErrorString(dwRet
).c_str());
707 while ( dwRet
!= 0 );
711 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
713 names
.Add(rasEntries
[n
].szEntryName
);
718 // return the number of entries
719 return names
.GetCount();
722 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
723 const wxString
& username
,
724 const wxString
& password
,
727 // check preconditions
728 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
730 if ( ms_hRasConnection
)
732 wxFAIL_MSG(wxT("there is already an active connection"));
737 // get the default ISP if none given
738 wxString
entryName(nameOfISP
);
742 size_t count
= GetISPNames(names
);
746 // no known ISPs, abort
747 wxLogError(_("Failed to connect: no ISP to dial."));
752 // only one ISP, choose it
753 entryName
= names
[0u];
757 // several ISPs, let the user choose
759 wxString
*strings
= new wxString
[count
];
760 for ( size_t i
= 0; i
< count
; i
++ )
762 strings
[i
] = names
[i
];
765 entryName
= wxGetSingleChoice
767 _("Choose ISP to dial"),
768 _("Please choose which ISP do you want to connect to"),
784 RASDIALPARAMS rasDialParams
;
785 rasDialParams
.dwSize
= sizeof(rasDialParams
);
786 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
788 // do we have the username and password?
789 if ( !username
|| !password
)
792 DWORD dwRet
= ms_pfnRasGetEntryDialParams
794 NULL
, // default phonebook
795 &rasDialParams
, // [in/out] the params of this entry
796 &gotPassword
// [out] did we get password?
801 wxLogError(_("Failed to connect: missing username/password."));
808 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
809 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
812 // default values for other fields
813 rasDialParams
.szPhoneNumber
[0] = '\0';
814 rasDialParams
.szCallbackNumber
[0] = '\0';
815 rasDialParams
.szCallbackNumber
[0] = '\0';
817 rasDialParams
.szDomain
[0] = '*';
818 rasDialParams
.szDomain
[1] = '\0';
820 // apparently, this is not really necessary - passing NULL instead of the
821 // phone book has the same effect
824 if ( wxGetOsVersion() == wxWINDOWS_NT
)
826 // first get the length
827 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
830 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
832 wxLogSysError(_("Cannot find the location of address book file"));
835 phoneBook
.UngetWriteBuf();
837 // this is the default phone book
838 phoneBook
<< "\\ras\\rasphone.pbk";
842 // TODO may be we should disable auto check while async dialing is in
847 DWORD dwRet
= ms_pfnRasDial
849 (LPRASDIALEXTENSIONS
)NULL
, // no extended features
850 NULL
, // default phone book file (NT only)
852 0, // use callback for notifications
853 async
? wxRasDialFunc
// the callback
854 : 0, // no notifications - sync operation
860 wxLogError(_("Failed to %s dialup connection: %s"),
861 async
? _("initiate") : _("establish"),
862 GetErrorString(dwRet
).c_str());
864 // we should still call RasHangUp() if we got a non 0 connection
865 if ( ms_hRasConnection
)
867 ms_pfnRasHangUp(ms_hRasConnection
);
868 ms_hRasConnection
= 0;
876 // for async dialing, we're not yet connected
879 ms_isConnected
= TRUE
;
885 bool wxDialUpManagerMSW::IsDialing() const
887 return GetDialer() != NULL
;
890 bool wxDialUpManagerMSW::CancelDialing()
898 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
905 bool wxDialUpManagerMSW::HangUp()
907 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
909 // we may terminate either the connection we initiated or another one which
912 if ( ms_hRasConnection
)
914 hRasConn
= ms_hRasConnection
;
916 ms_hRasConnection
= 0;
920 hRasConn
= FindActiveConnection();
925 wxLogError(_("Cannot hang up - no active dialup connection."));
930 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
933 wxLogError(_("Failed to terminate the dialup connection: %s"),
934 GetErrorString(dwRet
).c_str());
937 ms_isConnected
= FALSE
;
942 bool wxDialUpManagerMSW::IsAlwaysOnline() const
944 // we cache the result (presumably this won't change while the program is
946 if ( ms_isAlwaysOnline
!= -1 )
948 return ms_isAlwaysOnline
!= 0;
951 // try to use WinInet function first
953 wxDllType hDll
= wxDllLoader::LoadLibrary(_T("WININET"), &ok
);
956 typedef BOOL (*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
957 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
959 #define RESOLVE_FUNCTION(type, name) \
960 pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name))
962 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
964 if ( pfnInternetGetConnectedState
)
967 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
969 // there is some connection to the net, see of which type
970 ms_isAlwaysOnline
= (flags
& INTERNET_CONNECTION_LAN
!= 0) ||
971 (flags
& INTERNET_CONNECTION_PROXY
!= 0);
975 // no Internet connection at all
976 ms_isAlwaysOnline
= FALSE
;
980 wxDllLoader::UnloadLibrary(hDll
);
983 // did we succeed with WinInet? if not, try something else
984 if ( ms_isAlwaysOnline
== -1 )
988 // definitely no permanent connection because we are not connected
990 ms_isAlwaysOnline
= FALSE
;
994 // of course, having a modem doesn't prevent us from having a
995 // permanent connection as well, but we have to guess somehow and
996 // it's probably more common that a system connected via a modem
997 // doesn't have any other net access, so:
998 ms_isAlwaysOnline
= FALSE
;
1002 wxASSERT_MSG( ms_isAlwaysOnline
!= -1, wxT("logic error") );
1004 return ms_isAlwaysOnline
!= 0;
1007 bool wxDialUpManagerMSW::IsOnline() const
1009 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1011 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1013 // user specified flag overrides our logic
1014 return ms_userSpecifiedOnlineStatus
!= 0;
1018 // return TRUE if there is at least one active connection
1019 return FindActiveConnection() != 0;
1023 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1025 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1027 ms_userSpecifiedOnlineStatus
= isOnline
;
1030 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1032 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1034 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1038 // we're running under NT 4.0, Windows 98 or later and can use
1039 // RasConnectionNotification() to be notified by a secondary thread
1041 // first, see if we don't have this thread already running
1042 if ( m_hThread
!= 0 )
1044 DWORD dwSuspendCount
= 2;
1045 while ( dwSuspendCount
> 1 )
1047 dwSuspendCount
= ResumeThread(m_hThread
);
1048 if ( dwSuspendCount
== (DWORD
)-1 )
1050 wxLogLastError(wxT("ResumeThread(RasThread)"));
1063 // create all the stuff we need to be notified about RAS connection
1068 // first create an event to wait on
1069 m_data
.hEventRas
= CreateEvent
1071 NULL
, // security attribute (default)
1072 FALSE
, // manual reset (not)
1073 FALSE
, // initial state (not signaled)
1076 if ( !m_data
.hEventRas
)
1078 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1086 // create the event we use to quit the thread
1087 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1088 if ( !m_data
.hEventQuit
)
1090 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1092 CleanUpThreadData();
1098 if ( ok
&& !ms_hwndRas
)
1100 // create a hidden window to receive notification about connections
1102 extern const wxChar
*wxPanelClassName
;
1103 ms_hwndRas
= ::CreateWindow(wxPanelClassName
, NULL
,
1106 (HMENU
)NULL
, wxGetInstance(), 0);
1109 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1111 CleanUpThreadData();
1117 FARPROC windowProc
= MakeProcInstance
1119 (FARPROC
)wxRasStatusWindowProc
,
1123 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1126 m_data
.hWnd
= ms_hwndRas
;
1130 // start the secondary thread
1131 m_data
.dialUpManager
= this;
1134 m_hThread
= CreateThread
1138 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1146 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1148 CleanUpThreadData();
1154 // start receiving RAS notifications
1155 DWORD dwRet
= ms_pfnRasConnectionNotification
1157 (HRASCONN
)INVALID_HANDLE_VALUE
,
1159 3 /* RASCN_Connection | RASCN_Disconnection */
1164 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1165 GetErrorString(dwRet
).c_str());
1167 CleanUpThreadData();
1175 // we're running under Windows 95 and have to poll ourselves
1176 // (or, alternatively, the code above for NT/98 failed)
1177 m_timerStatusPolling
.Stop();
1178 if ( nSeconds
== 0 )
1183 m_timerStatusPolling
.Start(nSeconds
* 1000);
1188 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1190 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1194 // we have running secondary thread, it's just enough to suspend it
1195 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1197 wxLogLastError(wxT("SuspendThread(RasThread)"));
1202 // even simpler - just stop the timer
1203 m_timerStatusPolling
.Stop();
1207 // ----------------------------------------------------------------------------
1208 // stubs which don't do anything in MSW version
1209 // ----------------------------------------------------------------------------
1211 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1214 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1216 // nothing to do - we don't use this
1219 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1220 const wxString
& WXUNUSED(hangup
))
1222 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1224 // nothing to do - we don't use this
1227 // ----------------------------------------------------------------------------
1229 // ----------------------------------------------------------------------------
1231 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1234 handles
[0] = data
->hEventRas
;
1235 handles
[1] = data
->hEventQuit
;
1240 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1245 // RAS connection status changed
1246 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1250 case WAIT_OBJECT_0
+ 1:
1255 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1263 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1264 WPARAM wParam
, LPARAM lParam
)
1266 if ( message
== wxWM_RAS_STATUS_CHANGED
)
1268 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1269 data
->dialUpManager
->OnConnectStatusChange();
1271 else if ( message
== wxWM_RAS_DIALING_PROGRESS
)
1273 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1275 dialUpManager
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1281 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1282 RASCONNSTATE rasconnstate
,
1285 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1287 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1289 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1290 rasconnstate
, dwError
);
1295 #endif // wxUSE_DIALUP_MANAGER