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