]>
Commit | Line | Data |
---|---|---|
a0b4c98b | 1 | ///////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/msw/dialup.cpp |
a0b4c98b VZ |
3 | // Purpose: MSW implementation of network/dialup classes and functions |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 07.07.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
65571936 | 9 | // Licence: wxWindows licence |
a0b4c98b VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
a0b4c98b VZ |
27 | #if wxUSE_DIALUP_MANAGER |
28 | ||
670f9935 WS |
29 | #include "wx/dialup.h" |
30 | ||
a0b4c98b VZ |
31 | #ifndef WX_PRECOMP |
32 | #include "wx/log.h" | |
d1f4970d VZ |
33 | #include "wx/intl.h" |
34 | #include "wx/event.h" | |
670f9935 | 35 | #include "wx/app.h" |
c0badb70 | 36 | #include "wx/timer.h" |
02761f6c | 37 | #include "wx/module.h" |
a0b4c98b VZ |
38 | #endif |
39 | ||
fcec6429 VZ |
40 | #include "wx/generic/choicdgg.h" |
41 | ||
142ae7b3 | 42 | #include "wx/msw/private.h" |
b481194f | 43 | #include "wx/msw/private/hiddenwin.h" |
a0b4c98b | 44 | #include "wx/dynlib.h" |
a0b4c98b | 45 | |
9b11752c VZ |
46 | wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED, wxDialUpEvent ); |
47 | wxDEFINE_EVENT( wxEVT_DIALUP_DISCONNECTED, wxDialUpEvent ); | |
2e4df4bf | 48 | |
3bce6687 JS |
49 | // Doesn't yet compile under VC++ 4, BC++, Watcom C++, |
50 | // Wine: no wininet.h | |
3aabb24c | 51 | #if (!defined(__BORLANDC__) || (__BORLANDC__>=0x550)) && \ |
ce20b5d7 | 52 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \ |
7ba4fbeb | 53 | !defined(__GNUWIN32_OLD__) && \ |
5283098e | 54 | !defined(__WINE__) && \ |
7ba4fbeb | 55 | (!defined(__VISUALC__) || (__VISUALC__ >= 1020)) |
106f0395 | 56 | |
a0b4c98b VZ |
57 | #include <ras.h> |
58 | #include <raserror.h> | |
59 | ||
2690830e VZ |
60 | #include <wininet.h> |
61 | ||
a2327a9f JS |
62 | // Not in VC++ 5 |
63 | #ifndef INTERNET_CONNECTION_LAN | |
64 | #define INTERNET_CONNECTION_LAN 2 | |
65 | #endif | |
66 | #ifndef INTERNET_CONNECTION_PROXY | |
67 | #define INTERNET_CONNECTION_PROXY 4 | |
68 | #endif | |
69 | ||
8563c6f1 VZ |
70 | static const wxChar * |
71 | wxMSWDIALUP_WNDCLASSNAME = wxT("_wxDialUpManager_Internal_Class"); | |
72 | static const wxChar *gs_classForDialUpWindow = NULL; | |
73 | ||
a0b4c98b VZ |
74 | // ---------------------------------------------------------------------------- |
75 | // constants | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | // this message is sent by the secondary thread when RAS status changes | |
79 | #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010) | |
2690830e | 80 | #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011) |
a0b4c98b VZ |
81 | |
82 | // ---------------------------------------------------------------------------- | |
83 | // types | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | // the signatures of RAS functions: all this is quite heavy, but we must do it | |
87 | // to allow running wxWin programs on machine which don't have RAS installed | |
88 | // (this does exist) - if we link with rasapi32.lib, the program will fail on | |
89 | // startup because of the missing DLL... | |
90 | ||
91 | #ifndef UNICODE | |
2690830e VZ |
92 | typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCSTR, LPRASDIALPARAMSA, DWORD, LPVOID, LPHRASCONN ); |
93 | typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNA, LPDWORD, LPDWORD ); | |
94 | typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCSTR, LPCSTR, LPRASENTRYNAMEA, LPDWORD, LPDWORD ); | |
95 | typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSA ); | |
96 | typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPSTR, DWORD ); | |
97 | typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN ); | |
98 | typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD ); | |
99 | typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCSTR ); | |
100 | typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCSTR, LPCSTR ); | |
101 | typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, BOOL ); | |
102 | typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, LPBOOL ); | |
103 | typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOA, LPDWORD, LPDWORD ); | |
104 | typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOA, LPDWORD ); | |
105 | typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, LPDWORD, LPBYTE, LPDWORD ); | |
106 | typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, DWORD, LPBYTE, DWORD ); | |
107 | typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCSTR, LPCSTR, LPCSTR ); | |
108 | typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCSTR, LPCSTR ); | |
109 | typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCSTR, LPCSTR ); | |
110 | typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD ); | |
a0b4c98b | 111 | |
9a83f860 | 112 | static const wxChar gs_funcSuffix = wxT('A'); |
a0b4c98b | 113 | #else // Unicode |
2690830e VZ |
114 | typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCWSTR, LPRASDIALPARAMSW, DWORD, LPVOID, LPHRASCONN ); |
115 | typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNW, LPDWORD, LPDWORD ); | |
116 | typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCWSTR, LPCWSTR, LPRASENTRYNAMEW, LPDWORD, LPDWORD ); | |
117 | typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSW ); | |
118 | typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPWSTR, DWORD ); | |
119 | typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN ); | |
120 | typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD ); | |
121 | typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCWSTR ); | |
122 | typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCWSTR, LPCWSTR ); | |
123 | typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, BOOL ); | |
124 | typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, LPBOOL ); | |
125 | typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOW, LPDWORD, LPDWORD ); | |
126 | typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOW, LPDWORD ); | |
127 | typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, LPDWORD, LPBYTE, LPDWORD ); | |
128 | typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, DWORD, LPBYTE, DWORD ); | |
129 | typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCWSTR, LPCWSTR, LPCWSTR ); | |
130 | typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCWSTR, LPCWSTR ); | |
131 | typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCWSTR, LPCWSTR ); | |
132 | typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD ); | |
a0b4c98b | 133 | |
9a83f860 | 134 | static const wxChar gs_funcSuffix = wxT('W'); |
a0b4c98b VZ |
135 | #endif // ASCII/Unicode |
136 | ||
137 | // structure passed to the secondary thread | |
3b415ba4 | 138 | struct WXDLLEXPORT wxRasThreadData |
a0b4c98b VZ |
139 | { |
140 | wxRasThreadData() | |
141 | { | |
142 | hWnd = 0; | |
97247d36 VZ |
143 | hEventRas = |
144 | hEventQuit = 0; | |
a0b4c98b VZ |
145 | dialUpManager = NULL; |
146 | } | |
147 | ||
97247d36 VZ |
148 | ~wxRasThreadData() |
149 | { | |
150 | if ( hWnd ) | |
151 | DestroyWindow(hWnd); | |
152 | ||
153 | if ( hEventQuit ) | |
154 | CloseHandle(hEventQuit); | |
155 | ||
156 | if ( hEventRas ) | |
157 | CloseHandle(hEventRas); | |
158 | } | |
159 | ||
a0b4c98b | 160 | HWND hWnd; // window to send notifications to |
97247d36 VZ |
161 | HANDLE hEventRas, // automatic event which RAS signals when status changes |
162 | hEventQuit; // manual event which we signal when we terminate | |
a0b4c98b | 163 | |
fb1a4789 | 164 | class WXDLLIMPEXP_FWD_CORE wxDialUpManagerMSW *dialUpManager; // the owner |
a0b4c98b VZ |
165 | }; |
166 | ||
167 | // ---------------------------------------------------------------------------- | |
168 | // wxDialUpManager class for MSW | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
171 | class WXDLLEXPORT wxDialUpManagerMSW : public wxDialUpManager | |
172 | { | |
173 | public: | |
174 | // ctor & dtor | |
175 | wxDialUpManagerMSW(); | |
176 | virtual ~wxDialUpManagerMSW(); | |
177 | ||
178 | // implement base class pure virtuals | |
179 | virtual bool IsOk() const; | |
2690830e | 180 | virtual size_t GetISPNames(wxArrayString& names) const; |
a0b4c98b VZ |
181 | virtual bool Dial(const wxString& nameOfISP, |
182 | const wxString& username, | |
183 | const wxString& password, | |
184 | bool async); | |
185 | virtual bool IsDialing() const; | |
186 | virtual bool CancelDialing(); | |
187 | virtual bool HangUp(); | |
2690830e | 188 | virtual bool IsAlwaysOnline() const; |
a0b4c98b | 189 | virtual bool IsOnline() const; |
d71cc120 | 190 | virtual void SetOnlineStatus(bool isOnline = true); |
a0b4c98b VZ |
191 | virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds); |
192 | virtual void DisableAutoCheckOnlineStatus(); | |
193 | virtual void SetWellKnownHost(const wxString& hostname, int port); | |
194 | virtual void SetConnectCommand(const wxString& commandDial, | |
195 | const wxString& commandHangup); | |
196 | ||
197 | // for RasTimer | |
198 | void CheckRasStatus(); | |
199 | ||
200 | // for wxRasStatusWindowProc | |
201 | void OnConnectStatusChange(); | |
2690830e | 202 | void OnDialProgress(RASCONNSTATE rasconnstate, DWORD dwError); |
a0b4c98b VZ |
203 | |
204 | // for wxRasDialFunc | |
2690830e | 205 | static HWND GetRasWindow() { return ms_hwndRas; } |
8563c6f1 | 206 | static void ResetRasWindow() { ms_hwndRas = NULL; } |
a0b4c98b VZ |
207 | static wxDialUpManagerMSW *GetDialer() { return ms_dialer; } |
208 | ||
209 | private: | |
210 | // return the error string for the given RAS error code | |
211 | static wxString GetErrorString(DWORD error); | |
212 | ||
213 | // find the (first) handle of the active connection | |
214 | static HRASCONN FindActiveConnection(); | |
215 | ||
216 | // notify the application about status change | |
d71cc120 | 217 | void NotifyApp(bool connected, bool fromOurselves = false) const; |
a0b4c98b VZ |
218 | |
219 | // destroy the thread data and the thread itself | |
220 | void CleanUpThreadData(); | |
221 | ||
6bba111c VZ |
222 | // number of times EnableAutoCheckOnlineStatus() had been called minus the |
223 | // number of times DisableAutoCheckOnlineStatus() had been called | |
224 | int m_autoCheckLevel; | |
225 | ||
a0b4c98b | 226 | // timer used for polling RAS status |
3b415ba4 | 227 | class WXDLLEXPORT RasTimer : public wxTimer |
a0b4c98b VZ |
228 | { |
229 | public: | |
230 | RasTimer(wxDialUpManagerMSW *dialUpManager) | |
231 | { m_dialUpManager = dialUpManager; } | |
232 | ||
233 | virtual void Notify() { m_dialUpManager->CheckRasStatus(); } | |
234 | ||
235 | private: | |
236 | wxDialUpManagerMSW *m_dialUpManager; | |
2eb10e2a | 237 | |
c0c133e1 | 238 | wxDECLARE_NO_COPY_CLASS(RasTimer); |
a0b4c98b VZ |
239 | } m_timerStatusPolling; |
240 | ||
241 | // thread handle for the thread sitting on connection change event | |
242 | HANDLE m_hThread; | |
243 | ||
244 | // data used by this thread and our hidden window to send messages between | |
245 | // each other | |
97247d36 | 246 | wxRasThreadData *m_data; |
a0b4c98b | 247 | |
4f89dbc4 | 248 | // the handle of rasapi32.dll when it's loaded |
4522bb3e | 249 | wxDynamicLibrary m_dllRas; |
4f89dbc4 | 250 | |
2690830e VZ |
251 | // the hidden window we use for passing messages between threads |
252 | static HWND ms_hwndRas; | |
253 | ||
a0b4c98b VZ |
254 | // the handle of the connection we initiated or 0 if none |
255 | static HRASCONN ms_hRasConnection; | |
256 | ||
a0b4c98b VZ |
257 | // the pointers to RAS functions |
258 | static RASDIAL ms_pfnRasDial; | |
259 | static RASENUMCONNECTIONS ms_pfnRasEnumConnections; | |
260 | static RASENUMENTRIES ms_pfnRasEnumEntries; | |
261 | static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus; | |
262 | static RASGETERRORSTRING ms_pfnRasGetErrorString; | |
263 | static RASHANGUP ms_pfnRasHangUp; | |
264 | static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo; | |
265 | static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry; | |
266 | static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry; | |
267 | static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams; | |
268 | static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams; | |
269 | static RASENUMDEVICES ms_pfnRasEnumDevices; | |
270 | static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo; | |
271 | static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties; | |
272 | static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties; | |
273 | static RASRENAMEENTRY ms_pfnRasRenameEntry; | |
274 | static RASDELETEENTRY ms_pfnRasDeleteEntry; | |
275 | static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName; | |
276 | ||
277 | // this function is not supported by Win95 | |
278 | static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification; | |
279 | ||
280 | // if this flag is different from -1, it overrides IsOnline() | |
281 | static int ms_userSpecifiedOnlineStatus; | |
282 | ||
283 | // this flag tells us if we're online | |
284 | static int ms_isConnected; | |
285 | ||
286 | // this flag tells us whether a call to RasDial() is in progress | |
287 | static wxDialUpManagerMSW *ms_dialer; | |
2eb10e2a | 288 | |
c0c133e1 | 289 | wxDECLARE_NO_COPY_CLASS(wxDialUpManagerMSW); |
a0b4c98b VZ |
290 | }; |
291 | ||
8563c6f1 VZ |
292 | // module to destroy helper window created by wxDialUpManagerMSW |
293 | class wxDialUpManagerModule : public wxModule | |
294 | { | |
295 | public: | |
296 | bool OnInit() { return true; } | |
297 | void OnExit() | |
298 | { | |
299 | HWND hwnd = wxDialUpManagerMSW::GetRasWindow(); | |
300 | if ( hwnd ) | |
301 | { | |
302 | ::DestroyWindow(hwnd); | |
303 | wxDialUpManagerMSW::ResetRasWindow(); | |
304 | } | |
305 | ||
306 | if ( gs_classForDialUpWindow ) | |
307 | { | |
308 | ::UnregisterClass(wxMSWDIALUP_WNDCLASSNAME, wxGetInstance()); | |
309 | gs_classForDialUpWindow = NULL; | |
310 | } | |
311 | } | |
312 | ||
313 | private: | |
314 | DECLARE_DYNAMIC_CLASS(wxDialUpManagerModule) | |
315 | }; | |
316 | ||
317 | IMPLEMENT_DYNAMIC_CLASS(wxDialUpManagerModule, wxModule) | |
318 | ||
a0b4c98b VZ |
319 | // ---------------------------------------------------------------------------- |
320 | // private functions | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
323 | static LRESULT WINAPI wxRasStatusWindowProc(HWND hWnd, UINT message, | |
324 | WPARAM wParam, LPARAM lParam); | |
325 | ||
326 | static DWORD wxRasMonitorThread(wxRasThreadData *data); | |
327 | ||
328 | static void WINAPI wxRasDialFunc(UINT unMsg, | |
329 | RASCONNSTATE rasconnstate, | |
330 | DWORD dwError); | |
331 | ||
332 | // ============================================================================ | |
333 | // implementation | |
334 | // ============================================================================ | |
335 | ||
336 | // ---------------------------------------------------------------------------- | |
337 | // init the static variables | |
338 | // ---------------------------------------------------------------------------- | |
339 | ||
340 | HRASCONN wxDialUpManagerMSW::ms_hRasConnection = 0; | |
341 | ||
2690830e VZ |
342 | HWND wxDialUpManagerMSW::ms_hwndRas = 0; |
343 | ||
a0b4c98b VZ |
344 | RASDIAL wxDialUpManagerMSW::ms_pfnRasDial = 0; |
345 | RASENUMCONNECTIONS wxDialUpManagerMSW::ms_pfnRasEnumConnections = 0; | |
346 | RASENUMENTRIES wxDialUpManagerMSW::ms_pfnRasEnumEntries = 0; | |
347 | RASGETCONNECTSTATUS wxDialUpManagerMSW::ms_pfnRasGetConnectStatus = 0; | |
348 | RASGETERRORSTRING wxDialUpManagerMSW::ms_pfnRasGetErrorString = 0; | |
349 | RASHANGUP wxDialUpManagerMSW::ms_pfnRasHangUp = 0; | |
350 | RASGETPROJECTIONINFO wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo = 0; | |
351 | RASCREATEPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry = 0; | |
352 | RASEDITPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry = 0; | |
353 | RASSETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams = 0; | |
354 | RASGETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams = 0; | |
355 | RASENUMDEVICES wxDialUpManagerMSW::ms_pfnRasEnumDevices = 0; | |
356 | RASGETCOUNTRYINFO wxDialUpManagerMSW::ms_pfnRasGetCountryInfo = 0; | |
357 | RASGETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasGetEntryProperties = 0; | |
358 | RASSETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasSetEntryProperties = 0; | |
359 | RASRENAMEENTRY wxDialUpManagerMSW::ms_pfnRasRenameEntry = 0; | |
360 | RASDELETEENTRY wxDialUpManagerMSW::ms_pfnRasDeleteEntry = 0; | |
361 | RASVALIDATEENTRYNAME wxDialUpManagerMSW::ms_pfnRasValidateEntryName = 0; | |
362 | RASCONNECTIONNOTIFICATION wxDialUpManagerMSW::ms_pfnRasConnectionNotification = 0; | |
363 | ||
364 | int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus = -1; | |
365 | int wxDialUpManagerMSW::ms_isConnected = -1; | |
366 | wxDialUpManagerMSW *wxDialUpManagerMSW::ms_dialer = NULL; | |
367 | ||
368 | // ---------------------------------------------------------------------------- | |
369 | // ctor and dtor: the dynamic linking happens here | |
370 | // ---------------------------------------------------------------------------- | |
371 | ||
372 | // the static creator function is implemented here | |
373 | wxDialUpManager *wxDialUpManager::Create() | |
374 | { | |
375 | return new wxDialUpManagerMSW; | |
376 | } | |
377 | ||
378 | #ifdef __VISUALC__ | |
379 | // warning about "'this' : used in base member initializer list" - so what? | |
380 | #pragma warning(disable:4355) | |
381 | #endif // VC++ | |
382 | ||
383 | wxDialUpManagerMSW::wxDialUpManagerMSW() | |
4522bb3e | 384 | : m_timerStatusPolling(this), |
9a83f860 | 385 | m_dllRas(wxT("RASAPI32")) |
a0b4c98b VZ |
386 | { |
387 | // initialize our data | |
6bba111c | 388 | m_autoCheckLevel = 0; |
a0b4c98b | 389 | m_hThread = 0; |
97247d36 | 390 | m_data = new wxRasThreadData; |
a0b4c98b | 391 | |
4f89dbc4 RL |
392 | if ( !m_dllRas.IsLoaded() ) |
393 | { | |
394 | wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it.")); | |
395 | } | |
1c25d245 | 396 | else if ( !ms_pfnRasDial ) |
a0b4c98b | 397 | { |
4f89dbc4 RL |
398 | // resolve the functions we need |
399 | ||
400 | // this will contain the name of the function we failed to resolve | |
401 | // if any at the end | |
402 | const char *funcName = NULL; | |
403 | ||
404 | // get the function from rasapi32.dll and abort if it's not found | |
405 | #define RESOLVE_RAS_FUNCTION(type, name) \ | |
9a83f860 | 406 | ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(wxT(#name)) \ |
4f89dbc4 RL |
407 | + gs_funcSuffix); \ |
408 | if ( !ms_pfn##name ) \ | |
409 | { \ | |
410 | funcName = #name; \ | |
411 | goto exit; \ | |
412 | } | |
413 | ||
414 | // a variant of above macro which doesn't abort if the function is | |
415 | // not found in the DLL | |
416 | #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \ | |
9a83f860 | 417 | ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(wxT(#name)) \ |
4f89dbc4 RL |
418 | + gs_funcSuffix); |
419 | ||
420 | RESOLVE_RAS_FUNCTION(RASDIAL, RasDial); | |
421 | RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS, RasEnumConnections); | |
422 | RESOLVE_RAS_FUNCTION(RASENUMENTRIES, RasEnumEntries); | |
423 | RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS, RasGetConnectStatus); | |
424 | RESOLVE_RAS_FUNCTION(RASGETERRORSTRING, RasGetErrorString); | |
425 | RESOLVE_RAS_FUNCTION(RASHANGUP, RasHangUp); | |
426 | RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS, RasGetEntryDialParams); | |
427 | ||
428 | // suppress error messages about missing (non essential) functions | |
a0b4c98b | 429 | { |
4f89dbc4 RL |
430 | wxLogNull noLog; |
431 | ||
432 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO, RasGetProjectionInfo); | |
433 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY, RasCreatePhonebookEntry); | |
434 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY, RasEditPhonebookEntry); | |
435 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS, RasSetEntryDialParams); | |
436 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES, RasGetEntryProperties); | |
437 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES, RasSetEntryProperties); | |
438 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY, RasRenameEntry); | |
439 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY, RasDeleteEntry); | |
440 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME, RasValidateEntryName); | |
441 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO, RasGetCountryInfo); | |
442 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES, RasEnumDevices); | |
443 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION, RasConnectionNotification); | |
a0b4c98b | 444 | } |
a0b4c98b | 445 | |
4f89dbc4 RL |
446 | // keep your preprocessor name space clean |
447 | #undef RESOLVE_RAS_FUNCTION | |
448 | #undef RESOLVE_OPTIONAL_RAS_FUNCTION | |
a0b4c98b VZ |
449 | |
450 | exit: | |
4f89dbc4 RL |
451 | if ( funcName ) |
452 | { | |
e6d4038a VZ |
453 | wxLogError(_("The version of remote access service (RAS) installed " |
454 | "on this machine is too old, please upgrade (the " | |
455 | "following required function is missing: %s)."), | |
456 | funcName); | |
4f89dbc4 RL |
457 | m_dllRas.Unload(); |
458 | return; | |
a0b4c98b VZ |
459 | } |
460 | } | |
461 | ||
462 | // enable auto check by default | |
463 | EnableAutoCheckOnlineStatus(0); | |
464 | } | |
465 | ||
466 | wxDialUpManagerMSW::~wxDialUpManagerMSW() | |
467 | { | |
468 | CleanUpThreadData(); | |
a0b4c98b VZ |
469 | } |
470 | ||
471 | // ---------------------------------------------------------------------------- | |
472 | // helper functions | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | wxString wxDialUpManagerMSW::GetErrorString(DWORD error) | |
476 | { | |
477 | wxChar buffer[512]; // this should be more than enough according to MS docs | |
478 | DWORD dwRet = ms_pfnRasGetErrorString(error, buffer, WXSIZEOF(buffer)); | |
479 | switch ( dwRet ) | |
480 | { | |
481 | case ERROR_INVALID_PARAMETER: | |
482 | // this was a standard Win32 error probably | |
483 | return wxString(wxSysErrorMsg(error)); | |
484 | ||
485 | default: | |
486 | { | |
f6bcfd97 BP |
487 | wxLogSysError(dwRet, |
488 | _("Failed to retrieve text of RAS error message")); | |
a0b4c98b VZ |
489 | |
490 | wxString msg; | |
491 | msg.Printf(_("unknown error (error code %08x)."), error); | |
492 | return msg; | |
493 | } | |
494 | ||
495 | case 0: | |
496 | // we want the error message to start from a lower case letter | |
907173e5 | 497 | buffer[0] = (wxChar)wxTolower(buffer[0]); |
a0b4c98b VZ |
498 | |
499 | return wxString(buffer); | |
500 | } | |
501 | } | |
502 | ||
503 | HRASCONN wxDialUpManagerMSW::FindActiveConnection() | |
504 | { | |
505 | // enumerate connections | |
506 | DWORD cbBuf = sizeof(RASCONN); | |
507 | LPRASCONN lpRasConn = (LPRASCONN)malloc(cbBuf); | |
508 | if ( !lpRasConn ) | |
509 | { | |
510 | // out of memory | |
511 | return 0; | |
512 | } | |
513 | ||
514 | lpRasConn->dwSize = sizeof(RASCONN); | |
515 | ||
516 | DWORD nConnections = 0; | |
517 | DWORD dwRet = ERROR_BUFFER_TOO_SMALL; | |
518 | ||
519 | while ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
520 | { | |
521 | dwRet = ms_pfnRasEnumConnections(lpRasConn, &cbBuf, &nConnections); | |
522 | ||
523 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
524 | { | |
525 | LPRASCONN lpRasConnOld = lpRasConn; | |
526 | lpRasConn = (LPRASCONN)realloc(lpRasConn, cbBuf); | |
527 | if ( !lpRasConn ) | |
528 | { | |
529 | // out of memory | |
530 | free(lpRasConnOld); | |
531 | ||
532 | return 0; | |
533 | } | |
534 | } | |
535 | else if ( dwRet == 0 ) | |
536 | { | |
537 | // ok, success | |
538 | break; | |
539 | } | |
540 | else | |
541 | { | |
3103e8a9 | 542 | // an error occurred |
a0b4c98b | 543 | wxLogError(_("Cannot find active dialup connection: %s"), |
5b09e9c2 | 544 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
545 | return 0; |
546 | } | |
547 | } | |
548 | ||
549 | HRASCONN hrasconn; | |
550 | ||
551 | switch ( nConnections ) | |
552 | { | |
553 | case 0: | |
554 | // no connections | |
555 | hrasconn = 0; | |
556 | break; | |
557 | ||
558 | default: | |
559 | // more than 1 connection - we don't know what to do with this | |
560 | // case, so give a warning but continue (taking the first | |
561 | // connection) - the warning is really needed because this function | |
562 | // is used, for example, to select the connection to hang up and so | |
563 | // we may hang up the wrong connection here... | |
f6bcfd97 | 564 | wxLogWarning(_("Several active dialup connections found, choosing one randomly.")); |
a0b4c98b VZ |
565 | // fall through |
566 | ||
567 | case 1: | |
568 | // exactly 1 connection, great | |
569 | hrasconn = lpRasConn->hrasconn; | |
570 | } | |
571 | ||
572 | free(lpRasConn); | |
573 | ||
574 | return hrasconn; | |
575 | } | |
576 | ||
577 | void wxDialUpManagerMSW::CleanUpThreadData() | |
578 | { | |
579 | if ( m_hThread ) | |
580 | { | |
97247d36 | 581 | if ( !SetEvent(m_data->hEventQuit) ) |
a0b4c98b | 582 | { |
9a83f860 | 583 | wxLogLastError(wxT("SetEvent(RasThreadQuit)")); |
a0b4c98b | 584 | } |
97247d36 VZ |
585 | else // sent quit request to the background thread |
586 | { | |
587 | // the thread still needs m_data so we can't free it here, rather | |
588 | // let the thread do it itself | |
589 | m_data = NULL; | |
590 | } | |
a0b4c98b VZ |
591 | |
592 | CloseHandle(m_hThread); | |
593 | ||
594 | m_hThread = 0; | |
595 | } | |
596 | ||
5276b0a5 | 597 | wxDELETE(m_data); |
a0b4c98b VZ |
598 | } |
599 | ||
600 | // ---------------------------------------------------------------------------- | |
601 | // connection status | |
602 | // ---------------------------------------------------------------------------- | |
603 | ||
604 | void wxDialUpManagerMSW::CheckRasStatus() | |
605 | { | |
606 | // use int, not bool to compare with -1 | |
607 | int isConnected = FindActiveConnection() != 0; | |
608 | if ( isConnected != ms_isConnected ) | |
609 | { | |
610 | if ( ms_isConnected != -1 ) | |
611 | { | |
612 | // notify the program | |
613 | NotifyApp(isConnected != 0); | |
614 | } | |
615 | // else: it's the first time we're called, just update the flag | |
616 | ||
617 | ms_isConnected = isConnected; | |
618 | } | |
619 | } | |
620 | ||
621 | void wxDialUpManagerMSW::NotifyApp(bool connected, bool fromOurselves) const | |
622 | { | |
623 | wxDialUpEvent event(connected, fromOurselves); | |
624 | (void)wxTheApp->ProcessEvent(event); | |
625 | } | |
626 | ||
627 | // this function is called whenever the status of any RAS connection on this | |
628 | // machine changes by RAS itself | |
629 | void wxDialUpManagerMSW::OnConnectStatusChange() | |
630 | { | |
631 | // we know that status changed, but we don't know whether we're connected | |
632 | // or not - so find it out | |
633 | CheckRasStatus(); | |
634 | } | |
635 | ||
636 | // this function is called by our callback which we give to RasDial() when | |
637 | // calling it asynchronously | |
638 | void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate, | |
639 | DWORD dwError) | |
640 | { | |
641 | if ( !GetDialer() ) | |
642 | { | |
643 | // this probably means that CancelDialing() was called and we get | |
644 | // "disconnected" notification | |
645 | return; | |
646 | } | |
647 | ||
648 | // we're only interested in 2 events: connected and disconnected | |
649 | if ( dwError ) | |
650 | { | |
651 | wxLogError(_("Failed to establish dialup connection: %s"), | |
5b09e9c2 | 652 | GetErrorString(dwError).c_str()); |
a0b4c98b VZ |
653 | |
654 | // we should still call RasHangUp() if we got a non 0 connection | |
655 | if ( ms_hRasConnection ) | |
656 | { | |
657 | ms_pfnRasHangUp(ms_hRasConnection); | |
658 | ms_hRasConnection = 0; | |
659 | } | |
660 | ||
661 | ms_dialer = NULL; | |
662 | ||
d71cc120 | 663 | NotifyApp(false /* !connected */, true /* we dialed ourselves */); |
a0b4c98b VZ |
664 | } |
665 | else if ( rasconnstate == RASCS_Connected ) | |
666 | { | |
d71cc120 | 667 | ms_isConnected = true; |
a0b4c98b VZ |
668 | ms_dialer = NULL; |
669 | ||
d71cc120 | 670 | NotifyApp(true /* connected */, true /* we dialed ourselves */); |
a0b4c98b VZ |
671 | } |
672 | } | |
673 | ||
674 | // ---------------------------------------------------------------------------- | |
675 | // implementation of wxDialUpManager functions | |
676 | // ---------------------------------------------------------------------------- | |
677 | ||
678 | bool wxDialUpManagerMSW::IsOk() const | |
679 | { | |
4f89dbc4 | 680 | return m_dllRas.IsLoaded(); |
a0b4c98b VZ |
681 | } |
682 | ||
2690830e VZ |
683 | size_t wxDialUpManagerMSW::GetISPNames(wxArrayString& names) const |
684 | { | |
685 | // fetch the entries | |
686 | DWORD size = sizeof(RASENTRYNAME); | |
687 | RASENTRYNAME *rasEntries = (RASENTRYNAME *)malloc(size); | |
688 | rasEntries->dwSize = sizeof(RASENTRYNAME); | |
689 | ||
690 | DWORD nEntries; | |
691 | DWORD dwRet; | |
692 | do | |
693 | { | |
694 | dwRet = ms_pfnRasEnumEntries | |
695 | ( | |
696 | NULL, // reserved | |
697 | NULL, // default phone book (or all) | |
698 | rasEntries, // [out] buffer for the entries | |
699 | &size, // [in/out] size of the buffer | |
700 | &nEntries // [out] number of entries fetched | |
701 | ); | |
702 | ||
703 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
704 | { | |
705 | // reallocate the buffer | |
fb9bea06 JS |
706 | void *n = realloc(rasEntries, size); |
707 | if (n == NULL) | |
708 | { | |
709 | free(rasEntries); | |
710 | return 0; | |
711 | } | |
712 | rasEntries = (RASENTRYNAME *)n; | |
2690830e VZ |
713 | } |
714 | else if ( dwRet != 0 ) | |
715 | { | |
716 | // some other error - abort | |
5b09e9c2 VZ |
717 | wxLogError(_("Failed to get ISP names: %s"), |
718 | GetErrorString(dwRet).c_str()); | |
2690830e VZ |
719 | |
720 | free(rasEntries); | |
721 | ||
722 | return 0u; | |
723 | } | |
724 | } | |
725 | while ( dwRet != 0 ); | |
726 | ||
727 | // process them | |
728 | names.Empty(); | |
729 | for ( size_t n = 0; n < (size_t)nEntries; n++ ) | |
730 | { | |
731 | names.Add(rasEntries[n].szEntryName); | |
732 | } | |
733 | ||
734 | free(rasEntries); | |
735 | ||
736 | // return the number of entries | |
737 | return names.GetCount(); | |
738 | } | |
739 | ||
a0b4c98b VZ |
740 | bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP, |
741 | const wxString& username, | |
742 | const wxString& password, | |
743 | bool async) | |
744 | { | |
2690830e | 745 | // check preconditions |
d71cc120 | 746 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
747 | |
748 | if ( ms_hRasConnection ) | |
749 | { | |
223d09f6 | 750 | wxFAIL_MSG(wxT("there is already an active connection")); |
a0b4c98b | 751 | |
d71cc120 | 752 | return true; |
a0b4c98b VZ |
753 | } |
754 | ||
2690830e VZ |
755 | // get the default ISP if none given |
756 | wxString entryName(nameOfISP); | |
757 | if ( !entryName ) | |
758 | { | |
759 | wxArrayString names; | |
760 | size_t count = GetISPNames(names); | |
761 | switch ( count ) | |
762 | { | |
763 | case 0: | |
764 | // no known ISPs, abort | |
765 | wxLogError(_("Failed to connect: no ISP to dial.")); | |
766 | ||
d71cc120 | 767 | return false; |
2690830e VZ |
768 | |
769 | case 1: | |
770 | // only one ISP, choose it | |
771 | entryName = names[0u]; | |
772 | break; | |
773 | ||
774 | default: | |
775 | // several ISPs, let the user choose | |
776 | { | |
777 | wxString *strings = new wxString[count]; | |
778 | for ( size_t i = 0; i < count; i++ ) | |
779 | { | |
780 | strings[i] = names[i]; | |
781 | } | |
782 | ||
783 | entryName = wxGetSingleChoice | |
784 | ( | |
785 | _("Choose ISP to dial"), | |
f6bcfd97 | 786 | _("Please choose which ISP do you want to connect to"), |
2690830e VZ |
787 | count, |
788 | strings | |
789 | ); | |
790 | ||
791 | delete [] strings; | |
792 | ||
793 | if ( !entryName ) | |
794 | { | |
795 | // cancelled by user | |
d71cc120 | 796 | return false; |
2690830e VZ |
797 | } |
798 | } | |
799 | } | |
800 | } | |
801 | ||
a0b4c98b VZ |
802 | RASDIALPARAMS rasDialParams; |
803 | rasDialParams.dwSize = sizeof(rasDialParams); | |
64accea5 | 804 | wxStrlcpy(rasDialParams.szEntryName, entryName.c_str(), RAS_MaxEntryName); |
2690830e VZ |
805 | |
806 | // do we have the username and password? | |
807 | if ( !username || !password ) | |
808 | { | |
809 | BOOL gotPassword; | |
810 | DWORD dwRet = ms_pfnRasGetEntryDialParams | |
811 | ( | |
812 | NULL, // default phonebook | |
813 | &rasDialParams, // [in/out] the params of this entry | |
814 | &gotPassword // [out] did we get password? | |
815 | ); | |
816 | ||
817 | if ( dwRet != 0 ) | |
818 | { | |
819 | wxLogError(_("Failed to connect: missing username/password.")); | |
820 | ||
d71cc120 | 821 | return false; |
2690830e VZ |
822 | } |
823 | } | |
4f89dbc4 RL |
824 | else |
825 | { | |
64accea5 VZ |
826 | wxStrlcpy(rasDialParams.szUserName, username.c_str(), UNLEN); |
827 | wxStrlcpy(rasDialParams.szPassword, password.c_str(), PWLEN); | |
4f89dbc4 | 828 | } |
2690830e | 829 | |
4f89dbc4 | 830 | // default values for other fields |
a0b4c98b VZ |
831 | rasDialParams.szPhoneNumber[0] = '\0'; |
832 | rasDialParams.szCallbackNumber[0] = '\0'; | |
833 | rasDialParams.szCallbackNumber[0] = '\0'; | |
834 | ||
a0b4c98b VZ |
835 | rasDialParams.szDomain[0] = '*'; |
836 | rasDialParams.szDomain[1] = '\0'; | |
837 | ||
838 | // apparently, this is not really necessary - passing NULL instead of the | |
839 | // phone book has the same effect | |
840 | #if 0 | |
841 | wxString phoneBook; | |
842 | if ( wxGetOsVersion() == wxWINDOWS_NT ) | |
843 | { | |
844 | // first get the length | |
845 | UINT nLen = ::GetSystemDirectory(NULL, 0); | |
846 | nLen++; | |
847 | ||
848 | if ( !::GetSystemDirectory(phoneBook.GetWriteBuf(nLen), nLen) ) | |
849 | { | |
850 | wxLogSysError(_("Cannot find the location of address book file")); | |
851 | } | |
852 | ||
853 | phoneBook.UngetWriteBuf(); | |
854 | ||
855 | // this is the default phone book | |
856 | phoneBook << "\\ras\\rasphone.pbk"; | |
857 | } | |
858 | #endif // 0 | |
859 | ||
860 | // TODO may be we should disable auto check while async dialing is in | |
861 | // progress? | |
862 | ||
863 | ms_dialer = this; | |
864 | ||
865 | DWORD dwRet = ms_pfnRasDial | |
866 | ( | |
1c25d245 VZ |
867 | NULL, // no extended features |
868 | NULL, // default phone book file (NT only) | |
a0b4c98b | 869 | &rasDialParams, |
1c25d245 VZ |
870 | 0, // use callback for notifications |
871 | async ? (void *)wxRasDialFunc // cast needed for gcc 3.1 | |
872 | : 0, // no notifications, sync operation | |
a0b4c98b VZ |
873 | &ms_hRasConnection |
874 | ); | |
875 | ||
876 | if ( dwRet != 0 ) | |
877 | { | |
bf9b6266 | 878 | // can't pass a wxWCharBuffer through ( ... ) |
32efa676 | 879 | if ( async ) |
43b2d5e7 | 880 | { |
32efa676 VS |
881 | wxLogError(_("Failed to initiate dialup connection: %s"), |
882 | GetErrorString(dwRet).c_str()); | |
43b2d5e7 | 883 | } |
32efa676 | 884 | else |
43b2d5e7 | 885 | { |
32efa676 VS |
886 | wxLogError(_("Failed to establish dialup connection: %s"), |
887 | GetErrorString(dwRet).c_str()); | |
43b2d5e7 | 888 | } |
a0b4c98b VZ |
889 | |
890 | // we should still call RasHangUp() if we got a non 0 connection | |
891 | if ( ms_hRasConnection ) | |
892 | { | |
893 | ms_pfnRasHangUp(ms_hRasConnection); | |
894 | ms_hRasConnection = 0; | |
895 | } | |
896 | ||
897 | ms_dialer = NULL; | |
898 | ||
d71cc120 | 899 | return false; |
a0b4c98b VZ |
900 | } |
901 | ||
902 | // for async dialing, we're not yet connected | |
903 | if ( !async ) | |
904 | { | |
d71cc120 | 905 | ms_isConnected = true; |
a0b4c98b VZ |
906 | } |
907 | ||
d71cc120 | 908 | return true; |
a0b4c98b VZ |
909 | } |
910 | ||
911 | bool wxDialUpManagerMSW::IsDialing() const | |
912 | { | |
913 | return GetDialer() != NULL; | |
914 | } | |
915 | ||
916 | bool wxDialUpManagerMSW::CancelDialing() | |
917 | { | |
918 | if ( !GetDialer() ) | |
919 | { | |
920 | // silently ignore | |
d71cc120 | 921 | return false; |
a0b4c98b VZ |
922 | } |
923 | ||
223d09f6 | 924 | wxASSERT_MSG( ms_hRasConnection, wxT("dialing but no connection?") ); |
a0b4c98b VZ |
925 | |
926 | ms_dialer = NULL; | |
927 | ||
928 | return HangUp(); | |
929 | } | |
930 | ||
931 | bool wxDialUpManagerMSW::HangUp() | |
932 | { | |
d71cc120 | 933 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
934 | |
935 | // we may terminate either the connection we initiated or another one which | |
936 | // is active now | |
937 | HRASCONN hRasConn; | |
938 | if ( ms_hRasConnection ) | |
939 | { | |
940 | hRasConn = ms_hRasConnection; | |
941 | ||
942 | ms_hRasConnection = 0; | |
943 | } | |
944 | else | |
945 | { | |
946 | hRasConn = FindActiveConnection(); | |
947 | } | |
948 | ||
949 | if ( !hRasConn ) | |
950 | { | |
951 | wxLogError(_("Cannot hang up - no active dialup connection.")); | |
952 | ||
d71cc120 | 953 | return false; |
a0b4c98b VZ |
954 | } |
955 | ||
fbd3c46d VZ |
956 | // note that it's not an error if the connection had been already |
957 | // terminated | |
958 | const DWORD dwRet = ms_pfnRasHangUp(hRasConn); | |
959 | if ( dwRet != 0 && dwRet != ERROR_NO_CONNECTION ) | |
a0b4c98b VZ |
960 | { |
961 | wxLogError(_("Failed to terminate the dialup connection: %s"), | |
5b09e9c2 | 962 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
963 | } |
964 | ||
d71cc120 | 965 | ms_isConnected = false; |
a0b4c98b | 966 | |
d71cc120 | 967 | return true; |
a0b4c98b VZ |
968 | } |
969 | ||
2690830e VZ |
970 | bool wxDialUpManagerMSW::IsAlwaysOnline() const |
971 | { | |
19caf229 | 972 | // assume no permanent connection by default |
d71cc120 | 973 | bool isAlwaysOnline = false; |
2690830e | 974 | |
19caf229 | 975 | // try to use WinInet functions |
4f89dbc4 RL |
976 | |
977 | // NB: we could probably use wxDynamicLibrary here just as well, | |
978 | // but we allow multiple instances of wxDialUpManagerMSW so | |
979 | // we might as well use the ref counted version here too. | |
980 | ||
9a83f860 | 981 | wxDynamicLibrary hDll(wxT("WININET")); |
4f89dbc4 | 982 | if ( hDll.IsLoaded() ) |
2690830e | 983 | { |
5a56be28 | 984 | typedef BOOL (WINAPI *INTERNETGETCONNECTEDSTATE)(LPDWORD, DWORD); |
2690830e VZ |
985 | INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState; |
986 | ||
987 | #define RESOLVE_FUNCTION(type, name) \ | |
9a83f860 | 988 | pfn##name = (type)hDll.GetSymbol(wxT(#name)) |
2690830e VZ |
989 | |
990 | RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE, InternetGetConnectedState); | |
991 | ||
992 | if ( pfnInternetGetConnectedState ) | |
993 | { | |
994 | DWORD flags = 0; | |
995 | if ( pfnInternetGetConnectedState(&flags, 0 /* reserved */) ) | |
996 | { | |
997 | // there is some connection to the net, see of which type | |
19caf229 VZ |
998 | isAlwaysOnline = (flags & (INTERNET_CONNECTION_LAN | |
999 | INTERNET_CONNECTION_PROXY)) != 0; | |
2690830e | 1000 | } |
19caf229 | 1001 | //else: no Internet connection at all |
2690830e VZ |
1002 | } |
1003 | } | |
1004 | ||
19caf229 | 1005 | return isAlwaysOnline; |
2690830e VZ |
1006 | } |
1007 | ||
a0b4c98b VZ |
1008 | bool wxDialUpManagerMSW::IsOnline() const |
1009 | { | |
d71cc120 | 1010 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b | 1011 | |
90e9494a VZ |
1012 | if ( IsAlwaysOnline() ) |
1013 | { | |
1014 | // always => now | |
1015 | return true; | |
1016 | } | |
1017 | ||
a0b4c98b VZ |
1018 | if ( ms_userSpecifiedOnlineStatus != -1 ) |
1019 | { | |
1020 | // user specified flag overrides our logic | |
1021 | return ms_userSpecifiedOnlineStatus != 0; | |
1022 | } | |
1023 | else | |
1024 | { | |
d71cc120 | 1025 | // return true if there is at least one active connection |
a0b4c98b VZ |
1026 | return FindActiveConnection() != 0; |
1027 | } | |
1028 | } | |
1029 | ||
1030 | void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline) | |
1031 | { | |
223d09f6 | 1032 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1033 | |
1034 | ms_userSpecifiedOnlineStatus = isOnline; | |
1035 | } | |
1036 | ||
1037 | bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds) | |
1038 | { | |
d71cc120 | 1039 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b | 1040 | |
6bba111c VZ |
1041 | if ( m_autoCheckLevel++ ) |
1042 | { | |
1043 | // already checking | |
d71cc120 | 1044 | return true; |
6bba111c VZ |
1045 | } |
1046 | ||
a0b4c98b VZ |
1047 | bool ok = ms_pfnRasConnectionNotification != 0; |
1048 | ||
1049 | if ( ok ) | |
1050 | { | |
1051 | // we're running under NT 4.0, Windows 98 or later and can use | |
1052 | // RasConnectionNotification() to be notified by a secondary thread | |
1053 | ||
1054 | // first, see if we don't have this thread already running | |
1055 | if ( m_hThread != 0 ) | |
1056 | { | |
6bba111c | 1057 | if ( ::ResumeThread(m_hThread) != (DWORD)-1 ) |
d71cc120 | 1058 | return true; |
a0b4c98b | 1059 | |
6bba111c VZ |
1060 | // we're leaving a zombie thread... but what else can we do? |
1061 | wxLogLastError(wxT("ResumeThread(RasThread)")); | |
a0b4c98b | 1062 | |
d71cc120 | 1063 | ok = false; |
a0b4c98b VZ |
1064 | } |
1065 | } | |
1066 | ||
1067 | // create all the stuff we need to be notified about RAS connection | |
1068 | // status change | |
1069 | ||
1070 | if ( ok ) | |
1071 | { | |
1072 | // first create an event to wait on | |
d71cc120 | 1073 | m_data->hEventRas = ::CreateEvent |
97247d36 | 1074 | ( |
a0b4c98b | 1075 | NULL, // security attribute (default) |
97247d36 | 1076 | FALSE, // manual reset (no, it is automatic) |
a0b4c98b VZ |
1077 | FALSE, // initial state (not signaled) |
1078 | NULL // name (no) | |
97247d36 VZ |
1079 | ); |
1080 | if ( !m_data->hEventRas ) | |
a0b4c98b | 1081 | { |
f6bcfd97 | 1082 | wxLogLastError(wxT("CreateEvent(RasStatus)")); |
a0b4c98b | 1083 | |
d71cc120 | 1084 | ok = false; |
a0b4c98b VZ |
1085 | } |
1086 | } | |
1087 | ||
1088 | if ( ok ) | |
1089 | { | |
97247d36 VZ |
1090 | // create the event we use to quit the thread: using a manual event |
1091 | // here avoids problems with missing the event if wxDialUpManagerMSW | |
1092 | // is created and destroyed immediately, before wxRasStatusWindowProc | |
1093 | // starts waiting on the event | |
d71cc120 | 1094 | m_data->hEventQuit = ::CreateEvent |
97247d36 VZ |
1095 | ( |
1096 | NULL, // default security | |
1097 | TRUE, // manual event | |
1098 | FALSE, // initially non signalled | |
1099 | NULL // nameless | |
1100 | ); | |
1101 | if ( !m_data->hEventQuit ) | |
a0b4c98b | 1102 | { |
f6bcfd97 | 1103 | wxLogLastError(wxT("CreateEvent(RasThreadQuit)")); |
a0b4c98b VZ |
1104 | |
1105 | CleanUpThreadData(); | |
1106 | ||
d71cc120 | 1107 | ok = false; |
a0b4c98b VZ |
1108 | } |
1109 | } | |
1110 | ||
2690830e | 1111 | if ( ok && !ms_hwndRas ) |
a0b4c98b VZ |
1112 | { |
1113 | // create a hidden window to receive notification about connections | |
1114 | // status change | |
8563c6f1 VZ |
1115 | ms_hwndRas = wxCreateHiddenWindow |
1116 | ( | |
1117 | &gs_classForDialUpWindow, | |
1118 | wxMSWDIALUP_WNDCLASSNAME, | |
1119 | wxRasStatusWindowProc | |
1120 | ); | |
2690830e | 1121 | if ( !ms_hwndRas ) |
a0b4c98b | 1122 | { |
f6bcfd97 | 1123 | wxLogLastError(wxT("CreateWindow(RasHiddenWindow)")); |
a0b4c98b VZ |
1124 | |
1125 | CleanUpThreadData(); | |
1126 | ||
d71cc120 | 1127 | ok = false; |
a0b4c98b | 1128 | } |
a0b4c98b VZ |
1129 | } |
1130 | ||
97247d36 | 1131 | m_data->hWnd = ms_hwndRas; |
2690830e | 1132 | |
a0b4c98b VZ |
1133 | if ( ok ) |
1134 | { | |
1135 | // start the secondary thread | |
97247d36 | 1136 | m_data->dialUpManager = this; |
a0b4c98b VZ |
1137 | |
1138 | DWORD tid; | |
1139 | m_hThread = CreateThread | |
1140 | ( | |
1141 | NULL, | |
1142 | 0, | |
1143 | (LPTHREAD_START_ROUTINE)wxRasMonitorThread, | |
97247d36 | 1144 | (void *)m_data, |
a0b4c98b VZ |
1145 | 0, |
1146 | &tid | |
1147 | ); | |
1148 | ||
1149 | if ( !m_hThread ) | |
1150 | { | |
f6bcfd97 | 1151 | wxLogLastError(wxT("CreateThread(RasStatusThread)")); |
a0b4c98b VZ |
1152 | |
1153 | CleanUpThreadData(); | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | if ( ok ) | |
1158 | { | |
1159 | // start receiving RAS notifications | |
1160 | DWORD dwRet = ms_pfnRasConnectionNotification | |
1161 | ( | |
1162 | (HRASCONN)INVALID_HANDLE_VALUE, | |
97247d36 | 1163 | m_data->hEventRas, |
a0b4c98b VZ |
1164 | 3 /* RASCN_Connection | RASCN_Disconnection */ |
1165 | ); | |
1166 | ||
1167 | if ( dwRet != 0 ) | |
1168 | { | |
223d09f6 | 1169 | wxLogDebug(wxT("RasConnectionNotification() failed: %s"), |
5b09e9c2 | 1170 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
1171 | |
1172 | CleanUpThreadData(); | |
1173 | } | |
1174 | else | |
1175 | { | |
d71cc120 | 1176 | return true; |
a0b4c98b VZ |
1177 | } |
1178 | } | |
1179 | ||
1180 | // we're running under Windows 95 and have to poll ourselves | |
1181 | // (or, alternatively, the code above for NT/98 failed) | |
1182 | m_timerStatusPolling.Stop(); | |
1183 | if ( nSeconds == 0 ) | |
1184 | { | |
1185 | // default value | |
1186 | nSeconds = 60; | |
1187 | } | |
1188 | m_timerStatusPolling.Start(nSeconds * 1000); | |
1189 | ||
d71cc120 | 1190 | return true; |
a0b4c98b VZ |
1191 | } |
1192 | ||
1193 | void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus() | |
1194 | { | |
223d09f6 | 1195 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b | 1196 | |
8d7eaf91 | 1197 | if ( --m_autoCheckLevel != 0 ) |
6bba111c VZ |
1198 | { |
1199 | // still checking | |
1200 | return; | |
1201 | } | |
1202 | ||
a0b4c98b VZ |
1203 | if ( m_hThread ) |
1204 | { | |
1205 | // we have running secondary thread, it's just enough to suspend it | |
1206 | if ( SuspendThread(m_hThread) == (DWORD)-1 ) | |
1207 | { | |
f6bcfd97 | 1208 | wxLogLastError(wxT("SuspendThread(RasThread)")); |
a0b4c98b VZ |
1209 | } |
1210 | } | |
1211 | else | |
1212 | { | |
1213 | // even simpler - just stop the timer | |
1214 | m_timerStatusPolling.Stop(); | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | // ---------------------------------------------------------------------------- | |
1219 | // stubs which don't do anything in MSW version | |
1220 | // ---------------------------------------------------------------------------- | |
1221 | ||
1222 | void wxDialUpManagerMSW::SetWellKnownHost(const wxString& WXUNUSED(hostname), | |
1223 | int WXUNUSED(port)) | |
1224 | { | |
223d09f6 | 1225 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1226 | |
1227 | // nothing to do - we don't use this | |
1228 | } | |
1229 | ||
1230 | void wxDialUpManagerMSW::SetConnectCommand(const wxString& WXUNUSED(dial), | |
1231 | const wxString& WXUNUSED(hangup)) | |
1232 | { | |
223d09f6 | 1233 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1234 | |
1235 | // nothing to do - we don't use this | |
1236 | } | |
1237 | ||
1238 | // ---------------------------------------------------------------------------- | |
1239 | // callbacks | |
1240 | // ---------------------------------------------------------------------------- | |
1241 | ||
1242 | static DWORD wxRasMonitorThread(wxRasThreadData *data) | |
1243 | { | |
1244 | HANDLE handles[2]; | |
1245 | handles[0] = data->hEventRas; | |
1246 | handles[1] = data->hEventQuit; | |
1247 | ||
d71cc120 | 1248 | bool cont = true; |
a0b4c98b VZ |
1249 | while ( cont ) |
1250 | { | |
d71cc120 | 1251 | DWORD dwRet = ::WaitForMultipleObjects(2, handles, FALSE, INFINITE); |
a0b4c98b VZ |
1252 | |
1253 | switch ( dwRet ) | |
1254 | { | |
1255 | case WAIT_OBJECT_0: | |
1256 | // RAS connection status changed | |
1257 | SendMessage(data->hWnd, wxWM_RAS_STATUS_CHANGED, | |
1258 | 0, (LPARAM)data); | |
1259 | break; | |
1260 | ||
1261 | case WAIT_OBJECT_0 + 1: | |
d71cc120 | 1262 | cont = false; |
a0b4c98b VZ |
1263 | break; |
1264 | ||
97247d36 | 1265 | default: |
9a83f860 | 1266 | wxFAIL_MSG( wxT("unexpected return of WaitForMultipleObjects()") ); |
97247d36 VZ |
1267 | // fall through |
1268 | ||
a0b4c98b | 1269 | case WAIT_FAILED: |
97247d36 VZ |
1270 | // using wxLogLastError() from here is dangerous: we risk to |
1271 | // deadlock the main thread if wxLog sends output to GUI | |
1272 | DWORD err = GetLastError(); | |
141f4958 WS |
1273 | wxMessageOutputDebug dbg; |
1274 | dbg.Printf | |
97247d36 VZ |
1275 | ( |
1276 | wxT("WaitForMultipleObjects(RasMonitor) failed: 0x%08lx (%s)"), | |
1277 | err, | |
1278 | wxSysErrorMsg(err) | |
1279 | ); | |
97247d36 VZ |
1280 | |
1281 | // no sense in continuing, who knows if the handles we're | |
1282 | // waiting for even exist yet... | |
1283 | return (DWORD)-1; | |
a0b4c98b VZ |
1284 | } |
1285 | } | |
1286 | ||
97247d36 VZ |
1287 | // we don't need it any more now and if this thread ran, it is our |
1288 | // responsability to free the data | |
1289 | delete data; | |
1290 | ||
a0b4c98b VZ |
1291 | return 0; |
1292 | } | |
1293 | ||
1294 | static LRESULT APIENTRY wxRasStatusWindowProc(HWND hWnd, UINT message, | |
1295 | WPARAM wParam, LPARAM lParam) | |
1296 | { | |
cd6af1cb | 1297 | switch ( message ) |
a0b4c98b | 1298 | { |
cd6af1cb VZ |
1299 | case wxWM_RAS_STATUS_CHANGED: |
1300 | { | |
1301 | wxRasThreadData *data = (wxRasThreadData *)lParam; | |
1302 | data->dialUpManager->OnConnectStatusChange(); | |
1303 | } | |
1304 | break; | |
2690830e | 1305 | |
cd6af1cb VZ |
1306 | case wxWM_RAS_DIALING_PROGRESS: |
1307 | { | |
1308 | wxDialUpManagerMSW *dialMan = wxDialUpManagerMSW::GetDialer(); | |
1309 | ||
1310 | dialMan->OnDialProgress((RASCONNSTATE)wParam, lParam); | |
1311 | } | |
1312 | break; | |
1313 | ||
1314 | default: | |
1315 | return ::DefWindowProc(hWnd, message, wParam, lParam); | |
2690830e | 1316 | } |
a0b4c98b VZ |
1317 | |
1318 | return 0; | |
1319 | } | |
1320 | ||
2eb10e2a | 1321 | static void WINAPI wxRasDialFunc(UINT WXUNUSED(unMsg), |
a0b4c98b VZ |
1322 | RASCONNSTATE rasconnstate, |
1323 | DWORD dwError) | |
1324 | { | |
1325 | wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer(); | |
1326 | ||
223d09f6 | 1327 | wxCHECK_RET( dialUpManager, wxT("who started to dial then?") ); |
a0b4c98b | 1328 | |
2a1f999f | 1329 | SendMessage(wxDialUpManagerMSW::GetRasWindow(), wxWM_RAS_DIALING_PROGRESS, |
2690830e | 1330 | rasconnstate, dwError); |
a0b4c98b VZ |
1331 | } |
1332 | ||
1c25d245 VZ |
1333 | #endif // __BORLANDC__ |
1334 | ||
a0b4c98b | 1335 | #endif // wxUSE_DIALUP_MANAGER |