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