1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dialup.cpp
3 // Purpose: MSW implementation of network/dialup classes and functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 // these functions require Win32
28 #if defined(__WIN16__) && wxUSE_DIALUP_MANAGER
29 #undef wxUSE_DIALUP_MANAGER
30 #define wxUSE_DIALUP_MANAGER 0
31 #endif // wxUSE_DIALUP_MANAGER && Win16
33 #if wxUSE_DIALUP_MANAGER
41 #include "wx/dynlib.h"
43 #include "wx/dialup.h"
45 // Doesn't yet compile under BC++ nor with mingw: no wininet.h
46 #if !defined(__BORLANDC__) && !defined(__GNUWIN32_OLD__)
53 #include "wx/msw/private.h"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // this message is sent by the secondary thread when RAS status changes
60 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
61 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // the signatures of RAS functions: all this is quite heavy, but we must do it
68 // to allow running wxWin programs on machine which don't have RAS installed
69 // (this does exist) - if we link with rasapi32.lib, the program will fail on
70 // startup because of the missing DLL...
73 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
74 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
75 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
76 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
77 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
78 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
79 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
80 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
81 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
82 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
83 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
84 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
85 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
86 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
87 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
88 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
89 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
90 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
91 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
93 static const char gs_funcSuffix
= 'A';
95 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
96 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
97 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
98 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
99 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
100 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
101 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
102 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
103 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
104 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
105 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
106 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
107 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
108 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
109 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
110 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
111 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
112 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
113 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
115 static const char gs_funcSuffix
= 'W';
116 #endif // ASCII/Unicode
118 // structure passed to the secondary thread
119 struct wxRasThreadData
124 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
125 dialUpManager
= NULL
;
128 HWND hWnd
; // window to send notifications to
129 HANDLE hEventRas
, // event which RAS signals when status changes
130 hEventQuit
; // event which we signal when we terminate
132 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
135 // ----------------------------------------------------------------------------
136 // wxDialUpManager class for MSW
137 // ----------------------------------------------------------------------------
139 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
143 wxDialUpManagerMSW();
144 virtual ~wxDialUpManagerMSW();
146 // implement base class pure virtuals
147 virtual bool IsOk() const;
148 virtual size_t GetISPNames(wxArrayString
& names
) const;
149 virtual bool Dial(const wxString
& nameOfISP
,
150 const wxString
& username
,
151 const wxString
& password
,
153 virtual bool IsDialing() const;
154 virtual bool CancelDialing();
155 virtual bool HangUp();
156 virtual bool IsAlwaysOnline() const;
157 virtual bool IsOnline() const;
158 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
159 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
160 virtual void DisableAutoCheckOnlineStatus();
161 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
162 virtual void SetConnectCommand(const wxString
& commandDial
,
163 const wxString
& commandHangup
);
166 void CheckRasStatus();
168 // for wxRasStatusWindowProc
169 void OnConnectStatusChange();
170 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
173 static HWND
GetRasWindow() { return ms_hwndRas
; }
174 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
177 // return the error string for the given RAS error code
178 static wxString
GetErrorString(DWORD error
);
180 // find the (first) handle of the active connection
181 static HRASCONN
FindActiveConnection();
183 // notify the application about status change
184 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
186 // destroy the thread data and the thread itself
187 void CleanUpThreadData();
189 // timer used for polling RAS status
190 class RasTimer
: public wxTimer
193 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
194 { m_dialUpManager
= dialUpManager
; }
196 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
199 wxDialUpManagerMSW
*m_dialUpManager
;
200 } m_timerStatusPolling
;
202 // thread handle for the thread sitting on connection change event
205 // data used by this thread and our hidden window to send messages between
207 wxRasThreadData m_data
;
209 // the hidden window we use for passing messages between threads
210 static HWND ms_hwndRas
;
212 // the handle of the connection we initiated or 0 if none
213 static HRASCONN ms_hRasConnection
;
215 // the use count of rasapi32.dll
216 static int ms_nDllCount
;
218 // the handle of rasapi32.dll when it's loaded
219 static wxDllType ms_dllRas
;
221 // the pointers to RAS functions
222 static RASDIAL ms_pfnRasDial
;
223 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
224 static RASENUMENTRIES ms_pfnRasEnumEntries
;
225 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
226 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
227 static RASHANGUP ms_pfnRasHangUp
;
228 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
229 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
230 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
231 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
232 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
233 static RASENUMDEVICES ms_pfnRasEnumDevices
;
234 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
235 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
236 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
237 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
238 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
239 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
241 // this function is not supported by Win95
242 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
244 // if this flag is different from -1, it overrides IsOnline()
245 static int ms_userSpecifiedOnlineStatus
;
247 // this flag tells us if we're online
248 static int ms_isConnected
;
250 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
252 static int ms_isAlwaysOnline
;
254 // this flag tells us whether a call to RasDial() is in progress
255 static wxDialUpManagerMSW
*ms_dialer
;
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
263 WPARAM wParam
, LPARAM lParam
);
265 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
267 static void WINAPI
wxRasDialFunc(UINT unMsg
,
268 RASCONNSTATE rasconnstate
,
271 // ============================================================================
273 // ============================================================================
275 // ----------------------------------------------------------------------------
276 // init the static variables
277 // ----------------------------------------------------------------------------
279 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
281 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
283 int wxDialUpManagerMSW::ms_nDllCount
= 0;
284 wxDllType
wxDialUpManagerMSW::ms_dllRas
= 0;
286 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
287 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
288 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
289 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
290 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
291 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
292 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
293 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
294 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
295 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
296 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
297 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
298 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
299 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
300 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
301 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
302 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
303 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
304 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
306 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
307 int wxDialUpManagerMSW::ms_isConnected
= -1;
308 int wxDialUpManagerMSW::ms_isAlwaysOnline
= -1;
309 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
311 // ----------------------------------------------------------------------------
312 // ctor and dtor: the dynamic linking happens here
313 // ----------------------------------------------------------------------------
315 // the static creator function is implemented here
316 wxDialUpManager
*wxDialUpManager::Create()
318 return new wxDialUpManagerMSW
;
322 // warning about "'this' : used in base member initializer list" - so what?
323 #pragma warning(disable:4355)
326 wxDialUpManagerMSW::wxDialUpManagerMSW()
327 : m_timerStatusPolling(this)
329 // initialize our data
332 if ( !ms_nDllCount
++ )
334 // load the RAS library
335 ms_dllRas
= wxDllLoader::LoadLibrary("RASAPI32");
338 wxLogError(_("Dial up functions are unavailable because the "
339 "remote access service (RAS) is not installed "
340 "on this machine. Please install it."));
344 // resolve the functions we need
346 // this will contain the name of the function we failed to resolve
348 const char *funcName
= NULL
;
350 // get the function from rasapi32.dll and abort if it's not found
351 #define RESOLVE_RAS_FUNCTION(type, name) \
352 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
353 wxString(_T(#name)) + gs_funcSuffix); \
354 if ( !ms_pfn##name ) \
360 // a variant of above macro which doesn't abort if the function is
361 // not found in the DLL
362 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
363 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
364 wxString(_T(#name)) + gs_funcSuffix);
366 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
367 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
368 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
369 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
370 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
371 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
372 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
374 // suppress wxDllLoader messages about missing (non essential)
379 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
380 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
381 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
382 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
383 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
384 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
385 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
386 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
387 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
388 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
389 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
390 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
393 // keep your preprocessor name space clean
394 #undef RESOLVE_RAS_FUNCTION
395 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
400 wxLogError(_("The version of remote access service (RAS) "
401 "installed on this machine is too old, please "
402 "upgrade (the following required function is "
403 "missing: %s)."), funcName
);
405 wxDllLoader::UnloadLibrary(ms_dllRas
);
414 // enable auto check by default
415 EnableAutoCheckOnlineStatus(0);
418 wxDialUpManagerMSW::~wxDialUpManagerMSW()
422 if ( !--ms_nDllCount
)
424 // unload the RAS library
425 wxDllLoader::UnloadLibrary(ms_dllRas
);
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
436 wxChar buffer
[512]; // this should be more than enough according to MS docs
437 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
440 case ERROR_INVALID_PARAMETER
:
441 // this was a standard Win32 error probably
442 return wxString(wxSysErrorMsg(error
));
446 wxLogSysError(dwRet
, _("Failed to retrieve text of RAS "
450 msg
.Printf(_("unknown error (error code %08x)."), error
);
455 // we want the error message to start from a lower case letter
456 buffer
[0] = wxTolower(buffer
[0]);
458 return wxString(buffer
);
462 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
464 // enumerate connections
465 DWORD cbBuf
= sizeof(RASCONN
);
466 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
473 lpRasConn
->dwSize
= sizeof(RASCONN
);
475 DWORD nConnections
= 0;
476 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
478 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
480 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
482 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
484 LPRASCONN lpRasConnOld
= lpRasConn
;
485 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
494 else if ( dwRet
== 0 )
502 wxLogError(_("Cannot find active dialup connection: %s"),
503 GetErrorString(dwRet
));
510 switch ( nConnections
)
518 // more than 1 connection - we don't know what to do with this
519 // case, so give a warning but continue (taking the first
520 // connection) - the warning is really needed because this function
521 // is used, for example, to select the connection to hang up and so
522 // we may hang up the wrong connection here...
523 wxLogWarning(_("Several active dialup connections found, "
524 "choosing one randomly."));
528 // exactly 1 connection, great
529 hrasconn
= lpRasConn
->hrasconn
;
537 void wxDialUpManagerMSW::CleanUpThreadData()
541 if ( !SetEvent(m_data
.hEventQuit
) )
543 wxLogLastError("SetEvent(RasThreadQuit)");
546 CloseHandle(m_hThread
);
553 DestroyWindow(m_data
.hWnd
);
558 if ( m_data
.hEventQuit
)
560 CloseHandle(m_data
.hEventQuit
);
562 m_data
.hEventQuit
= 0;
565 if ( m_data
.hEventRas
)
567 CloseHandle(m_data
.hEventRas
);
569 m_data
.hEventRas
= 0;
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
577 void wxDialUpManagerMSW::CheckRasStatus()
579 // use int, not bool to compare with -1
580 int isConnected
= FindActiveConnection() != 0;
581 if ( isConnected
!= ms_isConnected
)
583 if ( ms_isConnected
!= -1 )
585 // notify the program
586 NotifyApp(isConnected
!= 0);
588 // else: it's the first time we're called, just update the flag
590 ms_isConnected
= isConnected
;
594 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
596 wxDialUpEvent
event(connected
, fromOurselves
);
597 (void)wxTheApp
->ProcessEvent(event
);
600 // this function is called whenever the status of any RAS connection on this
601 // machine changes by RAS itself
602 void wxDialUpManagerMSW::OnConnectStatusChange()
604 // we know that status changed, but we don't know whether we're connected
605 // or not - so find it out
609 // this function is called by our callback which we give to RasDial() when
610 // calling it asynchronously
611 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
616 // this probably means that CancelDialing() was called and we get
617 // "disconnected" notification
621 // we're only interested in 2 events: connected and disconnected
624 wxLogError(_("Failed to establish dialup connection: %s"),
625 GetErrorString(dwError
));
627 // we should still call RasHangUp() if we got a non 0 connection
628 if ( ms_hRasConnection
)
630 ms_pfnRasHangUp(ms_hRasConnection
);
631 ms_hRasConnection
= 0;
636 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
638 else if ( rasconnstate
== RASCS_Connected
)
640 ms_isConnected
= TRUE
;
643 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
647 // ----------------------------------------------------------------------------
648 // implementation of wxDialUpManager functions
649 // ----------------------------------------------------------------------------
651 bool wxDialUpManagerMSW::IsOk() const
653 return ms_dllRas
!= 0;
656 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
659 DWORD size
= sizeof(RASENTRYNAME
);
660 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
661 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
667 dwRet
= ms_pfnRasEnumEntries
670 NULL
, // default phone book (or all)
671 rasEntries
, // [out] buffer for the entries
672 &size
, // [in/out] size of the buffer
673 &nEntries
// [out] number of entries fetched
676 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
678 // reallocate the buffer
679 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
681 else if ( dwRet
!= 0 )
683 // some other error - abort
684 wxLogError(_("Failed to get ISP names: %s"), GetErrorString(dwRet
));
691 while ( dwRet
!= 0 );
695 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
697 names
.Add(rasEntries
[n
].szEntryName
);
702 // return the number of entries
703 return names
.GetCount();
706 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
707 const wxString
& username
,
708 const wxString
& password
,
711 // check preconditions
712 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
714 if ( ms_hRasConnection
)
716 wxFAIL_MSG(wxT("there is already an active connection"));
721 // get the default ISP if none given
722 wxString
entryName(nameOfISP
);
726 size_t count
= GetISPNames(names
);
730 // no known ISPs, abort
731 wxLogError(_("Failed to connect: no ISP to dial."));
736 // only one ISP, choose it
737 entryName
= names
[0u];
741 // several ISPs, let the user choose
743 wxString
*strings
= new wxString
[count
];
744 for ( size_t i
= 0; i
< count
; i
++ )
746 strings
[i
] = names
[i
];
749 entryName
= wxGetSingleChoice
751 _("Choose ISP to dial"),
752 _("Please choose which ISP do you want to "
769 RASDIALPARAMS rasDialParams
;
770 rasDialParams
.dwSize
= sizeof(rasDialParams
);
771 strncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
773 // do we have the username and password?
774 if ( !username
|| !password
)
777 DWORD dwRet
= ms_pfnRasGetEntryDialParams
779 NULL
, // default phonebook
780 &rasDialParams
, // [in/out] the params of this entry
781 &gotPassword
// [out] did we get password?
786 wxLogError(_("Failed to connect: missing username/password."));
793 strncpy(rasDialParams
.szUserName
, username
, UNLEN
);
794 strncpy(rasDialParams
.szPassword
, password
, PWLEN
);
797 // default values for other fields
798 rasDialParams
.szPhoneNumber
[0] = '\0';
799 rasDialParams
.szCallbackNumber
[0] = '\0';
800 rasDialParams
.szCallbackNumber
[0] = '\0';
802 rasDialParams
.szDomain
[0] = '*';
803 rasDialParams
.szDomain
[1] = '\0';
805 // apparently, this is not really necessary - passing NULL instead of the
806 // phone book has the same effect
809 if ( wxGetOsVersion() == wxWINDOWS_NT
)
811 // first get the length
812 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
815 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
817 wxLogSysError(_("Cannot find the location of address book file"));
820 phoneBook
.UngetWriteBuf();
822 // this is the default phone book
823 phoneBook
<< "\\ras\\rasphone.pbk";
827 // TODO may be we should disable auto check while async dialing is in
832 DWORD dwRet
= ms_pfnRasDial
834 (LPRASDIALEXTENSIONS
)NULL
, // no extended features
835 NULL
, // default phone book file (NT only)
837 0, // use callback for notifications
838 async
? wxRasDialFunc
// the callback
839 : 0, // no notifications - sync operation
845 wxLogError(_("Failed to %s dialup connection: %s"),
846 async
? _("initiate") : _("establish"),
847 GetErrorString(dwRet
));
849 // we should still call RasHangUp() if we got a non 0 connection
850 if ( ms_hRasConnection
)
852 ms_pfnRasHangUp(ms_hRasConnection
);
853 ms_hRasConnection
= 0;
861 // for async dialing, we're not yet connected
864 ms_isConnected
= TRUE
;
870 bool wxDialUpManagerMSW::IsDialing() const
872 return GetDialer() != NULL
;
875 bool wxDialUpManagerMSW::CancelDialing()
883 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
890 bool wxDialUpManagerMSW::HangUp()
892 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
894 // we may terminate either the connection we initiated or another one which
897 if ( ms_hRasConnection
)
899 hRasConn
= ms_hRasConnection
;
901 ms_hRasConnection
= 0;
905 hRasConn
= FindActiveConnection();
910 wxLogError(_("Cannot hang up - no active dialup connection."));
915 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
918 wxLogError(_("Failed to terminate the dialup connection: %s"),
919 GetErrorString(dwRet
));
922 ms_isConnected
= FALSE
;
927 bool wxDialUpManagerMSW::IsAlwaysOnline() const
929 // we cache the result (presumably this won't change while the program is
931 if ( ms_isAlwaysOnline
!= -1 )
933 return ms_isAlwaysOnline
!= 0;
936 // try to use WinInet function first
938 wxDllType hDll
= wxDllLoader::LoadLibrary(_T("WININET"), &ok
);
941 typedef BOOL (*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
942 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
944 #define RESOLVE_FUNCTION(type, name) \
945 pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name))
947 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
949 if ( pfnInternetGetConnectedState
)
952 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
954 // there is some connection to the net, see of which type
955 ms_isAlwaysOnline
= (flags
& INTERNET_CONNECTION_LAN
!= 0) ||
956 (flags
& INTERNET_CONNECTION_PROXY
!= 0);
958 wxLogMessage("InternetGetConnectedState() returned TRUE, "
959 "flags = %08x", flags
);
963 // no Internet connection at all
964 ms_isAlwaysOnline
= FALSE
;
968 wxDllLoader::UnloadLibrary(hDll
);
971 // did we succeed with WinInet? if not, try something else
972 if ( ms_isAlwaysOnline
== -1 )
976 // definitely no permanent connection because we are not connected
978 ms_isAlwaysOnline
= FALSE
;
982 // of course, having a modem doesn't prevent us from having a
983 // permanent connection as well, but we have to guess somehow and
984 // it's probably more common that a system connected via a modem
985 // doesn't have any other net access, so:
986 ms_isAlwaysOnline
= FALSE
;
990 wxASSERT_MSG( ms_isAlwaysOnline
!= -1, wxT("logic error") );
992 return ms_isAlwaysOnline
!= 0;
995 bool wxDialUpManagerMSW::IsOnline() const
997 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
999 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1001 // user specified flag overrides our logic
1002 return ms_userSpecifiedOnlineStatus
!= 0;
1006 // return TRUE if there is at least one active connection
1007 return FindActiveConnection() != 0;
1011 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1013 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1015 ms_userSpecifiedOnlineStatus
= isOnline
;
1018 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1020 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1022 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1026 // we're running under NT 4.0, Windows 98 or later and can use
1027 // RasConnectionNotification() to be notified by a secondary thread
1029 // first, see if we don't have this thread already running
1030 if ( m_hThread
!= 0 )
1032 DWORD dwSuspendCount
= 2;
1033 while ( dwSuspendCount
> 1 )
1035 dwSuspendCount
= ResumeThread(m_hThread
);
1036 if ( dwSuspendCount
== (DWORD
)-1 )
1038 wxLogLastError("ResumeThread(RasThread)");
1051 // create all the stuff we need to be notified about RAS connection
1056 // first create an event to wait on
1057 m_data
.hEventRas
= CreateEvent
1059 NULL
, // security attribute (default)
1060 FALSE
, // manual reset (not)
1061 FALSE
, // initial state (not signaled)
1064 if ( !m_data
.hEventRas
)
1066 wxLogLastError("CreateEvent(RasStatus)");
1074 // create the event we use to quit the thread
1075 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1076 if ( !m_data
.hEventQuit
)
1078 wxLogLastError("CreateEvent(RasThreadQuit)");
1080 CleanUpThreadData();
1086 if ( ok
&& !ms_hwndRas
)
1088 // create a hidden window to receive notification about connections
1090 extern const wxChar
*wxPanelClassName
;
1091 ms_hwndRas
= ::CreateWindow(wxPanelClassName
, NULL
,
1094 (HMENU
)NULL
, wxGetInstance(), 0);
1097 wxLogLastError("CreateWindow(RasHiddenWindow)");
1099 CleanUpThreadData();
1105 FARPROC windowProc
= MakeProcInstance
1107 (FARPROC
)wxRasStatusWindowProc
,
1111 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1114 m_data
.hWnd
= ms_hwndRas
;
1118 // start the secondary thread
1119 m_data
.dialUpManager
= this;
1122 m_hThread
= CreateThread
1126 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1134 wxLogLastError("CreateThread(RasStatusThread)");
1136 CleanUpThreadData();
1142 // start receiving RAS notifications
1143 DWORD dwRet
= ms_pfnRasConnectionNotification
1145 (HRASCONN
)INVALID_HANDLE_VALUE
,
1147 3 /* RASCN_Connection | RASCN_Disconnection */
1152 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1153 GetErrorString(dwRet
));
1155 CleanUpThreadData();
1163 // we're running under Windows 95 and have to poll ourselves
1164 // (or, alternatively, the code above for NT/98 failed)
1165 m_timerStatusPolling
.Stop();
1166 if ( nSeconds
== 0 )
1171 m_timerStatusPolling
.Start(nSeconds
* 1000);
1176 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1178 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1182 // we have running secondary thread, it's just enough to suspend it
1183 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1185 wxLogLastError("SuspendThread(RasThread)");
1190 // even simpler - just stop the timer
1191 m_timerStatusPolling
.Stop();
1195 // ----------------------------------------------------------------------------
1196 // stubs which don't do anything in MSW version
1197 // ----------------------------------------------------------------------------
1199 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1202 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1204 // nothing to do - we don't use this
1207 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1208 const wxString
& WXUNUSED(hangup
))
1210 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1212 // nothing to do - we don't use this
1215 // ----------------------------------------------------------------------------
1217 // ----------------------------------------------------------------------------
1219 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1222 handles
[0] = data
->hEventRas
;
1223 handles
[1] = data
->hEventQuit
;
1228 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1233 // RAS connection status changed
1234 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1238 case WAIT_OBJECT_0
+ 1:
1243 wxLogLastError("WaitForMultipleObjects(RasMonitor)");
1251 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1252 WPARAM wParam
, LPARAM lParam
)
1254 if ( message
== wxWM_RAS_STATUS_CHANGED
)
1256 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1257 data
->dialUpManager
->OnConnectStatusChange();
1259 else if ( message
== wxWM_RAS_DIALING_PROGRESS
)
1261 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1263 dialUpManager
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1269 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1270 RASCONNSTATE rasconnstate
,
1273 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1275 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1277 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1278 rasconnstate
, dwError
);
1283 #endif // wxUSE_DIALUP_MANAGER