1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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
29 #include "wx/dialup.h"
37 #include "wx/module.h"
40 #include "wx/generic/choicdgg.h"
42 #include "wx/msw/private.h"
43 #include "wx/msw/private/hiddenwin.h"
44 #include "wx/dynlib.h"
46 wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED
, wxDialUpEvent
);
47 wxDEFINE_EVENT( wxEVT_DIALUP_DISCONNECTED
, wxDialUpEvent
);
49 // Doesn't yet compile under VC++ 4, BC++, Watcom C++,
51 #if (!defined(__BORLANDC__) || (__BORLANDC__>=0x550)) && \
52 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
53 !defined(__GNUWIN32_OLD__) && \
54 !defined(__WINE__) && \
55 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
63 #ifndef INTERNET_CONNECTION_LAN
64 #define INTERNET_CONNECTION_LAN 2
66 #ifndef INTERNET_CONNECTION_PROXY
67 #define INTERNET_CONNECTION_PROXY 4
71 wxMSWDIALUP_WNDCLASSNAME
= wxT("_wxDialUpManager_Internal_Class");
72 static const wxChar
*gs_classForDialUpWindow
= NULL
;
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // this message is sent by the secondary thread when RAS status changes
79 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
80 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // the signatures of RAS functions: all this is quite heavy, but we must do it
87 // to allow running wxWin programs on machine which don't have RAS installed
88 // (this does exist) - if we link with rasapi32.lib, the program will fail on
89 // startup because of the missing DLL...
92 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
93 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
94 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
95 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
96 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
97 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
98 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
99 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
100 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
101 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
102 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
103 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
104 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
105 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
106 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
107 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
108 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
109 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
110 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
112 static const wxChar gs_funcSuffix
= wxT('A');
114 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
115 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
116 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
117 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
118 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
119 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
120 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
121 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
122 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
123 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
124 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
125 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
126 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
127 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
128 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
129 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
130 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
131 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
132 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
134 static const wxChar gs_funcSuffix
= wxT('W');
135 #endif // ASCII/Unicode
137 // structure passed to the secondary thread
138 struct WXDLLEXPORT wxRasThreadData
145 dialUpManager
= NULL
;
154 CloseHandle(hEventQuit
);
157 CloseHandle(hEventRas
);
160 HWND hWnd
; // window to send notifications to
161 HANDLE hEventRas
, // automatic event which RAS signals when status changes
162 hEventQuit
; // manual event which we signal when we terminate
164 class WXDLLIMPEXP_FWD_CORE wxDialUpManagerMSW
*dialUpManager
; // the owner
167 // ----------------------------------------------------------------------------
168 // wxDialUpManager class for MSW
169 // ----------------------------------------------------------------------------
171 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
175 wxDialUpManagerMSW();
176 virtual ~wxDialUpManagerMSW();
178 // implement base class pure virtuals
179 virtual bool IsOk() const;
180 virtual size_t GetISPNames(wxArrayString
& names
) const;
181 virtual bool Dial(const wxString
& nameOfISP
,
182 const wxString
& username
,
183 const wxString
& password
,
185 virtual bool IsDialing() const;
186 virtual bool CancelDialing();
187 virtual bool HangUp();
188 virtual bool IsAlwaysOnline() const;
189 virtual bool IsOnline() const;
190 virtual void SetOnlineStatus(bool isOnline
= true);
191 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
192 virtual void DisableAutoCheckOnlineStatus();
193 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
194 virtual void SetConnectCommand(const wxString
& commandDial
,
195 const wxString
& commandHangup
);
198 void CheckRasStatus();
200 // for wxRasStatusWindowProc
201 void OnConnectStatusChange();
202 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
205 static HWND
GetRasWindow() { return ms_hwndRas
; }
206 static void ResetRasWindow() { ms_hwndRas
= NULL
; }
207 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
210 // return the error string for the given RAS error code
211 static wxString
GetErrorString(DWORD error
);
213 // find the (first) handle of the active connection
214 static HRASCONN
FindActiveConnection();
216 // notify the application about status change
217 void NotifyApp(bool connected
, bool fromOurselves
= false) const;
219 // destroy the thread data and the thread itself
220 void CleanUpThreadData();
222 // number of times EnableAutoCheckOnlineStatus() had been called minus the
223 // number of times DisableAutoCheckOnlineStatus() had been called
224 int m_autoCheckLevel
;
226 // timer used for polling RAS status
227 class WXDLLEXPORT RasTimer
: public wxTimer
230 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
231 { m_dialUpManager
= dialUpManager
; }
233 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
236 wxDialUpManagerMSW
*m_dialUpManager
;
238 wxDECLARE_NO_COPY_CLASS(RasTimer
);
239 } m_timerStatusPolling
;
241 // thread handle for the thread sitting on connection change event
244 // data used by this thread and our hidden window to send messages between
246 wxRasThreadData
*m_data
;
248 // the handle of rasapi32.dll when it's loaded
249 wxDynamicLibrary m_dllRas
;
251 // the hidden window we use for passing messages between threads
252 static HWND ms_hwndRas
;
254 // the handle of the connection we initiated or 0 if none
255 static HRASCONN ms_hRasConnection
;
257 // the pointers to RAS functions
258 static RASDIAL ms_pfnRasDial
;
259 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
260 static RASENUMENTRIES ms_pfnRasEnumEntries
;
261 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
262 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
263 static RASHANGUP ms_pfnRasHangUp
;
264 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
265 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
266 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
267 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
268 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
269 static RASENUMDEVICES ms_pfnRasEnumDevices
;
270 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
271 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
272 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
273 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
274 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
275 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
277 // this function is not supported by Win95
278 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
280 // if this flag is different from -1, it overrides IsOnline()
281 static int ms_userSpecifiedOnlineStatus
;
283 // this flag tells us if we're online
284 static int ms_isConnected
;
286 // this flag tells us whether a call to RasDial() is in progress
287 static wxDialUpManagerMSW
*ms_dialer
;
289 wxDECLARE_NO_COPY_CLASS(wxDialUpManagerMSW
);
292 // module to destroy helper window created by wxDialUpManagerMSW
293 class wxDialUpManagerModule
: public wxModule
296 bool OnInit() { return true; }
299 HWND hwnd
= wxDialUpManagerMSW::GetRasWindow();
302 ::DestroyWindow(hwnd
);
303 wxDialUpManagerMSW::ResetRasWindow();
306 if ( gs_classForDialUpWindow
)
308 ::UnregisterClass(wxMSWDIALUP_WNDCLASSNAME
, wxGetInstance());
309 gs_classForDialUpWindow
= NULL
;
314 DECLARE_DYNAMIC_CLASS(wxDialUpManagerModule
)
317 IMPLEMENT_DYNAMIC_CLASS(wxDialUpManagerModule
, wxModule
)
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
324 WPARAM wParam
, LPARAM lParam
);
326 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
328 static void WINAPI
wxRasDialFunc(UINT unMsg
,
329 RASCONNSTATE rasconnstate
,
332 // ============================================================================
334 // ============================================================================
336 // ----------------------------------------------------------------------------
337 // init the static variables
338 // ----------------------------------------------------------------------------
340 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
342 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
344 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
345 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
346 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
347 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
348 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
349 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
350 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
351 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
352 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
353 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
354 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
355 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
356 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
357 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
358 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
359 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
360 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
361 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
362 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
364 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
365 int wxDialUpManagerMSW::ms_isConnected
= -1;
366 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
368 // ----------------------------------------------------------------------------
369 // ctor and dtor: the dynamic linking happens here
370 // ----------------------------------------------------------------------------
372 // the static creator function is implemented here
373 wxDialUpManager
*wxDialUpManager::Create()
375 return new wxDialUpManagerMSW
;
379 // warning about "'this' : used in base member initializer list" - so what?
380 #pragma warning(disable:4355)
383 wxDialUpManagerMSW::wxDialUpManagerMSW()
384 : m_timerStatusPolling(this),
385 m_dllRas(wxT("RASAPI32"))
387 // initialize our data
388 m_autoCheckLevel
= 0;
390 m_data
= new wxRasThreadData
;
392 if ( !m_dllRas
.IsLoaded() )
394 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
396 else if ( !ms_pfnRasDial
)
398 // resolve the functions we need
400 // this will contain the name of the function we failed to resolve
402 const char *funcName
= NULL
;
404 // get the function from rasapi32.dll and abort if it's not found
405 #define RESOLVE_RAS_FUNCTION(type, name) \
406 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(wxT(#name)) \
408 if ( !ms_pfn##name ) \
414 // a variant of above macro which doesn't abort if the function is
415 // not found in the DLL
416 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
417 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(wxT(#name)) \
420 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
421 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
422 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
423 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
424 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
425 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
426 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
428 // suppress error messages about missing (non essential) functions
432 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
433 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
434 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
435 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
436 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
437 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
438 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
439 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
440 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
441 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
442 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
443 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
446 // keep your preprocessor name space clean
447 #undef RESOLVE_RAS_FUNCTION
448 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
453 wxLogError(_("The version of remote access service (RAS) installed "
454 "on this machine is too old, please upgrade (the "
455 "following required function is missing: %s)."),
462 // enable auto check by default
463 EnableAutoCheckOnlineStatus(0);
466 wxDialUpManagerMSW::~wxDialUpManagerMSW()
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
477 wxChar buffer
[512]; // this should be more than enough according to MS docs
478 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
481 case ERROR_INVALID_PARAMETER
:
482 // this was a standard Win32 error probably
483 return wxString(wxSysErrorMsg(error
));
488 _("Failed to retrieve text of RAS error message"));
491 msg
.Printf(_("unknown error (error code %08x)."), error
);
496 // we want the error message to start from a lower case letter
497 buffer
[0] = (wxChar
)wxTolower(buffer
[0]);
499 return wxString(buffer
);
503 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
505 // enumerate connections
506 DWORD cbBuf
= sizeof(RASCONN
);
507 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
514 lpRasConn
->dwSize
= sizeof(RASCONN
);
516 DWORD nConnections
= 0;
517 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
519 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
521 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
523 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
525 LPRASCONN lpRasConnOld
= lpRasConn
;
526 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
535 else if ( dwRet
== 0 )
543 wxLogError(_("Cannot find active dialup connection: %s"),
544 GetErrorString(dwRet
).c_str());
551 switch ( nConnections
)
559 // more than 1 connection - we don't know what to do with this
560 // case, so give a warning but continue (taking the first
561 // connection) - the warning is really needed because this function
562 // is used, for example, to select the connection to hang up and so
563 // we may hang up the wrong connection here...
564 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
568 // exactly 1 connection, great
569 hrasconn
= lpRasConn
->hrasconn
;
577 void wxDialUpManagerMSW::CleanUpThreadData()
581 if ( !SetEvent(m_data
->hEventQuit
) )
583 wxLogLastError(wxT("SetEvent(RasThreadQuit)"));
585 else // sent quit request to the background thread
587 // the thread still needs m_data so we can't free it here, rather
588 // let the thread do it itself
592 CloseHandle(m_hThread
);
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 void wxDialUpManagerMSW::CheckRasStatus()
606 // use int, not bool to compare with -1
607 int isConnected
= FindActiveConnection() != 0;
608 if ( isConnected
!= ms_isConnected
)
610 if ( ms_isConnected
!= -1 )
612 // notify the program
613 NotifyApp(isConnected
!= 0);
615 // else: it's the first time we're called, just update the flag
617 ms_isConnected
= isConnected
;
621 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
623 wxDialUpEvent
event(connected
, fromOurselves
);
624 (void)wxTheApp
->ProcessEvent(event
);
627 // this function is called whenever the status of any RAS connection on this
628 // machine changes by RAS itself
629 void wxDialUpManagerMSW::OnConnectStatusChange()
631 // we know that status changed, but we don't know whether we're connected
632 // or not - so find it out
636 // this function is called by our callback which we give to RasDial() when
637 // calling it asynchronously
638 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
643 // this probably means that CancelDialing() was called and we get
644 // "disconnected" notification
648 // we're only interested in 2 events: connected and disconnected
651 wxLogError(_("Failed to establish dialup connection: %s"),
652 GetErrorString(dwError
).c_str());
654 // we should still call RasHangUp() if we got a non 0 connection
655 if ( ms_hRasConnection
)
657 ms_pfnRasHangUp(ms_hRasConnection
);
658 ms_hRasConnection
= 0;
663 NotifyApp(false /* !connected */, true /* we dialed ourselves */);
665 else if ( rasconnstate
== RASCS_Connected
)
667 ms_isConnected
= true;
670 NotifyApp(true /* connected */, true /* we dialed ourselves */);
674 // ----------------------------------------------------------------------------
675 // implementation of wxDialUpManager functions
676 // ----------------------------------------------------------------------------
678 bool wxDialUpManagerMSW::IsOk() const
680 return m_dllRas
.IsLoaded();
683 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
686 DWORD size
= sizeof(RASENTRYNAME
);
687 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
688 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
694 dwRet
= ms_pfnRasEnumEntries
697 NULL
, // default phone book (or all)
698 rasEntries
, // [out] buffer for the entries
699 &size
, // [in/out] size of the buffer
700 &nEntries
// [out] number of entries fetched
703 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
705 // reallocate the buffer
706 void *n
= realloc(rasEntries
, size
);
712 rasEntries
= (RASENTRYNAME
*)n
;
714 else if ( dwRet
!= 0 )
716 // some other error - abort
717 wxLogError(_("Failed to get ISP names: %s"),
718 GetErrorString(dwRet
).c_str());
725 while ( dwRet
!= 0 );
729 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
731 names
.Add(rasEntries
[n
].szEntryName
);
736 // return the number of entries
737 return names
.GetCount();
740 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
741 const wxString
& username
,
742 const wxString
& password
,
745 // check preconditions
746 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
748 if ( ms_hRasConnection
)
750 wxFAIL_MSG(wxT("there is already an active connection"));
755 // get the default ISP if none given
756 wxString
entryName(nameOfISP
);
760 size_t count
= GetISPNames(names
);
764 // no known ISPs, abort
765 wxLogError(_("Failed to connect: no ISP to dial."));
770 // only one ISP, choose it
771 entryName
= names
[0u];
775 // several ISPs, let the user choose
777 wxString
*strings
= new wxString
[count
];
778 for ( size_t i
= 0; i
< count
; i
++ )
780 strings
[i
] = names
[i
];
783 entryName
= wxGetSingleChoice
785 _("Choose ISP to dial"),
786 _("Please choose which ISP do you want to connect to"),
802 RASDIALPARAMS rasDialParams
;
803 rasDialParams
.dwSize
= sizeof(rasDialParams
);
804 wxStrlcpy(rasDialParams
.szEntryName
, entryName
.c_str(), RAS_MaxEntryName
);
806 // do we have the username and password?
807 if ( !username
|| !password
)
810 DWORD dwRet
= ms_pfnRasGetEntryDialParams
812 NULL
, // default phonebook
813 &rasDialParams
, // [in/out] the params of this entry
814 &gotPassword
// [out] did we get password?
819 wxLogError(_("Failed to connect: missing username/password."));
826 wxStrlcpy(rasDialParams
.szUserName
, username
.c_str(), UNLEN
);
827 wxStrlcpy(rasDialParams
.szPassword
, password
.c_str(), PWLEN
);
830 // default values for other fields
831 rasDialParams
.szPhoneNumber
[0] = '\0';
832 rasDialParams
.szCallbackNumber
[0] = '\0';
833 rasDialParams
.szCallbackNumber
[0] = '\0';
835 rasDialParams
.szDomain
[0] = '*';
836 rasDialParams
.szDomain
[1] = '\0';
838 // apparently, this is not really necessary - passing NULL instead of the
839 // phone book has the same effect
842 if ( wxGetOsVersion() == wxWINDOWS_NT
)
844 // first get the length
845 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
848 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
850 wxLogSysError(_("Cannot find the location of address book file"));
853 phoneBook
.UngetWriteBuf();
855 // this is the default phone book
856 phoneBook
<< "\\ras\\rasphone.pbk";
860 // TODO may be we should disable auto check while async dialing is in
865 DWORD dwRet
= ms_pfnRasDial
867 NULL
, // no extended features
868 NULL
, // default phone book file (NT only)
870 0, // use callback for notifications
871 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
872 : 0, // no notifications, sync operation
878 // can't pass a wxWCharBuffer through ( ... )
881 wxLogError(_("Failed to initiate dialup connection: %s"),
882 GetErrorString(dwRet
).c_str());
886 wxLogError(_("Failed to establish dialup connection: %s"),
887 GetErrorString(dwRet
).c_str());
890 // we should still call RasHangUp() if we got a non 0 connection
891 if ( ms_hRasConnection
)
893 ms_pfnRasHangUp(ms_hRasConnection
);
894 ms_hRasConnection
= 0;
902 // for async dialing, we're not yet connected
905 ms_isConnected
= true;
911 bool wxDialUpManagerMSW::IsDialing() const
913 return GetDialer() != NULL
;
916 bool wxDialUpManagerMSW::CancelDialing()
924 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
931 bool wxDialUpManagerMSW::HangUp()
933 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
935 // we may terminate either the connection we initiated or another one which
938 if ( ms_hRasConnection
)
940 hRasConn
= ms_hRasConnection
;
942 ms_hRasConnection
= 0;
946 hRasConn
= FindActiveConnection();
951 wxLogError(_("Cannot hang up - no active dialup connection."));
956 // note that it's not an error if the connection had been already
958 const DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
959 if ( dwRet
!= 0 && dwRet
!= ERROR_NO_CONNECTION
)
961 wxLogError(_("Failed to terminate the dialup connection: %s"),
962 GetErrorString(dwRet
).c_str());
965 ms_isConnected
= false;
970 bool wxDialUpManagerMSW::IsAlwaysOnline() const
972 // assume no permanent connection by default
973 bool isAlwaysOnline
= false;
975 // try to use WinInet functions
977 // NB: we could probably use wxDynamicLibrary here just as well,
978 // but we allow multiple instances of wxDialUpManagerMSW so
979 // we might as well use the ref counted version here too.
981 wxDynamicLibrary
hDll(wxT("WININET"));
982 if ( hDll
.IsLoaded() )
984 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
985 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
987 #define RESOLVE_FUNCTION(type, name) \
988 pfn##name = (type)hDll.GetSymbol(wxT(#name))
990 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
992 if ( pfnInternetGetConnectedState
)
995 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
997 // there is some connection to the net, see of which type
998 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
999 INTERNET_CONNECTION_PROXY
)) != 0;
1001 //else: no Internet connection at all
1005 return isAlwaysOnline
;
1008 bool wxDialUpManagerMSW::IsOnline() const
1010 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
1012 if ( IsAlwaysOnline() )
1018 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1020 // user specified flag overrides our logic
1021 return ms_userSpecifiedOnlineStatus
!= 0;
1025 // return true if there is at least one active connection
1026 return FindActiveConnection() != 0;
1030 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1032 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1034 ms_userSpecifiedOnlineStatus
= isOnline
;
1037 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1039 wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") );
1041 if ( m_autoCheckLevel
++ )
1047 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1051 // we're running under NT 4.0, Windows 98 or later and can use
1052 // RasConnectionNotification() to be notified by a secondary thread
1054 // first, see if we don't have this thread already running
1055 if ( m_hThread
!= 0 )
1057 if ( ::ResumeThread(m_hThread
) != (DWORD
)-1 )
1060 // we're leaving a zombie thread... but what else can we do?
1061 wxLogLastError(wxT("ResumeThread(RasThread)"));
1067 // create all the stuff we need to be notified about RAS connection
1072 // first create an event to wait on
1073 m_data
->hEventRas
= ::CreateEvent
1075 NULL
, // security attribute (default)
1076 FALSE
, // manual reset (no, it is automatic)
1077 FALSE
, // initial state (not signaled)
1080 if ( !m_data
->hEventRas
)
1082 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1090 // create the event we use to quit the thread: using a manual event
1091 // here avoids problems with missing the event if wxDialUpManagerMSW
1092 // is created and destroyed immediately, before wxRasStatusWindowProc
1093 // starts waiting on the event
1094 m_data
->hEventQuit
= ::CreateEvent
1096 NULL
, // default security
1097 TRUE
, // manual event
1098 FALSE
, // initially non signalled
1101 if ( !m_data
->hEventQuit
)
1103 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1105 CleanUpThreadData();
1111 if ( ok
&& !ms_hwndRas
)
1113 // create a hidden window to receive notification about connections
1115 ms_hwndRas
= wxCreateHiddenWindow
1117 &gs_classForDialUpWindow
,
1118 wxMSWDIALUP_WNDCLASSNAME
,
1119 wxRasStatusWindowProc
1123 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1125 CleanUpThreadData();
1131 m_data
->hWnd
= ms_hwndRas
;
1135 // start the secondary thread
1136 m_data
->dialUpManager
= this;
1139 m_hThread
= CreateThread
1143 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1151 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1153 CleanUpThreadData();
1159 // start receiving RAS notifications
1160 DWORD dwRet
= ms_pfnRasConnectionNotification
1162 (HRASCONN
)INVALID_HANDLE_VALUE
,
1164 3 /* RASCN_Connection | RASCN_Disconnection */
1169 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1170 GetErrorString(dwRet
).c_str());
1172 CleanUpThreadData();
1180 // we're running under Windows 95 and have to poll ourselves
1181 // (or, alternatively, the code above for NT/98 failed)
1182 m_timerStatusPolling
.Stop();
1183 if ( nSeconds
== 0 )
1188 m_timerStatusPolling
.Start(nSeconds
* 1000);
1193 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1195 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1197 if ( --m_autoCheckLevel
!= 0 )
1205 // we have running secondary thread, it's just enough to suspend it
1206 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1208 wxLogLastError(wxT("SuspendThread(RasThread)"));
1213 // even simpler - just stop the timer
1214 m_timerStatusPolling
.Stop();
1218 // ----------------------------------------------------------------------------
1219 // stubs which don't do anything in MSW version
1220 // ----------------------------------------------------------------------------
1222 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1225 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1227 // nothing to do - we don't use this
1230 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1231 const wxString
& WXUNUSED(hangup
))
1233 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1235 // nothing to do - we don't use this
1238 // ----------------------------------------------------------------------------
1240 // ----------------------------------------------------------------------------
1242 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1245 handles
[0] = data
->hEventRas
;
1246 handles
[1] = data
->hEventQuit
;
1251 DWORD dwRet
= ::WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1256 // RAS connection status changed
1257 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1261 case WAIT_OBJECT_0
+ 1:
1266 wxFAIL_MSG( wxT("unexpected return of WaitForMultipleObjects()") );
1270 // using wxLogLastError() from here is dangerous: we risk to
1271 // deadlock the main thread if wxLog sends output to GUI
1272 DWORD err
= GetLastError();
1273 wxMessageOutputDebug dbg
;
1276 wxT("WaitForMultipleObjects(RasMonitor) failed: 0x%08lx (%s)"),
1281 // no sense in continuing, who knows if the handles we're
1282 // waiting for even exist yet...
1287 // we don't need it any more now and if this thread ran, it is our
1288 // responsability to free the data
1294 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1295 WPARAM wParam
, LPARAM lParam
)
1299 case wxWM_RAS_STATUS_CHANGED
:
1301 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1302 data
->dialUpManager
->OnConnectStatusChange();
1306 case wxWM_RAS_DIALING_PROGRESS
:
1308 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1310 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1315 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1321 static void WINAPI
wxRasDialFunc(UINT
WXUNUSED(unMsg
),
1322 RASCONNSTATE rasconnstate
,
1325 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1327 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1329 SendMessage(wxDialUpManagerMSW::GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1330 rasconnstate
, dwError
);
1333 #endif // __BORLANDC__
1335 #endif // wxUSE_DIALUP_MANAGER