1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dialup.cpp
3 // Purpose: MSW implementation of network/dialup classes and functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 // these functions require Win32
28 #if defined(__WIN16__) && wxUSE_DIALUP_MANAGER
29 #undef wxUSE_DIALUP_MANAGER
30 #define wxUSE_DIALUP_MANAGER 0
31 #endif // wxUSE_DIALUP_MANAGER && Win16
33 #if wxUSE_DIALUP_MANAGER
43 #include "wx/generic/choicdgg.h"
45 #include "wx/dynlib.h"
46 #include "wx/dialup.h"
48 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED
)
49 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED
)
51 // Doesn't yet compile under VC++ 4, BC++, Watcom C++: no wininet.h
52 #if !defined(__BORLANDC__) && \
53 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
54 !defined(__GNUWIN32_OLD__) && \
55 !defined(__WATCOMC__) && \
56 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
64 #ifndef INTERNET_CONNECTION_LAN
65 #define INTERNET_CONNECTION_LAN 2
67 #ifndef INTERNET_CONNECTION_PROXY
68 #define INTERNET_CONNECTION_PROXY 4
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // this message is sent by the secondary thread when RAS status changes
76 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
77 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // the signatures of RAS functions: all this is quite heavy, but we must do it
84 // to allow running wxWin programs on machine which don't have RAS installed
85 // (this does exist) - if we link with rasapi32.lib, the program will fail on
86 // startup because of the missing DLL...
89 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCSTR
, LPRASDIALPARAMSA
, DWORD
, LPVOID
, LPHRASCONN
);
90 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNA
, LPDWORD
, LPDWORD
);
91 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCSTR
, LPCSTR
, LPRASENTRYNAMEA
, LPDWORD
, LPDWORD
);
92 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSA
);
93 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPSTR
, DWORD
);
94 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
95 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
96 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCSTR
);
97 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCSTR
, LPCSTR
);
98 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, BOOL
);
99 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCSTR
, LPRASDIALPARAMSA
, LPBOOL
);
100 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOA
, LPDWORD
, LPDWORD
);
101 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOA
, LPDWORD
);
102 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, LPDWORD
, LPBYTE
, LPDWORD
);
103 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCSTR
, LPCSTR
, LPRASENTRYA
, DWORD
, LPBYTE
, DWORD
);
104 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCSTR
, LPCSTR
, LPCSTR
);
105 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCSTR
, LPCSTR
);
106 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCSTR
, LPCSTR
);
107 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
109 static const wxChar gs_funcSuffix
= _T('A');
111 typedef DWORD (APIENTRY
* RASDIAL
)( LPRASDIALEXTENSIONS
, LPCWSTR
, LPRASDIALPARAMSW
, DWORD
, LPVOID
, LPHRASCONN
);
112 typedef DWORD (APIENTRY
* RASENUMCONNECTIONS
)( LPRASCONNW
, LPDWORD
, LPDWORD
);
113 typedef DWORD (APIENTRY
* RASENUMENTRIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYNAMEW
, LPDWORD
, LPDWORD
);
114 typedef DWORD (APIENTRY
* RASGETCONNECTSTATUS
)( HRASCONN
, LPRASCONNSTATUSW
);
115 typedef DWORD (APIENTRY
* RASGETERRORSTRING
)( UINT
, LPWSTR
, DWORD
);
116 typedef DWORD (APIENTRY
* RASHANGUP
)( HRASCONN
);
117 typedef DWORD (APIENTRY
* RASGETPROJECTIONINFO
)( HRASCONN
, RASPROJECTION
, LPVOID
, LPDWORD
);
118 typedef DWORD (APIENTRY
* RASCREATEPHONEBOOKENTRY
)( HWND
, LPCWSTR
);
119 typedef DWORD (APIENTRY
* RASEDITPHONEBOOKENTRY
)( HWND
, LPCWSTR
, LPCWSTR
);
120 typedef DWORD (APIENTRY
* RASSETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, BOOL
);
121 typedef DWORD (APIENTRY
* RASGETENTRYDIALPARAMS
)( LPCWSTR
, LPRASDIALPARAMSW
, LPBOOL
);
122 typedef DWORD (APIENTRY
* RASENUMDEVICES
)( LPRASDEVINFOW
, LPDWORD
, LPDWORD
);
123 typedef DWORD (APIENTRY
* RASGETCOUNTRYINFO
)( LPRASCTRYINFOW
, LPDWORD
);
124 typedef DWORD (APIENTRY
* RASGETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, LPDWORD
, LPBYTE
, LPDWORD
);
125 typedef DWORD (APIENTRY
* RASSETENTRYPROPERTIES
)( LPCWSTR
, LPCWSTR
, LPRASENTRYW
, DWORD
, LPBYTE
, DWORD
);
126 typedef DWORD (APIENTRY
* RASRENAMEENTRY
)( LPCWSTR
, LPCWSTR
, LPCWSTR
);
127 typedef DWORD (APIENTRY
* RASDELETEENTRY
)( LPCWSTR
, LPCWSTR
);
128 typedef DWORD (APIENTRY
* RASVALIDATEENTRYNAME
)( LPCWSTR
, LPCWSTR
);
129 typedef DWORD (APIENTRY
* RASCONNECTIONNOTIFICATION
)( HRASCONN
, HANDLE
, DWORD
);
131 static const wxChar gs_funcSuffix
= _T('W');
132 #endif // ASCII/Unicode
134 // structure passed to the secondary thread
135 struct WXDLLEXPORT wxRasThreadData
140 hEventRas
= hEventQuit
= INVALID_HANDLE_VALUE
;
141 dialUpManager
= NULL
;
144 HWND hWnd
; // window to send notifications to
145 HANDLE hEventRas
, // event which RAS signals when status changes
146 hEventQuit
; // event which we signal when we terminate
148 class WXDLLEXPORT wxDialUpManagerMSW
*dialUpManager
; // the owner
151 // ----------------------------------------------------------------------------
152 // wxDialUpManager class for MSW
153 // ----------------------------------------------------------------------------
155 class WXDLLEXPORT wxDialUpManagerMSW
: public wxDialUpManager
159 wxDialUpManagerMSW();
160 virtual ~wxDialUpManagerMSW();
162 // implement base class pure virtuals
163 virtual bool IsOk() const;
164 virtual size_t GetISPNames(wxArrayString
& names
) const;
165 virtual bool Dial(const wxString
& nameOfISP
,
166 const wxString
& username
,
167 const wxString
& password
,
169 virtual bool IsDialing() const;
170 virtual bool CancelDialing();
171 virtual bool HangUp();
172 virtual bool IsAlwaysOnline() const;
173 virtual bool IsOnline() const;
174 virtual void SetOnlineStatus(bool isOnline
= TRUE
);
175 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
176 virtual void DisableAutoCheckOnlineStatus();
177 virtual void SetWellKnownHost(const wxString
& hostname
, int port
);
178 virtual void SetConnectCommand(const wxString
& commandDial
,
179 const wxString
& commandHangup
);
182 void CheckRasStatus();
184 // for wxRasStatusWindowProc
185 void OnConnectStatusChange();
186 void OnDialProgress(RASCONNSTATE rasconnstate
, DWORD dwError
);
189 static HWND
GetRasWindow() { return ms_hwndRas
; }
190 static wxDialUpManagerMSW
*GetDialer() { return ms_dialer
; }
193 // return the error string for the given RAS error code
194 static wxString
GetErrorString(DWORD error
);
196 // find the (first) handle of the active connection
197 static HRASCONN
FindActiveConnection();
199 // notify the application about status change
200 void NotifyApp(bool connected
, bool fromOurselves
= FALSE
) const;
202 // destroy the thread data and the thread itself
203 void CleanUpThreadData();
205 // timer used for polling RAS status
206 class WXDLLEXPORT RasTimer
: public wxTimer
209 RasTimer(wxDialUpManagerMSW
*dialUpManager
)
210 { m_dialUpManager
= dialUpManager
; }
212 virtual void Notify() { m_dialUpManager
->CheckRasStatus(); }
215 wxDialUpManagerMSW
*m_dialUpManager
;
216 } m_timerStatusPolling
;
218 // thread handle for the thread sitting on connection change event
221 // data used by this thread and our hidden window to send messages between
223 wxRasThreadData m_data
;
225 // the handle of rasapi32.dll when it's loaded
226 wxPluginManager m_dllRas
;
228 // the hidden window we use for passing messages between threads
229 static HWND ms_hwndRas
;
231 // the handle of the connection we initiated or 0 if none
232 static HRASCONN ms_hRasConnection
;
234 // FIXME: There is probably no reason these really need to
235 // be static anymore since the dll refcounting is
236 // handled by wxPluginManager now. Whether or not
237 // we still _want_ them to be static is another
240 // the pointers to RAS functions
241 static RASDIAL ms_pfnRasDial
;
242 static RASENUMCONNECTIONS ms_pfnRasEnumConnections
;
243 static RASENUMENTRIES ms_pfnRasEnumEntries
;
244 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus
;
245 static RASGETERRORSTRING ms_pfnRasGetErrorString
;
246 static RASHANGUP ms_pfnRasHangUp
;
247 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo
;
248 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry
;
249 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry
;
250 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams
;
251 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams
;
252 static RASENUMDEVICES ms_pfnRasEnumDevices
;
253 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo
;
254 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties
;
255 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties
;
256 static RASRENAMEENTRY ms_pfnRasRenameEntry
;
257 static RASDELETEENTRY ms_pfnRasDeleteEntry
;
258 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName
;
260 // this function is not supported by Win95
261 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification
;
263 // if this flag is different from -1, it overrides IsOnline()
264 static int ms_userSpecifiedOnlineStatus
;
266 // this flag tells us if we're online
267 static int ms_isConnected
;
269 // this flag tells us whether a call to RasDial() is in progress
270 static wxDialUpManagerMSW
*ms_dialer
;
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 static LRESULT WINAPI
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
278 WPARAM wParam
, LPARAM lParam
);
280 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
);
282 static void WINAPI
wxRasDialFunc(UINT unMsg
,
283 RASCONNSTATE rasconnstate
,
286 // ============================================================================
288 // ============================================================================
290 // ----------------------------------------------------------------------------
291 // init the static variables
292 // ----------------------------------------------------------------------------
294 HRASCONN
wxDialUpManagerMSW::ms_hRasConnection
= 0;
296 HWND
wxDialUpManagerMSW::ms_hwndRas
= 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 wxDialUpManagerMSW
*wxDialUpManagerMSW::ms_dialer
= NULL
;
322 // ----------------------------------------------------------------------------
323 // ctor and dtor: the dynamic linking happens here
324 // ----------------------------------------------------------------------------
326 // the static creator function is implemented here
327 wxDialUpManager
*wxDialUpManager::Create()
329 return new wxDialUpManagerMSW
;
333 // warning about "'this' : used in base member initializer list" - so what?
334 #pragma warning(disable:4355)
337 wxDialUpManagerMSW::wxDialUpManagerMSW()
338 : m_timerStatusPolling(this)
339 , m_dllRas(_T("RASAPI32"))
341 // initialize our data
344 if ( !m_dllRas
.IsLoaded() )
346 wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it."));
348 else if ( !ms_pfnRasDial
)
350 // resolve the functions we need
352 // this will contain the name of the function we failed to resolve
354 const char *funcName
= NULL
;
356 // get the function from rasapi32.dll and abort if it's not found
357 #define RESOLVE_RAS_FUNCTION(type, name) \
358 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
360 if ( !ms_pfn##name ) \
366 // a variant of above macro which doesn't abort if the function is
367 // not found in the DLL
368 #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \
369 ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \
372 RESOLVE_RAS_FUNCTION(RASDIAL
, RasDial
);
373 RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS
, RasEnumConnections
);
374 RESOLVE_RAS_FUNCTION(RASENUMENTRIES
, RasEnumEntries
);
375 RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS
, RasGetConnectStatus
);
376 RESOLVE_RAS_FUNCTION(RASGETERRORSTRING
, RasGetErrorString
);
377 RESOLVE_RAS_FUNCTION(RASHANGUP
, RasHangUp
);
378 RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS
, RasGetEntryDialParams
);
380 // suppress error messages about missing (non essential) functions
384 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO
, RasGetProjectionInfo
);
385 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY
, RasCreatePhonebookEntry
);
386 RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY
, RasEditPhonebookEntry
);
387 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS
, RasSetEntryDialParams
);
388 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES
, RasGetEntryProperties
);
389 RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES
, RasSetEntryProperties
);
390 RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY
, RasRenameEntry
);
391 RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY
, RasDeleteEntry
);
392 RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME
, RasValidateEntryName
);
393 RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO
, RasGetCountryInfo
);
394 RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES
, RasEnumDevices
);
395 RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION
, RasConnectionNotification
);
398 // keep your preprocessor name space clean
399 #undef RESOLVE_RAS_FUNCTION
400 #undef RESOLVE_OPTIONAL_RAS_FUNCTION
405 static const wxChar
*msg
= wxTRANSLATE(
406 "The version of remote access service (RAS) installed on this machine is too\
407 old, please upgrade (the following required function is missing: %s)."
410 wxLogError(wxGetTranslation(msg
), funcName
);
416 // enable auto check by default
417 EnableAutoCheckOnlineStatus(0);
420 wxDialUpManagerMSW::~wxDialUpManagerMSW()
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 wxString
wxDialUpManagerMSW::GetErrorString(DWORD error
)
431 wxChar buffer
[512]; // this should be more than enough according to MS docs
432 DWORD dwRet
= ms_pfnRasGetErrorString(error
, buffer
, WXSIZEOF(buffer
));
435 case ERROR_INVALID_PARAMETER
:
436 // this was a standard Win32 error probably
437 return wxString(wxSysErrorMsg(error
));
442 _("Failed to retrieve text of RAS error message"));
445 msg
.Printf(_("unknown error (error code %08x)."), error
);
450 // we want the error message to start from a lower case letter
451 buffer
[0] = wxTolower(buffer
[0]);
453 return wxString(buffer
);
457 HRASCONN
wxDialUpManagerMSW::FindActiveConnection()
459 // enumerate connections
460 DWORD cbBuf
= sizeof(RASCONN
);
461 LPRASCONN lpRasConn
= (LPRASCONN
)malloc(cbBuf
);
468 lpRasConn
->dwSize
= sizeof(RASCONN
);
470 DWORD nConnections
= 0;
471 DWORD dwRet
= ERROR_BUFFER_TOO_SMALL
;
473 while ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
475 dwRet
= ms_pfnRasEnumConnections(lpRasConn
, &cbBuf
, &nConnections
);
477 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
479 LPRASCONN lpRasConnOld
= lpRasConn
;
480 lpRasConn
= (LPRASCONN
)realloc(lpRasConn
, cbBuf
);
489 else if ( dwRet
== 0 )
497 wxLogError(_("Cannot find active dialup connection: %s"),
498 GetErrorString(dwRet
).c_str());
505 switch ( nConnections
)
513 // more than 1 connection - we don't know what to do with this
514 // case, so give a warning but continue (taking the first
515 // connection) - the warning is really needed because this function
516 // is used, for example, to select the connection to hang up and so
517 // we may hang up the wrong connection here...
518 wxLogWarning(_("Several active dialup connections found, choosing one randomly."));
522 // exactly 1 connection, great
523 hrasconn
= lpRasConn
->hrasconn
;
531 void wxDialUpManagerMSW::CleanUpThreadData()
535 if ( !SetEvent(m_data
.hEventQuit
) )
537 wxLogLastError(_T("SetEvent(RasThreadQuit)"));
540 CloseHandle(m_hThread
);
547 DestroyWindow(m_data
.hWnd
);
552 if ( m_data
.hEventQuit
)
554 CloseHandle(m_data
.hEventQuit
);
556 m_data
.hEventQuit
= 0;
559 if ( m_data
.hEventRas
)
561 CloseHandle(m_data
.hEventRas
);
563 m_data
.hEventRas
= 0;
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 void wxDialUpManagerMSW::CheckRasStatus()
573 // use int, not bool to compare with -1
574 int isConnected
= FindActiveConnection() != 0;
575 if ( isConnected
!= ms_isConnected
)
577 if ( ms_isConnected
!= -1 )
579 // notify the program
580 NotifyApp(isConnected
!= 0);
582 // else: it's the first time we're called, just update the flag
584 ms_isConnected
= isConnected
;
588 void wxDialUpManagerMSW::NotifyApp(bool connected
, bool fromOurselves
) const
590 wxDialUpEvent
event(connected
, fromOurselves
);
591 (void)wxTheApp
->ProcessEvent(event
);
594 // this function is called whenever the status of any RAS connection on this
595 // machine changes by RAS itself
596 void wxDialUpManagerMSW::OnConnectStatusChange()
598 // we know that status changed, but we don't know whether we're connected
599 // or not - so find it out
603 // this function is called by our callback which we give to RasDial() when
604 // calling it asynchronously
605 void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate
,
610 // this probably means that CancelDialing() was called and we get
611 // "disconnected" notification
615 // we're only interested in 2 events: connected and disconnected
618 wxLogError(_("Failed to establish dialup connection: %s"),
619 GetErrorString(dwError
).c_str());
621 // we should still call RasHangUp() if we got a non 0 connection
622 if ( ms_hRasConnection
)
624 ms_pfnRasHangUp(ms_hRasConnection
);
625 ms_hRasConnection
= 0;
630 NotifyApp(FALSE
/* !connected */, TRUE
/* we dialed ourselves */);
632 else if ( rasconnstate
== RASCS_Connected
)
634 ms_isConnected
= TRUE
;
637 NotifyApp(TRUE
/* connected */, TRUE
/* we dialed ourselves */);
641 // ----------------------------------------------------------------------------
642 // implementation of wxDialUpManager functions
643 // ----------------------------------------------------------------------------
645 bool wxDialUpManagerMSW::IsOk() const
647 return m_dllRas
.IsLoaded();
650 size_t wxDialUpManagerMSW::GetISPNames(wxArrayString
& names
) const
653 DWORD size
= sizeof(RASENTRYNAME
);
654 RASENTRYNAME
*rasEntries
= (RASENTRYNAME
*)malloc(size
);
655 rasEntries
->dwSize
= sizeof(RASENTRYNAME
);
661 dwRet
= ms_pfnRasEnumEntries
664 NULL
, // default phone book (or all)
665 rasEntries
, // [out] buffer for the entries
666 &size
, // [in/out] size of the buffer
667 &nEntries
// [out] number of entries fetched
670 if ( dwRet
== ERROR_BUFFER_TOO_SMALL
)
672 // reallocate the buffer
673 rasEntries
= (RASENTRYNAME
*)realloc(rasEntries
, size
);
675 else if ( dwRet
!= 0 )
677 // some other error - abort
678 wxLogError(_("Failed to get ISP names: %s"),
679 GetErrorString(dwRet
).c_str());
686 while ( dwRet
!= 0 );
690 for ( size_t n
= 0; n
< (size_t)nEntries
; n
++ )
692 names
.Add(rasEntries
[n
].szEntryName
);
697 // return the number of entries
698 return names
.GetCount();
701 bool wxDialUpManagerMSW::Dial(const wxString
& nameOfISP
,
702 const wxString
& username
,
703 const wxString
& password
,
706 // check preconditions
707 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
709 if ( ms_hRasConnection
)
711 wxFAIL_MSG(wxT("there is already an active connection"));
716 // get the default ISP if none given
717 wxString
entryName(nameOfISP
);
721 size_t count
= GetISPNames(names
);
725 // no known ISPs, abort
726 wxLogError(_("Failed to connect: no ISP to dial."));
731 // only one ISP, choose it
732 entryName
= names
[0u];
736 // several ISPs, let the user choose
738 wxString
*strings
= new wxString
[count
];
739 for ( size_t i
= 0; i
< count
; i
++ )
741 strings
[i
] = names
[i
];
744 entryName
= wxGetSingleChoice
746 _("Choose ISP to dial"),
747 _("Please choose which ISP do you want to connect to"),
763 RASDIALPARAMS rasDialParams
;
764 rasDialParams
.dwSize
= sizeof(rasDialParams
);
765 wxStrncpy(rasDialParams
.szEntryName
, entryName
, RAS_MaxEntryName
);
767 // do we have the username and password?
768 if ( !username
|| !password
)
771 DWORD dwRet
= ms_pfnRasGetEntryDialParams
773 NULL
, // default phonebook
774 &rasDialParams
, // [in/out] the params of this entry
775 &gotPassword
// [out] did we get password?
780 wxLogError(_("Failed to connect: missing username/password."));
787 wxStrncpy(rasDialParams
.szUserName
, username
, UNLEN
);
788 wxStrncpy(rasDialParams
.szPassword
, password
, PWLEN
);
791 // default values for other fields
792 rasDialParams
.szPhoneNumber
[0] = '\0';
793 rasDialParams
.szCallbackNumber
[0] = '\0';
794 rasDialParams
.szCallbackNumber
[0] = '\0';
796 rasDialParams
.szDomain
[0] = '*';
797 rasDialParams
.szDomain
[1] = '\0';
799 // apparently, this is not really necessary - passing NULL instead of the
800 // phone book has the same effect
803 if ( wxGetOsVersion() == wxWINDOWS_NT
)
805 // first get the length
806 UINT nLen
= ::GetSystemDirectory(NULL
, 0);
809 if ( !::GetSystemDirectory(phoneBook
.GetWriteBuf(nLen
), nLen
) )
811 wxLogSysError(_("Cannot find the location of address book file"));
814 phoneBook
.UngetWriteBuf();
816 // this is the default phone book
817 phoneBook
<< "\\ras\\rasphone.pbk";
821 // TODO may be we should disable auto check while async dialing is in
826 DWORD dwRet
= ms_pfnRasDial
828 NULL
, // no extended features
829 NULL
, // default phone book file (NT only)
831 0, // use callback for notifications
832 async
? (void *)wxRasDialFunc
// cast needed for gcc 3.1
833 : 0, // no notifications, sync operation
839 // can't pass a wxWCharBuffer through ( ... )
840 wxLogError(_("Failed to %s dialup connection: %s"),
841 wxString(async
? _("initiate") : _("establish")).c_str(),
842 GetErrorString(dwRet
).c_str());
844 // we should still call RasHangUp() if we got a non 0 connection
845 if ( ms_hRasConnection
)
847 ms_pfnRasHangUp(ms_hRasConnection
);
848 ms_hRasConnection
= 0;
856 // for async dialing, we're not yet connected
859 ms_isConnected
= TRUE
;
865 bool wxDialUpManagerMSW::IsDialing() const
867 return GetDialer() != NULL
;
870 bool wxDialUpManagerMSW::CancelDialing()
878 wxASSERT_MSG( ms_hRasConnection
, wxT("dialing but no connection?") );
885 bool wxDialUpManagerMSW::HangUp()
887 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
889 // we may terminate either the connection we initiated or another one which
892 if ( ms_hRasConnection
)
894 hRasConn
= ms_hRasConnection
;
896 ms_hRasConnection
= 0;
900 hRasConn
= FindActiveConnection();
905 wxLogError(_("Cannot hang up - no active dialup connection."));
910 DWORD dwRet
= ms_pfnRasHangUp(hRasConn
);
913 wxLogError(_("Failed to terminate the dialup connection: %s"),
914 GetErrorString(dwRet
).c_str());
917 ms_isConnected
= FALSE
;
922 bool wxDialUpManagerMSW::IsAlwaysOnline() const
924 // assume no permanent connection by default
925 bool isAlwaysOnline
= FALSE
;
927 // try to use WinInet functions
929 // NB: we could probably use wxDynamicLibrary here just as well,
930 // but we allow multiple instances of wxDialUpManagerMSW so
931 // we might as well use the ref counted version here too.
933 wxPluginManager
hDll(_T("WININET"));
934 if ( hDll
.IsLoaded() )
936 typedef BOOL (WINAPI
*INTERNETGETCONNECTEDSTATE
)(LPDWORD
, DWORD
);
937 INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState
;
939 #define RESOLVE_FUNCTION(type, name) \
940 pfn##name = (type)hDll.GetSymbol(_T(#name))
942 RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE
, InternetGetConnectedState
);
944 if ( pfnInternetGetConnectedState
)
947 if ( pfnInternetGetConnectedState(&flags
, 0 /* reserved */) )
949 // there is some connection to the net, see of which type
950 isAlwaysOnline
= (flags
& (INTERNET_CONNECTION_LAN
|
951 INTERNET_CONNECTION_PROXY
)) != 0;
953 //else: no Internet connection at all
957 return isAlwaysOnline
;
960 bool wxDialUpManagerMSW::IsOnline() const
962 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
964 if ( ms_userSpecifiedOnlineStatus
!= -1 )
966 // user specified flag overrides our logic
967 return ms_userSpecifiedOnlineStatus
!= 0;
971 // return TRUE if there is at least one active connection
972 return FindActiveConnection() != 0;
976 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline
)
978 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
980 ms_userSpecifiedOnlineStatus
= isOnline
;
983 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds
)
985 wxCHECK_MSG( IsOk(), FALSE
, wxT("using uninitialized wxDialUpManager") );
987 bool ok
= ms_pfnRasConnectionNotification
!= 0;
991 // we're running under NT 4.0, Windows 98 or later and can use
992 // RasConnectionNotification() to be notified by a secondary thread
994 // first, see if we don't have this thread already running
995 if ( m_hThread
!= 0 )
997 DWORD dwSuspendCount
= 2;
998 while ( dwSuspendCount
> 1 )
1000 dwSuspendCount
= ResumeThread(m_hThread
);
1001 if ( dwSuspendCount
== (DWORD
)-1 )
1003 wxLogLastError(wxT("ResumeThread(RasThread)"));
1016 // create all the stuff we need to be notified about RAS connection
1021 // first create an event to wait on
1022 m_data
.hEventRas
= CreateEvent
1024 NULL
, // security attribute (default)
1025 FALSE
, // manual reset (not)
1026 FALSE
, // initial state (not signaled)
1029 if ( !m_data
.hEventRas
)
1031 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1039 // create the event we use to quit the thread
1040 m_data
.hEventQuit
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1041 if ( !m_data
.hEventQuit
)
1043 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1045 CleanUpThreadData();
1051 if ( ok
&& !ms_hwndRas
)
1053 // create a hidden window to receive notification about connections
1055 extern const wxChar
*wxCanvasClassName
;
1056 ms_hwndRas
= ::CreateWindow(wxCanvasClassName
, NULL
,
1059 (HMENU
)NULL
, wxGetInstance(), 0);
1062 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1064 CleanUpThreadData();
1070 FARPROC windowProc
= MakeProcInstance
1072 (FARPROC
)wxRasStatusWindowProc
,
1076 ::SetWindowLong(ms_hwndRas
, GWL_WNDPROC
, (LONG
) windowProc
);
1079 m_data
.hWnd
= ms_hwndRas
;
1083 // start the secondary thread
1084 m_data
.dialUpManager
= this;
1087 m_hThread
= CreateThread
1091 (LPTHREAD_START_ROUTINE
)wxRasMonitorThread
,
1099 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1101 CleanUpThreadData();
1107 // start receiving RAS notifications
1108 DWORD dwRet
= ms_pfnRasConnectionNotification
1110 (HRASCONN
)INVALID_HANDLE_VALUE
,
1112 3 /* RASCN_Connection | RASCN_Disconnection */
1117 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1118 GetErrorString(dwRet
).c_str());
1120 CleanUpThreadData();
1128 // we're running under Windows 95 and have to poll ourselves
1129 // (or, alternatively, the code above for NT/98 failed)
1130 m_timerStatusPolling
.Stop();
1131 if ( nSeconds
== 0 )
1136 m_timerStatusPolling
.Start(nSeconds
* 1000);
1141 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1143 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1147 // we have running secondary thread, it's just enough to suspend it
1148 if ( SuspendThread(m_hThread
) == (DWORD
)-1 )
1150 wxLogLastError(wxT("SuspendThread(RasThread)"));
1155 // even simpler - just stop the timer
1156 m_timerStatusPolling
.Stop();
1160 // ----------------------------------------------------------------------------
1161 // stubs which don't do anything in MSW version
1162 // ----------------------------------------------------------------------------
1164 void wxDialUpManagerMSW::SetWellKnownHost(const wxString
& WXUNUSED(hostname
),
1167 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1169 // nothing to do - we don't use this
1172 void wxDialUpManagerMSW::SetConnectCommand(const wxString
& WXUNUSED(dial
),
1173 const wxString
& WXUNUSED(hangup
))
1175 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1177 // nothing to do - we don't use this
1180 // ----------------------------------------------------------------------------
1182 // ----------------------------------------------------------------------------
1184 static DWORD
wxRasMonitorThread(wxRasThreadData
*data
)
1187 handles
[0] = data
->hEventRas
;
1188 handles
[1] = data
->hEventQuit
;
1193 DWORD dwRet
= WaitForMultipleObjects(2, handles
, FALSE
, INFINITE
);
1198 // RAS connection status changed
1199 SendMessage(data
->hWnd
, wxWM_RAS_STATUS_CHANGED
,
1203 case WAIT_OBJECT_0
+ 1:
1208 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1216 static LRESULT APIENTRY
wxRasStatusWindowProc(HWND hWnd
, UINT message
,
1217 WPARAM wParam
, LPARAM lParam
)
1221 case wxWM_RAS_STATUS_CHANGED
:
1223 wxRasThreadData
*data
= (wxRasThreadData
*)lParam
;
1224 data
->dialUpManager
->OnConnectStatusChange();
1228 case wxWM_RAS_DIALING_PROGRESS
:
1230 wxDialUpManagerMSW
*dialMan
= wxDialUpManagerMSW::GetDialer();
1232 dialMan
->OnDialProgress((RASCONNSTATE
)wParam
, lParam
);
1237 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
1243 static void WINAPI
wxRasDialFunc(UINT unMsg
,
1244 RASCONNSTATE rasconnstate
,
1247 wxDialUpManagerMSW
*dialUpManager
= wxDialUpManagerMSW::GetDialer();
1249 wxCHECK_RET( dialUpManager
, wxT("who started to dial then?") );
1251 SendMessage(dialUpManager
->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS
,
1252 rasconnstate
, dwError
);
1255 #endif // __BORLANDC__
1257 #endif // wxUSE_DIALUP_MANAGER