1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dialup.cpp
3 // Purpose: MSW implementation of network/dialup classes and functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 // these functions require Win32
28 #if defined(__WIN16__) && wxUSE_DIALUP_MANAGER
29 #undef wxUSE_DIALUP_MANAGER
30 #define wxUSE_DIALUP_MANAGER 0
31 #endif // wxUSE_DIALUP_MANAGER && Win16
33 #if wxUSE_DIALUP_MANAGER
41 #include "wx/msw/private.h" // must be before #include "dynlib.h"
43 #if !wxUSE_DYNLIB_CLASS
44 #error You need wxUSE_DYNLIB_CLASS to be 1 to compile dialup.cpp.
47 #include "wx/dynlib.h"
49 #include "wx/dialup.h"
51 // Doesn't yet compile under VC++ 4, BC++, mingw, Watcom C++: no wininet.h
52 #if !defined(__BORLANDC__) && !defined(__GNUWIN32_OLD__) && !defined(__GNUWIN32__) && !defined(__WATCOMC__) && ! (defined(__VISUALC__) && (__VISUALC__ < 1020))
60 #ifndef INTERNET_CONNECTION_LAN
61 #define INTERNET_CONNECTION_LAN 2
63 #ifndef INTERNET_CONNECTION_PROXY
64 #define INTERNET_CONNECTION_PROXY 4
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // this message is sent by the secondary thread when RAS status changes
72 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
73 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // the signatures of RAS functions: all this is quite heavy, but we must do it
80 // to allow running wxWin programs on machine which don't have RAS installed
81 // (this does exist) - if we link with rasapi32.lib, the program will fail on
82 // startup because of the missing DLL...
85 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
86 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
87 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
88 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
89 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
90 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
91 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
92 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
93 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
94 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
95 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
96 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
97 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
98 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
99 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
100 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
101 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
102 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
103 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
105 static const char gs_funcSuffix
= 'A';
107 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
108 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
109 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
110 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
111 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
112 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
113 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
114 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
115 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
116 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
117 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
118 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
119 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
120 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
121 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
122 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
123 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
124 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
125 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
127 static const char gs_funcSuffix
= 'W';
128 #endif // ASCII/Unicode
130 // structure passed to the secondary thread
131 struct wxRasThreadData
136 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
137 dialUpManager
= NULL
;
140 HWND hWnd
; // window to send notifications to
141 HANDLE hEventRas
, // event which RAS signals when status changes
142 hEventQuit
; // event which we signal when we terminate
144 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
147 // ----------------------------------------------------------------------------
148 // wxDialUpManager class for MSW
149 // ----------------------------------------------------------------------------
151 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
155 wxDialUpManagerMSW();
156 virtual ~wxDialUpManagerMSW();
158 // implement base class pure virtuals
159 virtual bool IsOk() const;
160 virtual size_t GetISPNames(wxArrayString
& names
) const;
161 virtual bool Dial(const wxString
& nameOfISP
,
162 const wxString
& username
,
163 const wxString
& password
,
165 virtual bool IsDialing() const;
166 virtual bool CancelDialing();
167 virtual bool HangUp();
168 virtual bool IsAlwaysOnline() const;
169 virtual bool IsOnline() const;
170 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
171 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
172 virtual void DisableAutoCheckOnlineStatus();
173 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
174 virtual void SetConnectCommand(const wxString
& commandDial
,
175 const wxString
& commandHangup
);
178 void CheckRasStatus();
180 // for wxRasStatusWindowProc
181 void OnConnectStatusChange();
182 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
185 static HWND
GetRasWindow() { return ms_hwndRas
; }
186 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
189 // return the error string for the given RAS error code
190 static wxString
GetErrorString(DWORD error
);
192 // find the (first) handle of the active connection
193 static HRASCONN
FindActiveConnection();
195 // notify the application about status change
196 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
198 // destroy the thread data and the thread itself
199 void CleanUpThreadData();
201 // timer used for polling RAS status
202 class RasTimer
: public wxTimer
205 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
206 { m_dialUpManager
= dialUpManager
; }
208 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
211 wxDialUpManagerMSW
*m_dialUpManager
;
212 } m_timerStatusPolling
;
214 // thread handle for the thread sitting on connection change event
217 // data used by this thread and our hidden window to send messages between
219 wxRasThreadData m_data
;
221 // the hidden window we use for passing messages between threads
222 static HWND ms_hwndRas
;
224 // the handle of the connection we initiated or 0 if none
225 static HRASCONN ms_hRasConnection
;
227 // the use count of rasapi32.dll
228 static int ms_nDllCount
;
230 // the handle of rasapi32.dll when it's loaded
231 static wxDllType ms_dllRas
;
233 // the pointers to RAS functions
234 static RASDIAL ms_pfnRasDial
;
235 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
236 static RASENUMENTRIES ms_pfnRasEnumEntries
;
237 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
238 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
239 static RASHANGUP ms_pfnRasHangUp
;
240 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
241 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
242 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
243 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
244 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
245 static RASENUMDEVICES ms_pfnRasEnumDevices
;
246 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
247 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
248 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
249 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
250 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
251 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
253 // this function is not supported by Win95
254 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
256 // if this flag is different from -1, it overrides IsOnline()
257 static int ms_userSpecifiedOnlineStatus
;
259 // this flag tells us if we're online
260 static int ms_isConnected
;
262 // this flag is the result of the call to IsAlwaysOnline() (-1 if not
264 static int ms_isAlwaysOnline
;
266 // this flag tells us whether a call to RasDial() is in progress
267 static wxDialUpManagerMSW
*ms_dialer
;
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
275 WPARAM wParam
, LPARAM lParam
);
277 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
279 static void WINAPI
wxRasDialFunc(UINT unMsg
,
280 RASCONNSTATE rasconnstate
,
283 // ============================================================================
285 // ============================================================================
287 // ----------------------------------------------------------------------------
288 // init the static variables
289 // ----------------------------------------------------------------------------
291 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
293 HWND
wxDialUpManagerMSW::ms_hwndRas
= 0;
295 int wxDialUpManagerMSW::ms_nDllCount
= 0;
296 wxDllType
wxDialUpManagerMSW::ms_dllRas
= 0;
298 RASDIAL
wxDialUpManagerMSW::ms_pfnRasDial
= 0;
299 RASENUMCONNECTIONS
wxDialUpManagerMSW::ms_pfnRasEnumConnections
= 0;
300 RASENUMENTRIES
wxDialUpManagerMSW::ms_pfnRasEnumEntries
= 0;
301 RASGETCONNECTSTATUS
wxDialUpManagerMSW::ms_pfnRasGetConnectStatus
= 0;
302 RASGETERRORSTRING
wxDialUpManagerMSW::ms_pfnRasGetErrorString
= 0;
303 RASHANGUP
wxDialUpManagerMSW::ms_pfnRasHangUp
= 0;
304 RASGETPROJECTIONINFO
wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo
= 0;
305 RASCREATEPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry
= 0;
306 RASEDITPHONEBOOKENTRY
wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry
= 0;
307 RASSETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams
= 0;
308 RASGETENTRYDIALPARAMS
wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams
= 0;
309 RASENUMDEVICES
wxDialUpManagerMSW::ms_pfnRasEnumDevices
= 0;
310 RASGETCOUNTRYINFO
wxDialUpManagerMSW::ms_pfnRasGetCountryInfo
= 0;
311 RASGETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasGetEntryProperties
= 0;
312 RASSETENTRYPROPERTIES
wxDialUpManagerMSW::ms_pfnRasSetEntryProperties
= 0;
313 RASRENAMEENTRY
wxDialUpManagerMSW::ms_pfnRasRenameEntry
= 0;
314 RASDELETEENTRY
wxDialUpManagerMSW::ms_pfnRasDeleteEntry
= 0;
315 RASVALIDATEENTRYNAME
wxDialUpManagerMSW::ms_pfnRasValidateEntryName
= 0;
316 RASCONNECTIONNOTIFICATION
wxDialUpManagerMSW::ms_pfnRasConnectionNotification
= 0;
318 int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus
= -1;
319 int wxDialUpManagerMSW::ms_isConnected
= -1;
320 int wxDialUpManagerMSW::ms_isAlwaysOnline
= -1;
321 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
323 // ----------------------------------------------------------------------------
324 // ctor and dtor: the dynamic linking happens here
325 // ----------------------------------------------------------------------------
327 // the static creator function is implemented here
328 wxDialUpManager
*wxDialUpManager::Create()
330 return new wxDialUpManagerMSW
;
334 // warning about "'this' : used in base member initializer list" - so what?
335 #pragma warning(disable:4355)
338 wxDialUpManagerMSW::wxDialUpManagerMSW()
339 : m_timerStatusPolling(this)
341 // initialize our data
344 if ( !ms_nDllCount
++ )
346 // load the RAS library
347 ms_dllRas
= wxDllLoader::LoadLibrary("RASAPI32");
350 wxLogError(_("Dial up functions are unavailable because the "
351 "remote access service (RAS) is not installed "
352 "on this machine. Please install it."));
356 // resolve the functions we need
358 // this will contain the name of the function we failed to resolve
360 const char *funcName
= NULL
;
362 // get the function from rasapi32.dll and abort if it's not found
363 #define RESOLVE_RAS_FUNCTION(type, name) \
364 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
365 wxString(_T(#name)) + gs_funcSuffix); \
366 if ( !ms_pfn##name ) \
372 // a variant of above macro which doesn't abort if the function is
373 // not found in the DLL
374 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
375 ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \
376 wxString(_T(#name)) + gs_funcSuffix);
378 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
379 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
380 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
381 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
382 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
383 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
384 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
386 // suppress wxDllLoader messages about missing (non essential)
391 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
392 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
393 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
394 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
395 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
396 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
397 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
398 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
399 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
400 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
401 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
402 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
405 // keep your preprocessor name space clean
406 #undef RESOLVE_RAS_FUNCTION
407 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
412 wxLogError(_("The version of remote access service (RAS) "
413 "installed on this machine is too old, please "
414 "upgrade (the following required function is "
415 "missing: %s)."), funcName
);
417 wxDllLoader::UnloadLibrary(ms_dllRas
);
426 // enable auto check by default
427 EnableAutoCheckOnlineStatus(0);
430 wxDialUpManagerMSW::~wxDialUpManagerMSW()
434 if ( !--ms_nDllCount
)
436 // unload the RAS library
437 wxDllLoader::UnloadLibrary(ms_dllRas
);
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
448 wxChar buffer
[512]; // this should be more than enough according to MS docs
449 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
452 case ERROR_INVALID_PARAMETER
:
453 // this was a standard Win32 error probably
454 return wxString(wxSysErrorMsg(error
));
458 wxLogSysError(dwRet
, _("Failed to retrieve text of RAS "
462 msg
.Printf(_("unknown error (error code %08x)."), error
);
467 // we want the error message to start from a lower case letter
468 buffer
[0] = wxTolower(buffer
[0]);
470 return wxString(buffer
);
474 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
476 // enumerate connections
477 DWORD cbBuf
= sizeof(RASCONN
);
478 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
485 lpRasConn
->dwSize
= sizeof(RASCONN
);
487 DWORD nConnections
= 0;
488 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
490 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
492 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
494 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
496 LPRASCONN lpRasConnOld
= lpRasConn
;
497 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
506 else if ( dwRet
== 0 )
514 wxLogError(_("Cannot find active dialup connection: %s"),
515 GetErrorString(dwRet
));
522 switch ( nConnections
)
530 // more than 1 connection - we don't know what to do with this
531 // case, so give a warning but continue (taking the first
532 // connection) - the warning is really needed because this function
533 // is used, for example, to select the connection to hang up and so
534 // we may hang up the wrong connection here...
535 wxLogWarning(_("Several active dialup connections found, "
536 "choosing one randomly."));
540 // exactly 1 connection, great
541 hrasconn
= lpRasConn
->hrasconn
;
549 void wxDialUpManagerMSW::CleanUpThreadData()
553 if ( !SetEvent(m_data
.hEventQuit
) )
555 wxLogLastError("SetEvent(RasThreadQuit)");
558 CloseHandle(m_hThread
);
565 DestroyWindow(m_data
.hWnd
);
570 if ( m_data
.hEventQuit
)
572 CloseHandle(m_data
.hEventQuit
);
574 m_data
.hEventQuit
= 0;
577 if ( m_data
.hEventRas
)
579 CloseHandle(m_data
.hEventRas
);
581 m_data
.hEventRas
= 0;
585 // ----------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------
589 void wxDialUpManagerMSW::CheckRasStatus()
591 // use int, not bool to compare with -1
592 int isConnected
= FindActiveConnection() != 0;
593 if ( isConnected
!= ms_isConnected
)
595 if ( ms_isConnected
!= -1 )
597 // notify the program
598 NotifyApp(isConnected
!= 0);
600 // else: it's the first time we're called, just update the flag
602 ms_isConnected
= isConnected
;
606 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
608 wxDialUpEvent
event(connected
, fromOurselves
);
609 (void)wxTheApp
->ProcessEvent(event
);
612 // this function is called whenever the status of any RAS connection on this
613 // machine changes by RAS itself
614 void wxDialUpManagerMSW::OnConnectStatusChange()
616 // we know that status changed, but we don't know whether we're connected
617 // or not - so find it out
621 // this function is called by our callback which we give to RasDial() when
622 // calling it asynchronously
623 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
628 // this probably means that CancelDialing() was called and we get
629 // "disconnected" notification
633 // we're only interested in 2 events: connected and disconnected
636 wxLogError(_("Failed to establish dialup connection: %s"),
637 GetErrorString(dwError
));
639 // we should still call RasHangUp() if we got a non 0 connection
640 if ( ms_hRasConnection
)
642 ms_pfnRasHangUp(ms_hRasConnection
);
643 ms_hRasConnection
= 0;
648 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
650 else if ( rasconnstate
== RASCS_Connected
)
652 ms_isConnected
= TRUE
;
655 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
659 // ----------------------------------------------------------------------------
660 // implementation of wxDialUpManager functions
661 // ----------------------------------------------------------------------------
663 bool wxDialUpManagerMSW::IsOk() const
665 return ms_dllRas
!= 0;
668 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
671 DWORD size
= sizeof(RASENTRYNAME
);
672 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
673 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
679 dwRet
= ms_pfnRasEnumEntries
682 NULL
, // default phone book (or all)
683 rasEntries
, // [out] buffer for the entries
684 &size
, // [in/out] size of the buffer
685 &nEntries
// [out] number of entries fetched
688 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
690 // reallocate the buffer
691 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
693 else if ( dwRet
!= 0 )
695 // some other error - abort
696 wxLogError(_("Failed to get ISP names: %s"), GetErrorString(dwRet
));
703 while ( dwRet
!= 0 );
707 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
709 names
.Add(rasEntries
[n
].szEntryName
);
714 // return the number of entries
715 return names
.GetCount();
718 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
719 const wxString
& username
,
720 const wxString
& password
,
723 // check preconditions
724 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
726 if ( ms_hRasConnection
)
728 wxFAIL_MSG(wxT("there is already an active connection"));
733 // get the default ISP if none given
734 wxString
entryName(nameOfISP
);
738 size_t count
= GetISPNames(names
);
742 // no known ISPs, abort
743 wxLogError(_("Failed to connect: no ISP to dial."));
748 // only one ISP, choose it
749 entryName
= names
[0u];
753 // several ISPs, let the user choose
755 wxString
*strings
= new wxString
[count
];
756 for ( size_t i
= 0; i
< count
; i
++ )
758 strings
[i
] = names
[i
];
761 entryName
= wxGetSingleChoice
763 _("Choose ISP to dial"),
764 _("Please choose which ISP do you want to "
781 RASDIALPARAMS rasDialParams
;
782 rasDialParams
.dwSize
= sizeof(rasDialParams
);
783 strncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
785 // do we have the username and password?
786 if ( !username
|| !password
)
789 DWORD dwRet
= ms_pfnRasGetEntryDialParams
791 NULL
, // default phonebook
792 &rasDialParams
, // [in/out] the params of this entry
793 &gotPassword
// [out] did we get password?
798 wxLogError(_("Failed to connect: missing username/password."));
805 strncpy(rasDialParams
.szUserName
, username
, UNLEN
);
806 strncpy(rasDialParams
.szPassword
, password
, PWLEN
);
809 // default values for other fields
810 rasDialParams
.szPhoneNumber
[0] = '\0';
811 rasDialParams
.szCallbackNumber
[0] = '\0';
812 rasDialParams
.szCallbackNumber
[0] = '\0';
814 rasDialParams
.szDomain
[0] = '*';
815 rasDialParams
.szDomain
[1] = '\0';
817 // apparently, this is not really necessary - passing NULL instead of the
818 // phone book has the same effect
821 if ( wxGetOsVersion() == wxWINDOWS_NT
)
823 // first get the length
824 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
827 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
829 wxLogSysError(_("Cannot find the location of address book file"));
832 phoneBook
.UngetWriteBuf();
834 // this is the default phone book
835 phoneBook
<< "\\ras\\rasphone.pbk";
839 // TODO may be we should disable auto check while async dialing is in
844 DWORD dwRet
= ms_pfnRasDial
846 (LPRASDIALEXTENSIONS
)NULL
, // no extended features
847 NULL
, // default phone book file (NT only)
849 0, // use callback for notifications
850 async
? wxRasDialFunc
// the callback
851 : 0, // no notifications - sync operation
857 wxLogError(_("Failed to %s dialup connection: %s"),
858 async
? _("initiate") : _("establish"),
859 GetErrorString(dwRet
));
861 // we should still call RasHangUp() if we got a non 0 connection
862 if ( ms_hRasConnection
)
864 ms_pfnRasHangUp(ms_hRasConnection
);
865 ms_hRasConnection
= 0;
873 // for async dialing, we're not yet connected
876 ms_isConnected
= TRUE
;
882 bool wxDialUpManagerMSW::IsDialing() const
884 return GetDialer() != NULL
;
887 bool wxDialUpManagerMSW::CancelDialing()
895 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
902 bool wxDialUpManagerMSW::HangUp()
904 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
906 // we may terminate either the connection we initiated or another one which
909 if ( ms_hRasConnection
)
911 hRasConn
= ms_hRasConnection
;
913 ms_hRasConnection
= 0;
917 hRasConn
= FindActiveConnection();
922 wxLogError(_("Cannot hang up - no active dialup connection."));
927 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
930 wxLogError(_("Failed to terminate the dialup connection: %s"),
931 GetErrorString(dwRet
));
934 ms_isConnected
= FALSE
;
939 bool wxDialUpManagerMSW::IsAlwaysOnline() const
941 // we cache the result (presumably this won't change while the program is
943 if ( ms_isAlwaysOnline
!= -1 )
945 return ms_isAlwaysOnline
!= 0;
948 // try to use WinInet function first
950 wxDllType hDll
= wxDllLoader::LoadLibrary(_T("WININET"), &ok
);
953 typedef BOOL (*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
954 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
956 #define RESOLVE_FUNCTION(type, name) \
957 pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name))
959 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
961 if ( pfnInternetGetConnectedState
)
964 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
966 // there is some connection to the net, see of which type
967 ms_isAlwaysOnline
= (flags
& INTERNET_CONNECTION_LAN
!= 0) ||
968 (flags
& INTERNET_CONNECTION_PROXY
!= 0);
970 wxLogMessage("InternetGetConnectedState() returned TRUE, "
971 "flags = %08x", flags
);
975 // no Internet connection at all
976 ms_isAlwaysOnline
= FALSE
;
980 wxDllLoader::UnloadLibrary(hDll
);
983 // did we succeed with WinInet? if not, try something else
984 if ( ms_isAlwaysOnline
== -1 )
988 // definitely no permanent connection because we are not connected
990 ms_isAlwaysOnline
= FALSE
;
994 // of course, having a modem doesn't prevent us from having a
995 // permanent connection as well, but we have to guess somehow and
996 // it's probably more common that a system connected via a modem
997 // doesn't have any other net access, so:
998 ms_isAlwaysOnline
= FALSE
;
1002 wxASSERT_MSG( ms_isAlwaysOnline
!= -1, wxT("logic error") );
1004 return ms_isAlwaysOnline
!= 0;
1007 bool wxDialUpManagerMSW::IsOnline() const
1009 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1011 if ( ms_userSpecifiedOnlineStatus
!= -1 )
1013 // user specified flag overrides our logic
1014 return ms_userSpecifiedOnlineStatus
!= 0;
1018 // return TRUE if there is at least one active connection
1019 return FindActiveConnection() != 0;
1023 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
1025 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1027 ms_userSpecifiedOnlineStatus
= isOnline
;
1030 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
1032 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
1034 bool ok
= ms_pfnRasConnectionNotification
!= 0;
1038 // we're running under NT 4.0, Windows 98 or later and can use
1039 // RasConnectionNotification() to be notified by a secondary thread
1041 // first, see if we don't have this thread already running
1042 if ( m_hThread
!= 0 )
1044 DWORD dwSuspendCount
= 2;
1045 while ( dwSuspendCount
> 1 )
1047 dwSuspendCount
= ResumeThread(m_hThread
);
1048 if ( dwSuspendCount
== (DWORD
)-1 )
1050 wxLogLastError("ResumeThread(RasThread)");
1063 // create all the stuff we need to be notified about RAS connection
1068 // first create an event to wait on
1069 m_data
.hEventRas
= CreateEvent
1071 NULL
, // security attribute (default)
1072 FALSE
, // manual reset (not)
1073 FALSE
, // initial state (not signaled)
1076 if ( !m_data
.hEventRas
)
1078 wxLogLastError("CreateEvent(RasStatus)");
1086 // create the event we use to quit the thread
1087 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1088 if ( !m_data
.hEventQuit
)
1090 wxLogLastError("CreateEvent(RasThreadQuit)");
1092 CleanUpThreadData();
1098 if ( ok
&& !ms_hwndRas
)
1100 // create a hidden window to receive notification about connections
1102 extern const wxChar
*wxPanelClassName
;
1103 ms_hwndRas
= ::CreateWindow(wxPanelClassName
, NULL
,
1106 (HMENU
)NULL
, wxGetInstance(), 0);
1109 wxLogLastError("CreateWindow(RasHiddenWindow)");
1111 CleanUpThreadData();
1117 FARPROC windowProc
= MakeProcInstance
1119 (FARPROC
)wxRasStatusWindowProc
,
1123 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1126 m_data
.hWnd
= ms_hwndRas
;
1130 // start the secondary thread
1131 m_data
.dialUpManager
= this;
1134 m_hThread
= CreateThread
1138 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1146 wxLogLastError("CreateThread(RasStatusThread)");
1148 CleanUpThreadData();
1154 // start receiving RAS notifications
1155 DWORD dwRet
= ms_pfnRasConnectionNotification
1157 (HRASCONN
)INVALID_HANDLE_VALUE
,
1159 3 /* RASCN_Connection | RASCN_Disconnection */
1164 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1165 GetErrorString(dwRet
));
1167 CleanUpThreadData();
1175 // we're running under Windows 95 and have to poll ourselves
1176 // (or, alternatively, the code above for NT/98 failed)
1177 m_timerStatusPolling
.Stop();
1178 if ( nSeconds
== 0 )
1183 m_timerStatusPolling
.Start(nSeconds
* 1000);
1188 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1190 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1194 // we have running secondary thread, it's just enough to suspend it
1195 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1197 wxLogLastError("SuspendThread(RasThread)");
1202 // even simpler - just stop the timer
1203 m_timerStatusPolling
.Stop();
1207 // ----------------------------------------------------------------------------
1208 // stubs which don't do anything in MSW version
1209 // ----------------------------------------------------------------------------
1211 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1214 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1216 // nothing to do - we don't use this
1219 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1220 const wxString
& WXUNUSED(hangup
))
1222 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1224 // nothing to do - we don't use this
1227 // ----------------------------------------------------------------------------
1229 // ----------------------------------------------------------------------------
1231 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1234 handles
[0] = data
->hEventRas
;
1235 handles
[1] = data
->hEventQuit
;
1240 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1245 // RAS connection status changed
1246 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1250 case WAIT_OBJECT_0
+ 1:
1255 wxLogLastError("WaitForMultipleObjects(RasMonitor)");
1263 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1264 WPARAM wParam
, LPARAM lParam
)
1266 if ( message
== wxWM_RAS_STATUS_CHANGED
)
1268 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1269 data
->dialUpManager
->OnConnectStatusChange();
1271 else if ( message
== wxWM_RAS_DIALING_PROGRESS
)
1273 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1275 dialUpManager
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1281 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1282 RASCONNSTATE rasconnstate
,
1285 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1287 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1289 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1290 rasconnstate
, dwError
);
1295 #endif // wxUSE_DIALUP_MANAGER