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 #if wxUSE_DIALUP_MANAGER
37 #include "wx/generic/choicdgg.h"
39 #include "wx/dynlib.h"
40 #include "wx/dialup.h"
42 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED
)
43 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED
)
45 // Doesn't yet compile under VC++ 4, BC++, Watcom C++,
47 #if (!defined(__BORLANDC__) || (__BORLANDC__>=0x550)) && \
48 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
49 !defined(__GNUWIN32_OLD__) && \
50 !defined(__WINE__) && \
51 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
59 #ifndef INTERNET_CONNECTION_LAN
60 #define INTERNET_CONNECTION_LAN 2
62 #ifndef INTERNET_CONNECTION_PROXY
63 #define INTERNET_CONNECTION_PROXY 4
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // this message is sent by the secondary thread when RAS status changes
71 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
72 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // the signatures of RAS functions: all this is quite heavy, but we must do it
79 // to allow running wxWin programs on machine which don't have RAS installed
80 // (this does exist) - if we link with rasapi32.lib, the program will fail on
81 // startup because of the missing DLL...
84 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
85 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
86 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
87 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
88 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
89 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
90 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
91 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
92 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
93 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
94 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
95 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
96 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
97 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
98 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
99 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
100 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
101 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
102 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
104 static const wxChar gs_funcSuffix
= _T('A');
106 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
107 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
108 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
109 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
110 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
111 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
112 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
113 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
114 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
115 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
116 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
117 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
118 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
119 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
120 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
121 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
122 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
123 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
124 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
126 static const wxChar gs_funcSuffix
= _T('W');
127 #endif // ASCII/Unicode
129 // structure passed to the secondary thread
130 struct WXDLLEXPORT wxRasThreadData
137 dialUpManager
= NULL
;
146 CloseHandle(hEventQuit
);
149 CloseHandle(hEventRas
);
152 HWND hWnd
; // window to send notifications to
153 HANDLE hEventRas
, // automatic event which RAS signals when status changes
154 hEventQuit
; // manual event which we signal when we terminate
156 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
159 // ----------------------------------------------------------------------------
160 // wxDialUpManager class for MSW
161 // ----------------------------------------------------------------------------
163 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
167 wxDialUpManagerMSW();
168 virtual ~wxDialUpManagerMSW();
170 // implement base class pure virtuals
171 virtual bool IsOk() const;
172 virtual size_t GetISPNames(wxArrayString
& names
) const;
173 virtual bool Dial(const wxString
& nameOfISP
,
174 const wxString
& username
,
175 const wxString
& password
,
177 virtual bool IsDialing() const;
178 virtual bool CancelDialing();
179 virtual bool HangUp();
180 virtual bool IsAlwaysOnline() const;
181 virtual bool IsOnline() const;
182 virtual void SetOnlineStatus(bool isOnline
= true);
183 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
184 virtual void DisableAutoCheckOnlineStatus();
185 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
186 virtual void SetConnectCommand(const wxString
& commandDial
,
187 const wxString
& commandHangup
);
190 void CheckRasStatus();
192 // for wxRasStatusWindowProc
193 void OnConnectStatusChange();
194 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
197 static HWND
GetRasWindow() { return ms_hwndRas
; }
198 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
201 // return the error string for the given RAS error code
202 static wxString
GetErrorString(DWORD error
);
204 // find the (first) handle of the active connection
205 static HRASCONN
FindActiveConnection();
207 // notify the application about status change
208 void NotifyApp(bool connected
, bool fromOurselves
= false) const;
210 // destroy the thread data and the thread itself
211 void CleanUpThreadData();
213 // number of times EnableAutoCheckOnlineStatus() had been called minus the
214 // number of times DisableAutoCheckOnlineStatus() had been called
215 int m_autoCheckLevel
;
217 // timer used for polling RAS status
218 class WXDLLEXPORT RasTimer
: public wxTimer
221 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
222 { m_dialUpManager
= dialUpManager
; }
224 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
227 wxDialUpManagerMSW
*m_dialUpManager
;
229 DECLARE_NO_COPY_CLASS(RasTimer
)
230 } m_timerStatusPolling
;
232 // thread handle for the thread sitting on connection change event
235 // data used by this thread and our hidden window to send messages between
237 wxRasThreadData
*m_data
;
239 // the handle of rasapi32.dll when it's loaded
240 wxDynamicLibrary m_dllRas
;
242 // the hidden window we use for passing messages between threads
243 static HWND ms_hwndRas
;
245 // the handle of the connection we initiated or 0 if none
246 static HRASCONN ms_hRasConnection
;
248 // the pointers to RAS functions
249 static RASDIAL ms_pfnRasDial
;
250 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
251 static RASENUMENTRIES ms_pfnRasEnumEntries
;
252 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
253 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
254 static RASHANGUP ms_pfnRasHangUp
;
255 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
256 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
257 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
258 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
259 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
260 static RASENUMDEVICES ms_pfnRasEnumDevices
;
261 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
262 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
263 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
264 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
265 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
266 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
268 // this function is not supported by Win95
269 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
271 // if this flag is different from -1, it overrides IsOnline()
272 static int ms_userSpecifiedOnlineStatus
;
274 // this flag tells us if we're online
275 static int ms_isConnected
;
277 // this flag tells us whether a call to RasDial() is in progress
278 static wxDialUpManagerMSW
*ms_dialer
;
280 DECLARE_NO_COPY_CLASS(wxDialUpManagerMSW
)
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
288 WPARAM wParam
, LPARAM lParam
);
290 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
292 static void WINAPI
wxRasDialFunc(UINT unMsg
,
293 RASCONNSTATE rasconnstate
,
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
301 // init the static variables
302 // ----------------------------------------------------------------------------
304 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
306 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
308 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
309 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
310 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
311 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
312 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
313 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
314 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
315 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
316 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
317 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
318 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
319 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
320 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
321 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
322 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
323 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
324 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
325 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
326 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
328 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
329 int wxDialUpManagerMSW::ms_isConnected
= -1;
330 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
332 // ----------------------------------------------------------------------------
333 // ctor and dtor: the dynamic linking happens here
334 // ----------------------------------------------------------------------------
336 // the static creator function is implemented here
337 wxDialUpManager
*wxDialUpManager::Create()
339 return new wxDialUpManagerMSW
;
343 // warning about "'this' : used in base member initializer list" - so what?
344 #pragma warning(disable:4355)
347 wxDialUpManagerMSW::wxDialUpManagerMSW()
348 : m_timerStatusPolling(this),
349 m_dllRas(_T("RASAPI32"))
351 // initialize our data
352 m_autoCheckLevel
= 0;
354 m_data
= new wxRasThreadData
;
356 if ( !m_dllRas
.IsLoaded() )
358 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
360 else if ( !ms_pfnRasDial
)
362 // resolve the functions we need
364 // this will contain the name of the function we failed to resolve
366 const char *funcName
= NULL
;
368 // get the function from rasapi32.dll and abort if it's not found
369 #define RESOLVE_RAS_FUNCTION(type, name) \
370 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
372 if ( !ms_pfn##name ) \
378 // a variant of above macro which doesn't abort if the function is
379 // not found in the DLL
380 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
381 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
384 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
385 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
386 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
387 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
388 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
389 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
390 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
392 // suppress error messages about missing (non essential) functions
396 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
397 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
398 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
399 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
400 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
401 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
402 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
403 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
404 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
405 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
406 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
407 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
410 // keep your preprocessor name space clean
411 #undef RESOLVE_RAS_FUNCTION
412 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
417 static const wxChar
*msg
= wxTRANSLATE(
418 "The version of remote access service (RAS) installed on this machine is too\
419 old, please upgrade (the following required function is missing: %s)."
422 wxLogError(wxGetTranslation(msg
), funcName
);
428 // enable auto check by default
429 EnableAutoCheckOnlineStatus(0);
432 wxDialUpManagerMSW::~wxDialUpManagerMSW()
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
443 wxChar buffer
[512]; // this should be more than enough according to MS docs
444 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
447 case ERROR_INVALID_PARAMETER
:
448 // this was a standard Win32 error probably
449 return wxString(wxSysErrorMsg(error
));
454 _("Failed to retrieve text of RAS error message"));
457 msg
.Printf(_("unknown error (error code %08x)."), error
);
462 // we want the error message to start from a lower case letter
463 buffer
[0] = wxTolower(buffer
[0]);
465 return wxString(buffer
);
469 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
471 // enumerate connections
472 DWORD cbBuf
= sizeof(RASCONN
);
473 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
480 lpRasConn
->dwSize
= sizeof(RASCONN
);
482 DWORD nConnections
= 0;
483 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
485 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
487 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
489 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
491 LPRASCONN lpRasConnOld
= lpRasConn
;
492 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
501 else if ( dwRet
== 0 )
509 wxLogError(_("Cannot find active dialup connection: %s"),
510 GetErrorString(dwRet
).c_str());
517 switch ( nConnections
)
525 // more than 1 connection - we don't know what to do with this
526 // case, so give a warning but continue (taking the first
527 // connection) - the warning is really needed because this function
528 // is used, for example, to select the connection to hang up and so
529 // we may hang up the wrong connection here...
530 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
534 // exactly 1 connection, great
535 hrasconn
= lpRasConn
->hrasconn
;
543 void wxDialUpManagerMSW::CleanUpThreadData()
547 if ( !SetEvent(m_data
->hEventQuit
) )
549 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
551 else // sent quit request to the background thread
553 // the thread still needs m_data so we can't free it here, rather
554 // let the thread do it itself
558 CloseHandle(m_hThread
);
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 void wxDialUpManagerMSW::CheckRasStatus()
576 // use int, not bool to compare with -1
577 int isConnected
= FindActiveConnection() != 0;
578 if ( isConnected
!= ms_isConnected
)
580 if ( ms_isConnected
!= -1 )
582 // notify the program
583 NotifyApp(isConnected
!= 0);
585 // else: it's the first time we're called, just update the flag
587 ms_isConnected
= isConnected
;
591 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
593 wxDialUpEvent
event(connected
, fromOurselves
);
594 (void)wxTheApp
->ProcessEvent(event
);
597 // this function is called whenever the status of any RAS connection on this
598 // machine changes by RAS itself
599 void wxDialUpManagerMSW::OnConnectStatusChange()
601 // we know that status changed, but we don't know whether we're connected
602 // or not - so find it out
606 // this function is called by our callback which we give to RasDial() when
607 // calling it asynchronously
608 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
613 // this probably means that CancelDialing() was called and we get
614 // "disconnected" notification
618 // we're only interested in 2 events: connected and disconnected
621 wxLogError(_("Failed to establish dialup connection: %s"),
622 GetErrorString(dwError
).c_str());
624 // we should still call RasHangUp() if we got a non 0 connection
625 if ( ms_hRasConnection
)
627 ms_pfnRasHangUp(ms_hRasConnection
);
628 ms_hRasConnection
= 0;
633 NotifyApp(false /* !connected */, true /* we dialed ourselves */);
635 else if ( rasconnstate
== RASCS_Connected
)
637 ms_isConnected
= true;
640 NotifyApp(true /* connected */, true /* we dialed ourselves */);
644 // ----------------------------------------------------------------------------
645 // implementation of wxDialUpManager functions
646 // ----------------------------------------------------------------------------
648 bool wxDialUpManagerMSW::IsOk() const
650 return m_dllRas
.IsLoaded();
653 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
656 DWORD size
= sizeof(RASENTRYNAME
);
657 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
658 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
664 dwRet
= ms_pfnRasEnumEntries
667 NULL
, // default phone book (or all)
668 rasEntries
, // [out] buffer for the entries
669 &size
, // [in/out] size of the buffer
670 &nEntries
// [out] number of entries fetched
673 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
675 // reallocate the buffer
676 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
678 else if ( dwRet
!= 0 )
680 // some other error - abort
681 wxLogError(_("Failed to get ISP names: %s"),
682 GetErrorString(dwRet
).c_str());
689 while ( dwRet
!= 0 );
693 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
695 names
.Add(rasEntries
[n
].szEntryName
);
700 // return the number of entries
701 return names
.GetCount();
704 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
705 const wxString
& username
,
706 const wxString
& password
,
709 // check preconditions
710 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
712 if ( ms_hRasConnection
)
714 wxFAIL_MSG(wxT("there is already an active connection"));
719 // get the default ISP if none given
720 wxString
entryName(nameOfISP
);
724 size_t count
= GetISPNames(names
);
728 // no known ISPs, abort
729 wxLogError(_("Failed to connect: no ISP to dial."));
734 // only one ISP, choose it
735 entryName
= names
[0u];
739 // several ISPs, let the user choose
741 wxString
*strings
= new wxString
[count
];
742 for ( size_t i
= 0; i
< count
; i
++ )
744 strings
[i
] = names
[i
];
747 entryName
= wxGetSingleChoice
749 _("Choose ISP to dial"),
750 _("Please choose which ISP do you want to connect to"),
766 RASDIALPARAMS rasDialParams
;
767 rasDialParams
.dwSize
= sizeof(rasDialParams
);
768 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
770 // do we have the username and password?
771 if ( !username
|| !password
)
774 DWORD dwRet
= ms_pfnRasGetEntryDialParams
776 NULL
, // default phonebook
777 &rasDialParams
, // [in/out] the params of this entry
778 &gotPassword
// [out] did we get password?
783 wxLogError(_("Failed to connect: missing username/password."));
790 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
791 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
794 // default values for other fields
795 rasDialParams
.szPhoneNumber
[0] = '\0';
796 rasDialParams
.szCallbackNumber
[0] = '\0';
797 rasDialParams
.szCallbackNumber
[0] = '\0';
799 rasDialParams
.szDomain
[0] = '*';
800 rasDialParams
.szDomain
[1] = '\0';
802 // apparently, this is not really necessary - passing NULL instead of the
803 // phone book has the same effect
806 if ( wxGetOsVersion() == wxWINDOWS_NT
)
808 // first get the length
809 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
812 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
814 wxLogSysError(_("Cannot find the location of address book file"));
817 phoneBook
.UngetWriteBuf();
819 // this is the default phone book
820 phoneBook
<< "\\ras\\rasphone.pbk";
824 // TODO may be we should disable auto check while async dialing is in
829 DWORD dwRet
= ms_pfnRasDial
831 NULL
, // no extended features
832 NULL
, // default phone book file (NT only)
834 0, // use callback for notifications
835 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
836 : 0, // no notifications, sync operation
842 // can't pass a wxWCharBuffer through ( ... )
843 wxLogError(_("Failed to %s dialup connection: %s"),
844 wxString(async
? _("initiate") : _("establish")).c_str(),
845 GetErrorString(dwRet
).c_str());
847 // we should still call RasHangUp() if we got a non 0 connection
848 if ( ms_hRasConnection
)
850 ms_pfnRasHangUp(ms_hRasConnection
);
851 ms_hRasConnection
= 0;
859 // for async dialing, we're not yet connected
862 ms_isConnected
= true;
868 bool wxDialUpManagerMSW::IsDialing() const
870 return GetDialer() != NULL
;
873 bool wxDialUpManagerMSW::CancelDialing()
881 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
888 bool wxDialUpManagerMSW::HangUp()
890 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
892 // we may terminate either the connection we initiated or another one which
895 if ( ms_hRasConnection
)
897 hRasConn
= ms_hRasConnection
;
899 ms_hRasConnection
= 0;
903 hRasConn
= FindActiveConnection();
908 wxLogError(_("Cannot hang up - no active dialup connection."));
913 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
916 wxLogError(_("Failed to terminate the dialup connection: %s"),
917 GetErrorString(dwRet
).c_str());
920 ms_isConnected
= false;
925 bool wxDialUpManagerMSW::IsAlwaysOnline() const
927 // assume no permanent connection by default
928 bool isAlwaysOnline
= false;
930 // try to use WinInet functions
932 // NB: we could probably use wxDynamicLibrary here just as well,
933 // but we allow multiple instances of wxDialUpManagerMSW so
934 // we might as well use the ref counted version here too.
936 wxDynamicLibrary
hDll(_T("WININET"));
937 if ( hDll
.IsLoaded() )
939 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
940 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
942 #define RESOLVE_FUNCTION(type, name) \
943 pfn##name = (type)hDll.GetSymbol(_T(#name))
945 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
947 if ( pfnInternetGetConnectedState
)
950 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
952 // there is some connection to the net, see of which type
953 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
954 INTERNET_CONNECTION_PROXY
)) != 0;
956 //else: no Internet connection at all
960 return isAlwaysOnline
;
963 bool wxDialUpManagerMSW::IsOnline() const
965 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
967 if ( IsAlwaysOnline() )
973 if ( ms_userSpecifiedOnlineStatus
!= -1 )
975 // user specified flag overrides our logic
976 return ms_userSpecifiedOnlineStatus
!= 0;
980 // return true if there is at least one active connection
981 return FindActiveConnection() != 0;
985 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
987 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
989 ms_userSpecifiedOnlineStatus
= isOnline
;
992 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
994 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
996 if ( m_autoCheckLevel
++ )
1002 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1006 // we're running under NT 4.0, Windows 98 or later and can use
1007 // RasConnectionNotification() to be notified by a secondary thread
1009 // first, see if we don't have this thread already running
1010 if ( m_hThread
!= 0 )
1012 if ( ::ResumeThread(m_hThread
) != (DWORD
)-1 )
1015 // we're leaving a zombie thread... but what else can we do?
1016 wxLogLastError(wxT("ResumeThread(RasThread)"));
1022 // create all the stuff we need to be notified about RAS connection
1027 // first create an event to wait on
1028 m_data
->hEventRas
= ::CreateEvent
1030 NULL
, // security attribute (default)
1031 FALSE
, // manual reset (no, it is automatic)
1032 FALSE
, // initial state (not signaled)
1035 if ( !m_data
->hEventRas
)
1037 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1045 // create the event we use to quit the thread: using a manual event
1046 // here avoids problems with missing the event if wxDialUpManagerMSW
1047 // is created and destroyed immediately, before wxRasStatusWindowProc
1048 // starts waiting on the event
1049 m_data
->hEventQuit
= ::CreateEvent
1051 NULL
, // default security
1052 TRUE
, // manual event
1053 FALSE
, // initially non signalled
1056 if ( !m_data
->hEventQuit
)
1058 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1060 CleanUpThreadData();
1066 if ( ok
&& !ms_hwndRas
)
1068 // create a hidden window to receive notification about connections
1070 extern const wxChar
*wxCanvasClassName
;
1071 ms_hwndRas
= ::CreateWindow(wxCanvasClassName
, NULL
,
1074 (HMENU
)NULL
, wxGetInstance(), 0);
1077 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1079 CleanUpThreadData();
1085 wxSetWindowProc(ms_hwndRas
, wxRasStatusWindowProc
);
1088 m_data
->hWnd
= ms_hwndRas
;
1092 // start the secondary thread
1093 m_data
->dialUpManager
= this;
1096 m_hThread
= CreateThread
1100 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1108 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1110 CleanUpThreadData();
1116 // start receiving RAS notifications
1117 DWORD dwRet
= ms_pfnRasConnectionNotification
1119 (HRASCONN
)INVALID_HANDLE_VALUE
,
1121 3 /* RASCN_Connection | RASCN_Disconnection */
1126 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1127 GetErrorString(dwRet
).c_str());
1129 CleanUpThreadData();
1137 // we're running under Windows 95 and have to poll ourselves
1138 // (or, alternatively, the code above for NT/98 failed)
1139 m_timerStatusPolling
.Stop();
1140 if ( nSeconds
== 0 )
1145 m_timerStatusPolling
.Start(nSeconds
* 1000);
1150 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1152 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1154 if ( --m_autoCheckLevel
)
1162 // we have running secondary thread, it's just enough to suspend it
1163 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1165 wxLogLastError(wxT("SuspendThread(RasThread)"));
1170 // even simpler - just stop the timer
1171 m_timerStatusPolling
.Stop();
1175 // ----------------------------------------------------------------------------
1176 // stubs which don't do anything in MSW version
1177 // ----------------------------------------------------------------------------
1179 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1182 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1184 // nothing to do - we don't use this
1187 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1188 const wxString
& WXUNUSED(hangup
))
1190 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1192 // nothing to do - we don't use this
1195 // ----------------------------------------------------------------------------
1197 // ----------------------------------------------------------------------------
1199 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1202 handles
[0] = data
->hEventRas
;
1203 handles
[1] = data
->hEventQuit
;
1208 DWORD dwRet
= ::WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1213 // RAS connection status changed
1214 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1218 case WAIT_OBJECT_0
+ 1:
1223 wxFAIL_MSG( _T("unexpected return of WaitForMultipleObjects()") );
1228 // using wxLogLastError() from here is dangerous: we risk to
1229 // deadlock the main thread if wxLog sends output to GUI
1230 DWORD err
= GetLastError();
1231 wxMessageOutputDebug().Printf
1233 wxT("WaitForMultipleObjects(RasMonitor) failed: 0x%08lx (%s)"),
1237 #endif // __WXDEBUG__
1239 // no sense in continuing, who knows if the handles we're
1240 // waiting for even exist yet...
1245 // we don't need it any more now and if this thread ran, it is our
1246 // responsability to free the data
1252 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1253 WPARAM wParam
, LPARAM lParam
)
1257 case wxWM_RAS_STATUS_CHANGED
:
1259 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1260 data
->dialUpManager
->OnConnectStatusChange();
1264 case wxWM_RAS_DIALING_PROGRESS
:
1266 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1268 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1273 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1279 static void WINAPI
wxRasDialFunc(UINT
WXUNUSED(unMsg
),
1280 RASCONNSTATE rasconnstate
,
1283 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1285 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1287 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1288 rasconnstate
, dwError
);
1291 #endif // __BORLANDC__
1293 #endif // wxUSE_DIALUP_MANAGER