]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
119f7bb7 | 2 | // Name: src/msw/utils.cpp |
2bda0e17 KB |
3 | // Purpose: Various utilities |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
b568d04f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
a4f96412 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
a4f96412 VZ |
28 | #include "wx/utils.h" |
29 | #include "wx/app.h" | |
1f0500b3 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
743e0a66 | 32 | #endif //WX_PRECOMP |
2bda0e17 | 33 | |
66d84d11 | 34 | #include "wx/msw/registry.h" |
2739d4f0 | 35 | #include "wx/apptrait.h" |
23ee4b48 | 36 | #include "wx/dynlib.h" |
15cdcf6a | 37 | #include "wx/dynload.h" |
a45fb5b4 | 38 | #include "wx/scopeguard.h" |
b815db22 | 39 | #include "wx/filename.h" |
2739d4f0 | 40 | |
2dc357ea VZ |
41 | #include "wx/confbase.h" // for wxExpandEnvVars() |
42 | ||
b568d04f | 43 | #include "wx/msw/private.h" // includes <windows.h> |
b481194f | 44 | #include "wx/msw/private/hiddenwin.h" |
a6c2e2c7 | 45 | #include "wx/msw/missing.h" // for CHARSET_HANGUL |
b568d04f | 46 | |
f6afe7fd RN |
47 | #if defined(__CYGWIN__) |
48 | //CYGWIN gives annoying warning about runtime stuff if we don't do this | |
49 | # define USE_SYS_TYPES_FD_SET | |
50 | # include <sys/types.h> | |
51 | #endif | |
52 | ||
7acf6a92 JS |
53 | // Doesn't work with Cygwin at present |
54 | #if wxUSE_SOCKETS && (defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) || defined(__CYGWIN32__)) | |
c1cb4153 VZ |
55 | // apparently we need to include winsock.h to get WSADATA and other stuff |
56 | // used in wxGetFullHostName() with the old mingw32 versions | |
57 | #include <winsock.h> | |
58 | #endif | |
59 | ||
f172cb82 | 60 | #if !defined(__GNUWIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
a4f96412 | 61 | #include <direct.h> |
ce3ed50d | 62 | |
a4f96412 VZ |
63 | #ifndef __MWERKS__ |
64 | #include <dos.h> | |
65 | #endif | |
743e0a66 | 66 | #endif //GNUWIN32 |
2bda0e17 | 67 | |
b39dbf34 | 68 | #if defined(__CYGWIN__) |
a4f96412 VZ |
69 | #include <sys/unistd.h> |
70 | #include <sys/stat.h> | |
715e4f7e VZ |
71 | #include <sys/cygwin.h> // for cygwin_conv_path() |
72 | // and cygwin_conv_to_full_win32_path() | |
73 | #include <cygwin/version.h> | |
743e0a66 VZ |
74 | #endif //GNUWIN32 |
75 | ||
2bda0e17 KB |
76 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs |
77 | // this (3.1 I believe) and how to test for it. | |
78 | // If this works for Borland 4.0 as well, then no worries. | |
a4f96412 | 79 | #include <dir.h> |
2bda0e17 KB |
80 | #endif |
81 | ||
a4f96412 VZ |
82 | // VZ: there is some code using NetXXX() functions to get the full user name: |
83 | // I don't think it's a good idea because they don't work under Win95 and | |
84 | // seem to return the same as wxGetUserId() under NT. If you really want | |
85 | // to use them, just #define USE_NET_API | |
86 | #undef USE_NET_API | |
87 | ||
88 | #ifdef USE_NET_API | |
89 | #include <lm.h> | |
90 | #endif // USE_NET_API | |
91 | ||
4676948b | 92 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
2e38557f JS |
93 | #ifndef __UNIX__ |
94 | #include <io.h> | |
95 | #endif | |
2bda0e17 | 96 | |
a4f96412 VZ |
97 | #ifndef __GNUWIN32__ |
98 | #include <shellapi.h> | |
99 | #endif | |
2bda0e17 KB |
100 | #endif |
101 | ||
2bda0e17 | 102 | #ifndef __WATCOMC__ |
3f4a0c5b VZ |
103 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
104 | #include <errno.h> | |
105 | #endif | |
2bda0e17 | 106 | #endif |
2bda0e17 | 107 | |
e0f6b731 JS |
108 | // For wxKillAllChildren |
109 | #include <tlhelp32.h> | |
110 | ||
b568d04f VZ |
111 | // ---------------------------------------------------------------------------- |
112 | // constants | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
2bda0e17 | 115 | // In the WIN.INI file |
119f7bb7 | 116 | #if (!defined(USE_NET_API) && !defined(__WXWINCE__)) || defined(__WXMICROWIN__) |
e7d9c398 | 117 | static const wxChar WX_SECTION[] = wxT("wxWindows"); |
119f7bb7 WS |
118 | #endif |
119 | ||
120 | #if (!defined(USE_NET_API) && !defined(__WXWINCE__)) | |
b568d04f | 121 | static const wxChar eUSERNAME[] = wxT("UserName"); |
119f7bb7 | 122 | #endif |
b568d04f | 123 | |
c5ba4851 VZ |
124 | WXDLLIMPEXP_DATA_BASE(const wxChar *) wxUserResourceStr = wxT("TEXT"); |
125 | ||
b568d04f VZ |
126 | // ============================================================================ |
127 | // implementation | |
128 | // ============================================================================ | |
2bda0e17 | 129 | |
b568d04f VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // get host name and related | |
132 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 133 | |
1f0500b3 | 134 | // Get hostname only (without domain name) |
32cdc453 | 135 | bool wxGetHostName(wxChar *buf, int maxSize) |
2bda0e17 | 136 | { |
4676948b | 137 | #if defined(__WXWINCE__) |
32cdc453 VZ |
138 | // GetComputerName() is not supported but the name seems to be stored in |
139 | // this location in the registry, at least for PPC2003 and WM5 | |
140 | wxString hostName; | |
141 | wxRegKey regKey(wxRegKey::HKLM, wxT("Ident")); | |
142 | if ( !regKey.HasValue(wxT("Name")) || | |
143 | !regKey.QueryValue(wxT("Name"), hostName) ) | |
144 | return false; | |
145 | ||
146 | wxStrlcpy(buf, hostName.wx_str(), maxSize); | |
147 | #else // !__WXWINCE__ | |
b568d04f VZ |
148 | DWORD nSize = maxSize; |
149 | if ( !::GetComputerName(buf, &nSize) ) | |
150 | { | |
f6bcfd97 | 151 | wxLogLastError(wxT("GetComputerName")); |
b568d04f | 152 | |
7010702f | 153 | return false; |
b568d04f | 154 | } |
32cdc453 | 155 | #endif // __WXWINCE__/!__WXWINCE__ |
b568d04f | 156 | |
27d2dbbc | 157 | return true; |
2bda0e17 KB |
158 | } |
159 | ||
1f0500b3 | 160 | // get full hostname (with domain name if possible) |
b568d04f VZ |
161 | bool wxGetFullHostName(wxChar *buf, int maxSize) |
162 | { | |
a8ff046b | 163 | #if !defined( __WXMICROWIN__) && wxUSE_DYNLIB_CLASS && wxUSE_SOCKETS |
1f0500b3 | 164 | // TODO should use GetComputerNameEx() when available |
79180098 | 165 | |
15cdcf6a | 166 | // we don't want to always link with Winsock DLL as we might not use it at |
7b28e0ed VZ |
167 | // all, so load it dynamically here if needed (and don't complain if it is |
168 | // missing, we handle this) | |
169 | wxLogNull noLog; | |
170 | ||
9a83f860 | 171 | wxDynamicLibrary dllWinsock(wxT("ws2_32.dll"), wxDL_VERBATIM); |
15cdcf6a | 172 | if ( dllWinsock.IsLoaded() ) |
1f0500b3 | 173 | { |
15cdcf6a VZ |
174 | typedef int (PASCAL *WSAStartup_t)(WORD, WSADATA *); |
175 | typedef int (PASCAL *gethostname_t)(char *, int); | |
176 | typedef hostent* (PASCAL *gethostbyname_t)(const char *); | |
177 | typedef hostent* (PASCAL *gethostbyaddr_t)(const char *, int , int); | |
178 | typedef int (PASCAL *WSACleanup_t)(void); | |
1f0500b3 | 179 | |
15cdcf6a VZ |
180 | #define LOAD_WINSOCK_FUNC(func) \ |
181 | func ## _t \ | |
9a83f860 | 182 | pfn ## func = (func ## _t)dllWinsock.GetSymbol(wxT(#func)) |
1f0500b3 | 183 | |
15cdcf6a VZ |
184 | LOAD_WINSOCK_FUNC(WSAStartup); |
185 | ||
186 | WSADATA wsa; | |
187 | if ( pfnWSAStartup && pfnWSAStartup(MAKEWORD(1, 1), &wsa) == 0 ) | |
188 | { | |
189 | LOAD_WINSOCK_FUNC(gethostname); | |
190 | ||
191 | wxString host; | |
192 | if ( pfngethostname ) | |
193 | { | |
194 | char bufA[256]; | |
195 | if ( pfngethostname(bufA, WXSIZEOF(bufA)) == 0 ) | |
1f0500b3 | 196 | { |
15cdcf6a VZ |
197 | // gethostname() won't usually include the DNS domain name, |
198 | // for this we need to work a bit more | |
199 | if ( !strchr(bufA, '.') ) | |
200 | { | |
201 | LOAD_WINSOCK_FUNC(gethostbyname); | |
202 | ||
203 | struct hostent *pHostEnt = pfngethostbyname | |
204 | ? pfngethostbyname(bufA) | |
205 | : NULL; | |
206 | ||
207 | if ( pHostEnt ) | |
208 | { | |
209 | // Windows will use DNS internally now | |
210 | LOAD_WINSOCK_FUNC(gethostbyaddr); | |
211 | ||
212 | pHostEnt = pfngethostbyaddr | |
213 | ? pfngethostbyaddr(pHostEnt->h_addr, | |
214 | 4, AF_INET) | |
215 | : NULL; | |
216 | } | |
217 | ||
218 | if ( pHostEnt ) | |
219 | { | |
220 | host = wxString::FromAscii(pHostEnt->h_name); | |
221 | } | |
222 | } | |
1f0500b3 VZ |
223 | } |
224 | } | |
1f0500b3 | 225 | |
15cdcf6a VZ |
226 | LOAD_WINSOCK_FUNC(WSACleanup); |
227 | if ( pfnWSACleanup ) | |
228 | pfnWSACleanup(); | |
1f0500b3 | 229 | |
1f0500b3 | 230 | |
15cdcf6a VZ |
231 | if ( !host.empty() ) |
232 | { | |
64accea5 | 233 | wxStrlcpy(buf, host.c_str(), maxSize); |
15cdcf6a | 234 | |
27d2dbbc | 235 | return true; |
15cdcf6a | 236 | } |
1f0500b3 VZ |
237 | } |
238 | } | |
15cdcf6a | 239 | #endif // !__WXMICROWIN__ |
1f0500b3 | 240 | |
b568d04f VZ |
241 | return wxGetHostName(buf, maxSize); |
242 | } | |
243 | ||
2bda0e17 | 244 | // Get user ID e.g. jacs |
7bea7b91 WS |
245 | bool wxGetUserId(wxChar *WXUNUSED_IN_WINCE(buf), |
246 | int WXUNUSED_IN_WINCE(maxSize)) | |
2bda0e17 | 247 | { |
4676948b | 248 | #if defined(__WXWINCE__) |
040e5f77 | 249 | // TODO-CE |
7010702f | 250 | return false; |
e408bf52 | 251 | #else |
0a144271 VZ |
252 | DWORD nSize = maxSize; |
253 | if ( ::GetUserName(buf, &nSize) == 0 ) | |
254 | { | |
a4f96412 | 255 | // actually, it does happen on Win9x if the user didn't log on |
223d09f6 | 256 | DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize); |
a4f96412 VZ |
257 | if ( res == 0 ) |
258 | { | |
259 | // not found | |
27d2dbbc | 260 | return false; |
a4f96412 | 261 | } |
0a144271 VZ |
262 | } |
263 | ||
27d2dbbc | 264 | return true; |
2bda0e17 KB |
265 | #endif |
266 | } | |
267 | ||
268 | // Get user name e.g. Julian Smart | |
119f7bb7 | 269 | bool wxGetUserName(wxChar *buf, int maxSize) |
2bda0e17 | 270 | { |
119f7bb7 | 271 | wxCHECK_MSG( buf && ( maxSize > 0 ), false, |
9a83f860 | 272 | wxT("empty buffer in wxGetUserName") ); |
f77c0fe3 | 273 | #if defined(__WXWINCE__) && wxUSE_REGKEY |
66d84d11 WS |
274 | wxLogNull noLog; |
275 | wxRegKey key(wxRegKey::HKCU, wxT("ControlPanel\\Owner")); | |
119f7bb7 WS |
276 | if(!key.Open(wxRegKey::Read)) |
277 | return false; | |
278 | wxString name; | |
66d84d11 | 279 | if(!key.QueryValue(wxT("Owner"),name)) |
119f7bb7 | 280 | return false; |
e408bf52 | 281 | wxStrlcpy(buf, name.c_str(), maxSize); |
119f7bb7 | 282 | return true; |
4676948b | 283 | #elif defined(USE_NET_API) |
3e3be693 VZ |
284 | CHAR szUserName[256]; |
285 | if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) ) | |
7010702f | 286 | return false; |
a4f96412 | 287 | |
3e3be693 VZ |
288 | // TODO how to get the domain name? |
289 | CHAR *szDomain = ""; | |
a4f96412 | 290 | |
3e3be693 VZ |
291 | // the code is based on the MSDN example (also see KB article Q119670) |
292 | WCHAR wszUserName[256]; // Unicode user name | |
293 | WCHAR wszDomain[256]; | |
294 | LPBYTE ComputerName; | |
a4f96412 | 295 | |
3e3be693 | 296 | USER_INFO_2 *ui2; // User structure |
a4f96412 | 297 | |
3e3be693 VZ |
298 | // Convert ANSI user name and domain to Unicode |
299 | MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1, | |
300 | wszUserName, WXSIZEOF(wszUserName) ); | |
301 | MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1, | |
302 | wszDomain, WXSIZEOF(wszDomain) ); | |
a4f96412 | 303 | |
3e3be693 VZ |
304 | // Get the computer name of a DC for the domain. |
305 | if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success ) | |
306 | { | |
4c51a665 | 307 | wxLogError(wxT("Cannot find domain controller")); |
a4f96412 | 308 | |
3e3be693 VZ |
309 | goto error; |
310 | } | |
a4f96412 | 311 | |
3e3be693 VZ |
312 | // Look up the user on the DC |
313 | NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName, | |
314 | (LPWSTR)&wszUserName, | |
315 | 2, // level - we want USER_INFO_2 | |
316 | (LPBYTE *) &ui2 ); | |
317 | switch ( status ) | |
318 | { | |
319 | case NERR_Success: | |
320 | // ok | |
321 | break; | |
a4f96412 | 322 | |
3e3be693 VZ |
323 | case NERR_InvalidComputer: |
324 | wxLogError(wxT("Invalid domain controller name.")); | |
a4f96412 | 325 | |
3e3be693 | 326 | goto error; |
a4f96412 | 327 | |
3e3be693 VZ |
328 | case NERR_UserNotFound: |
329 | wxLogError(wxT("Invalid user name '%s'."), szUserName); | |
a4f96412 | 330 | |
3e3be693 | 331 | goto error; |
a4f96412 | 332 | |
3e3be693 VZ |
333 | default: |
334 | wxLogSysError(wxT("Can't get information about user")); | |
a4f96412 | 335 | |
3e3be693 VZ |
336 | goto error; |
337 | } | |
a4f96412 | 338 | |
3e3be693 VZ |
339 | // Convert the Unicode full name to ANSI |
340 | WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1, | |
341 | buf, maxSize, NULL, NULL ); | |
a4f96412 | 342 | |
27d2dbbc | 343 | return true; |
a4f96412 VZ |
344 | |
345 | error: | |
3e3be693 | 346 | wxLogError(wxT("Couldn't look up full user name.")); |
a4f96412 | 347 | |
27d2dbbc | 348 | return false; |
a4f96412 | 349 | #else // !USE_NET_API |
3e3be693 | 350 | // Could use NIS, MS-Mail or other site specific programs |
77ffb593 | 351 | // Use wxWidgets configuration data |
fda7962d | 352 | bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxEmptyString, buf, maxSize - 1) != 0; |
3e3be693 VZ |
353 | if ( !ok ) |
354 | { | |
355 | ok = wxGetUserId(buf, maxSize); | |
356 | } | |
0a144271 | 357 | |
3e3be693 VZ |
358 | if ( !ok ) |
359 | { | |
e408bf52 | 360 | wxStrlcpy(buf, wxT("Unknown User"), maxSize); |
b568d04f | 361 | } |
0a144271 | 362 | |
27d2dbbc | 363 | return true; |
040e5f77 | 364 | #endif // Win32/16 |
2bda0e17 KB |
365 | } |
366 | ||
b568d04f | 367 | const wxChar* wxGetHomeDir(wxString *pstr) |
2bda0e17 | 368 | { |
2dc357ea | 369 | wxString& strDir = *pstr; |
2bda0e17 | 370 | |
2dc357ea | 371 | // first branch is for Cygwin |
dc484d49 | 372 | #if defined(__UNIX__) && !defined(__WINE__) |
715e4f7e | 373 | const wxChar *szHome = wxGetenv(wxT("HOME")); |
b568d04f VZ |
374 | if ( szHome == NULL ) { |
375 | // we're homeless... | |
376 | wxLogWarning(_("can't find user's HOME, using current directory.")); | |
377 | strDir = wxT("."); | |
378 | } | |
379 | else | |
380 | strDir = szHome; | |
2bda0e17 | 381 | |
b568d04f VZ |
382 | // add a trailing slash if needed |
383 | if ( strDir.Last() != wxT('/') ) | |
384 | strDir << wxT('/'); | |
3ffbc733 | 385 | |
e02e8816 | 386 | #ifdef __CYGWIN__ |
2dc357ea VZ |
387 | // Cygwin returns unix type path but that does not work well |
388 | static wxChar windowsPath[MAX_PATH]; | |
715e4f7e VZ |
389 | #if CYGWIN_VERSION_DLL_MAJOR >= 1007 |
390 | cygwin_conv_path(CCP_POSIX_TO_WIN_W, strDir, windowsPath, MAX_PATH); | |
391 | #else | |
392 | cygwin_conv_to_full_win32_path(strDir, windowsPath); | |
393 | #endif | |
2dc357ea | 394 | strDir = windowsPath; |
3ffbc733 | 395 | #endif |
4676948b | 396 | #elif defined(__WXWINCE__) |
0367d339 | 397 | strDir = wxT("\\"); |
4676948b | 398 | #else |
2dc357ea | 399 | strDir.clear(); |
96c21216 | 400 | |
2dc357ea VZ |
401 | // If we have a valid HOME directory, as is used on many machines that |
402 | // have unix utilities on them, we should use that. | |
403 | const wxChar *szHome = wxGetenv(wxT("HOME")); | |
96c21216 | 404 | |
2dc357ea VZ |
405 | if ( szHome != NULL ) |
406 | { | |
96c21216 | 407 | strDir = szHome; |
2dc357ea VZ |
408 | } |
409 | else // no HOME, try HOMEDRIVE/PATH | |
410 | { | |
411 | szHome = wxGetenv(wxT("HOMEDRIVE")); | |
412 | if ( szHome != NULL ) | |
96c21216 | 413 | strDir << szHome; |
2dc357ea | 414 | szHome = wxGetenv(wxT("HOMEPATH")); |
96c21216 | 415 | |
2dc357ea VZ |
416 | if ( szHome != NULL ) |
417 | { | |
96c21216 VZ |
418 | strDir << szHome; |
419 | ||
420 | // the idea is that under NT these variables have default values | |
421 | // of "%systemdrive%:" and "\\". As we don't want to create our | |
422 | // config files in the root directory of the system drive, we will | |
423 | // create it in our program's dir. However, if the user took care | |
424 | // to set HOMEPATH to something other than "\\", we suppose that he | |
425 | // knows what he is doing and use the supplied value. | |
426 | if ( wxStrcmp(szHome, wxT("\\")) == 0 ) | |
2dc357ea VZ |
427 | strDir.clear(); |
428 | } | |
429 | } | |
96c21216 | 430 | |
2dc357ea VZ |
431 | if ( strDir.empty() ) |
432 | { | |
433 | // If we have a valid USERPROFILE directory, as is the case in | |
434 | // Windows NT, 2000 and XP, we should use that as our home directory. | |
435 | szHome = wxGetenv(wxT("USERPROFILE")); | |
2bda0e17 | 436 | |
2dc357ea | 437 | if ( szHome != NULL ) |
96c21216 | 438 | strDir = szHome; |
2dc357ea | 439 | } |
2bda0e17 | 440 | |
2dc357ea VZ |
441 | if ( !strDir.empty() ) |
442 | { | |
443 | // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the | |
444 | // value once again, it shouldn't hurt anyhow | |
445 | strDir = wxExpandEnvVars(strDir); | |
446 | } | |
447 | else // fall back to the program directory | |
448 | { | |
77d8d6cd | 449 | // extract the directory component of the program file name |
bd365871 | 450 | wxFileName::SplitPath(wxGetFullModuleName(), &strDir, NULL, NULL); |
2dc357ea | 451 | } |
4676948b | 452 | #endif // UNIX/Win |
b568d04f | 453 | |
2dc357ea | 454 | return strDir.c_str(); |
afb74891 VZ |
455 | } |
456 | ||
14d63513 | 457 | wxString wxGetUserHome(const wxString& user) |
2bda0e17 | 458 | { |
14d63513 | 459 | wxString home; |
2bda0e17 | 460 | |
14d63513 VZ |
461 | if ( user.empty() || user == wxGetUserId() ) |
462 | wxGetHomeDir(&home); | |
463 | ||
464 | return home; | |
2bda0e17 KB |
465 | } |
466 | ||
7bea7b91 | 467 | bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path), |
7ba7c4e6 VZ |
468 | wxDiskspaceSize_t *WXUNUSED_IN_WINCE(pTotal), |
469 | wxDiskspaceSize_t *WXUNUSED_IN_WINCE(pFree)) | |
eadd7bd2 | 470 | { |
4676948b | 471 | #ifdef __WXWINCE__ |
040e5f77 | 472 | // TODO-CE |
7010702f | 473 | return false; |
4676948b | 474 | #else |
eadd7bd2 | 475 | if ( path.empty() ) |
7010702f | 476 | return false; |
eadd7bd2 | 477 | |
ee88cb34 MB |
478 | // old w32api don't have ULARGE_INTEGER |
479 | #if defined(__WIN32__) && \ | |
480 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 )) | |
eadd7bd2 VZ |
481 | // GetDiskFreeSpaceEx() is not available under original Win95, check for |
482 | // it | |
9ee966ec VZ |
483 | typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR, |
484 | PULARGE_INTEGER, | |
485 | PULARGE_INTEGER, | |
486 | PULARGE_INTEGER); | |
eadd7bd2 VZ |
487 | |
488 | GetDiskFreeSpaceEx_t | |
bb0e27ee | 489 | pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress |
eadd7bd2 | 490 | ( |
9a83f860 | 491 | ::GetModuleHandle(wxT("kernel32.dll")), |
eadd7bd2 VZ |
492 | #if wxUSE_UNICODE |
493 | "GetDiskFreeSpaceExW" | |
494 | #else | |
495 | "GetDiskFreeSpaceExA" | |
496 | #endif | |
497 | ); | |
498 | ||
499 | if ( pGetDiskFreeSpaceEx ) | |
500 | { | |
501 | ULARGE_INTEGER bytesFree, bytesTotal; | |
502 | ||
503 | // may pass the path as is, GetDiskFreeSpaceEx() is smart enough | |
715e4f7e | 504 | if ( !pGetDiskFreeSpaceEx(path.t_str(), |
eadd7bd2 VZ |
505 | &bytesFree, |
506 | &bytesTotal, | |
507 | NULL) ) | |
508 | { | |
9a83f860 | 509 | wxLogLastError(wxT("GetDiskFreeSpaceEx")); |
eadd7bd2 | 510 | |
27d2dbbc | 511 | return false; |
eadd7bd2 VZ |
512 | } |
513 | ||
221e1181 VZ |
514 | // ULARGE_INTEGER is a union of a 64 bit value and a struct containing |
515 | // two 32 bit fields which may be or may be not named - try to make it | |
516 | // compile in all cases | |
517 | #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT) | |
518 | #define UL(ul) ul.u | |
519 | #else // anon union | |
520 | #define UL(ul) ul | |
521 | #endif | |
eadd7bd2 VZ |
522 | if ( pTotal ) |
523 | { | |
7ba7c4e6 VZ |
524 | #if wxUSE_LONGLONG |
525 | *pTotal = wxDiskspaceSize_t(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart); | |
526 | #else | |
527 | *pTotal = wxDiskspaceSize_t(UL(bytesTotal).LowPart); | |
528 | #endif | |
eadd7bd2 VZ |
529 | } |
530 | ||
531 | if ( pFree ) | |
532 | { | |
7ba7c4e6 | 533 | #if wxUSE_LONGLONG |
221e1181 | 534 | *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart); |
7ba7c4e6 VZ |
535 | #else |
536 | *pFree = wxDiskspaceSize_t(UL(bytesFree).LowPart); | |
537 | #endif | |
eadd7bd2 VZ |
538 | } |
539 | } | |
540 | else | |
541 | #endif // Win32 | |
542 | { | |
bb0e27ee VZ |
543 | // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx() |
544 | // should be used instead - but if it's not available, fall back on | |
545 | // GetDiskFreeSpace() nevertheless... | |
546 | ||
eadd7bd2 VZ |
547 | DWORD lSectorsPerCluster, |
548 | lBytesPerSector, | |
549 | lNumberOfFreeClusters, | |
550 | lTotalNumberOfClusters; | |
551 | ||
552 | // FIXME: this is wrong, we should extract the root drive from path | |
553 | // instead, but this is the job for wxFileName... | |
715e4f7e | 554 | if ( !::GetDiskFreeSpace(path.t_str(), |
eadd7bd2 VZ |
555 | &lSectorsPerCluster, |
556 | &lBytesPerSector, | |
557 | &lNumberOfFreeClusters, | |
558 | &lTotalNumberOfClusters) ) | |
559 | { | |
9a83f860 | 560 | wxLogLastError(wxT("GetDiskFreeSpace")); |
eadd7bd2 | 561 | |
27d2dbbc | 562 | return false; |
eadd7bd2 VZ |
563 | } |
564 | ||
7ba7c4e6 | 565 | wxDiskspaceSize_t lBytesPerCluster = (wxDiskspaceSize_t) lSectorsPerCluster; |
eadd7bd2 VZ |
566 | lBytesPerCluster *= lBytesPerSector; |
567 | ||
568 | if ( pTotal ) | |
569 | { | |
570 | *pTotal = lBytesPerCluster; | |
571 | *pTotal *= lTotalNumberOfClusters; | |
572 | } | |
573 | ||
574 | if ( pFree ) | |
575 | { | |
576 | *pFree = lBytesPerCluster; | |
577 | *pFree *= lNumberOfFreeClusters; | |
578 | } | |
579 | } | |
580 | ||
27d2dbbc | 581 | return true; |
4676948b JS |
582 | #endif |
583 | // __WXWINCE__ | |
eadd7bd2 VZ |
584 | } |
585 | ||
308978f6 VZ |
586 | // ---------------------------------------------------------------------------- |
587 | // env vars | |
588 | // ---------------------------------------------------------------------------- | |
589 | ||
7bea7b91 WS |
590 | bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var), |
591 | wxString *WXUNUSED_IN_WINCE(value)) | |
308978f6 | 592 | { |
4676948b | 593 | #ifdef __WXWINCE__ |
040e5f77 | 594 | // no environment variables under CE |
27d2dbbc | 595 | return false; |
e622dd68 | 596 | #else // Win32 |
308978f6 | 597 | // first get the size of the buffer |
dc874fb4 | 598 | DWORD dwRet = ::GetEnvironmentVariable(var.t_str(), NULL, 0); |
308978f6 VZ |
599 | if ( !dwRet ) |
600 | { | |
601 | // this means that there is no such variable | |
27d2dbbc | 602 | return false; |
308978f6 VZ |
603 | } |
604 | ||
605 | if ( value ) | |
606 | { | |
dc874fb4 | 607 | (void)::GetEnvironmentVariable(var.t_str(), |
e0a050e3 | 608 | wxStringBuffer(*value, dwRet), |
de564874 | 609 | dwRet); |
308978f6 VZ |
610 | } |
611 | ||
27d2dbbc | 612 | return true; |
3a5bcc4d | 613 | #endif // WinCE/32 |
308978f6 VZ |
614 | } |
615 | ||
787de19a | 616 | bool wxDoSetEnv(const wxString& var, const wxChar *value) |
1fb45475 | 617 | { |
7bea7b91 WS |
618 | #ifdef __WXWINCE__ |
619 | // no environment variables under CE | |
787de19a VZ |
620 | wxUnusedVar(var); |
621 | wxUnusedVar(value); | |
7bea7b91 | 622 | return false; |
787de19a VZ |
623 | #else // !__WXWINCE__ |
624 | // update the CRT environment if possible as people expect getenv() to also | |
625 | // work and it is not affected by Win32 SetEnvironmentVariable() call (OTOH | |
626 | // the CRT does use Win32 call to update the process environment block so | |
627 | // there is no need to call it) | |
628 | // | |
629 | // TODO: add checks for the other compilers (and update wxSetEnv() | |
630 | // documentation in interface/wx/utils.h accordingly) | |
1b2f7b6d | 631 | #if defined(__VISUALC__) || defined(__MINGW32__) |
787de19a VZ |
632 | // notice that Microsoft _putenv() has different semantics from POSIX |
633 | // function with almost the same name: in particular it makes a copy of the | |
634 | // string instead of using it as part of environment so we can safely call | |
635 | // it here without going through all the troubles with wxSetEnvModule as in | |
636 | // src/unix/utilsunx.cpp | |
637 | wxString envstr = var; | |
638 | envstr += '='; | |
639 | if ( value ) | |
640 | envstr += value; | |
46b38b02 | 641 | _tputenv(envstr.t_str()); |
787de19a | 642 | #else // other compiler |
dc874fb4 | 643 | if ( !::SetEnvironmentVariable(var.t_str(), value) ) |
1fb45475 | 644 | { |
9a83f860 | 645 | wxLogLastError(wxT("SetEnvironmentVariable")); |
1fb45475 | 646 | |
27d2dbbc | 647 | return false; |
1fb45475 | 648 | } |
787de19a | 649 | #endif // compiler |
1fb45475 | 650 | |
27d2dbbc | 651 | return true; |
787de19a | 652 | #endif // __WXWINCE__/!__WXWINCE__ |
1fb45475 VZ |
653 | } |
654 | ||
90a77e64 VS |
655 | bool wxSetEnv(const wxString& variable, const wxString& value) |
656 | { | |
dc874fb4 | 657 | return wxDoSetEnv(variable, value.t_str()); |
90a77e64 VS |
658 | } |
659 | ||
660 | bool wxUnsetEnv(const wxString& variable) | |
661 | { | |
662 | return wxDoSetEnv(variable, NULL); | |
663 | } | |
664 | ||
b568d04f VZ |
665 | // ---------------------------------------------------------------------------- |
666 | // process management | |
667 | // ---------------------------------------------------------------------------- | |
668 | ||
50567b69 VZ |
669 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() |
670 | struct wxFindByPidParams | |
b568d04f | 671 | { |
50567b69 VZ |
672 | wxFindByPidParams() { hwnd = 0; pid = 0; } |
673 | ||
674 | // the HWND used to return the result | |
675 | HWND hwnd; | |
676 | ||
677 | // the PID we're looking from | |
678 | DWORD pid; | |
22f3361e | 679 | |
c0c133e1 | 680 | wxDECLARE_NO_COPY_CLASS(wxFindByPidParams); |
50567b69 VZ |
681 | }; |
682 | ||
683 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
684 | // level) window belonging to the given process | |
685 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
686 | { | |
687 | DWORD pid; | |
688 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
689 | ||
690 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
691 | if ( pid == params->pid ) | |
692 | { | |
693 | // remember the window we found | |
694 | params->hwnd = hwnd; | |
695 | ||
696 | // return FALSE to stop the enumeration | |
697 | return FALSE; | |
698 | } | |
699 | ||
700 | // continue enumeration | |
701 | return TRUE; | |
702 | } | |
e949bf13 | 703 | |
e0f6b731 JS |
704 | int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc); |
705 | ||
706 | int wxKill(long pid, wxSignal sig, wxKillError *krc, int flags) | |
50567b69 | 707 | { |
e0f6b731 JS |
708 | if (flags & wxKILL_CHILDREN) |
709 | wxKillAllChildren(pid, sig, krc); | |
9d8aca48 | 710 | |
50567b69 | 711 | // get the process handle to operate on |
e19dbcf7 VZ |
712 | DWORD dwAccess = PROCESS_QUERY_INFORMATION | SYNCHRONIZE; |
713 | if ( sig == wxSIGKILL ) | |
714 | dwAccess |= PROCESS_TERMINATE; | |
715 | ||
716 | HANDLE hProcess = ::OpenProcess(dwAccess, FALSE, (DWORD)pid); | |
50567b69 | 717 | if ( hProcess == NULL ) |
e949bf13 | 718 | { |
50567b69 | 719 | if ( krc ) |
e949bf13 | 720 | { |
a45fb5b4 VZ |
721 | // recognize wxKILL_ACCESS_DENIED as special because this doesn't |
722 | // mean that the process doesn't exist and this is important for | |
723 | // wxProcess::Exists() | |
724 | *krc = ::GetLastError() == ERROR_ACCESS_DENIED | |
725 | ? wxKILL_ACCESS_DENIED | |
726 | : wxKILL_NO_PROCESS; | |
50567b69 VZ |
727 | } |
728 | ||
729 | return -1; | |
730 | } | |
731 | ||
a45fb5b4 VZ |
732 | wxON_BLOCK_EXIT1(::CloseHandle, hProcess); |
733 | ||
e436c454 VZ |
734 | // Default timeout for waiting for the process termination after killing |
735 | // it. It should be long enough to allow the process to terminate even on a | |
736 | // busy system but short enough to avoid blocking the main thread for too | |
737 | // long. | |
738 | DWORD waitTimeout = 500; // ms | |
739 | ||
27d2dbbc | 740 | bool ok = true; |
50567b69 VZ |
741 | switch ( sig ) |
742 | { | |
743 | case wxSIGKILL: | |
744 | // kill the process forcefully returning -1 as error code | |
745 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
746 | { | |
747 | wxLogSysError(_("Failed to kill process %d"), pid); | |
748 | ||
749 | if ( krc ) | |
e949bf13 | 750 | { |
50567b69 VZ |
751 | // this is not supposed to happen if we could open the |
752 | // process | |
753 | *krc = wxKILL_ERROR; | |
e949bf13 | 754 | } |
50567b69 | 755 | |
27d2dbbc | 756 | ok = false; |
e949bf13 | 757 | } |
50567b69 VZ |
758 | break; |
759 | ||
760 | case wxSIGNONE: | |
e436c454 VZ |
761 | // Opening the process handle may succeed for a process even if it |
762 | // doesn't run any more (typically because open handles to it still | |
763 | // exist elsewhere, possibly in this process itself if we're | |
764 | // killing a child process) so we still need check if it hasn't | |
765 | // terminated yet but, unlike when killing it, we don't need to | |
766 | // wait for any time at all. | |
767 | waitTimeout = 0; | |
768 | break; | |
50567b69 VZ |
769 | |
770 | default: | |
771 | // any other signal means "terminate" | |
772 | { | |
773 | wxFindByPidParams params; | |
774 | params.pid = (DWORD)pid; | |
775 | ||
776 | // EnumWindows() has nice semantics: it returns 0 if it found | |
3103e8a9 | 777 | // something or if an error occurred and non zero if it |
50567b69 VZ |
778 | // enumerated all the window |
779 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
780 | { | |
781 | // did we find any window? | |
782 | if ( params.hwnd ) | |
783 | { | |
784 | // tell the app to close | |
785 | // | |
3103e8a9 | 786 | // NB: this is the harshest way, the app won't have an |
50567b69 VZ |
787 | // opportunity to save any files, for example, but |
788 | // this is probably what we want here. If not we | |
789 | // can also use SendMesageTimeout(WM_CLOSE) | |
790 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
791 | { | |
9a83f860 | 792 | wxLogLastError(wxT("PostMessage(WM_QUIT)")); |
50567b69 VZ |
793 | } |
794 | } | |
795 | else // it was an error then | |
796 | { | |
9a83f860 | 797 | wxLogLastError(wxT("EnumWindows")); |
50567b69 | 798 | |
27d2dbbc | 799 | ok = false; |
50567b69 VZ |
800 | } |
801 | } | |
802 | else // no windows for this PID | |
803 | { | |
804 | if ( krc ) | |
50567b69 | 805 | *krc = wxKILL_ERROR; |
50567b69 | 806 | |
27d2dbbc | 807 | ok = false; |
50567b69 VZ |
808 | } |
809 | } | |
810 | } | |
811 | ||
812 | // the return code | |
50567b69 VZ |
813 | if ( ok ) |
814 | { | |
815 | // as we wait for a short time, we can use just WaitForSingleObject() | |
816 | // and not MsgWaitForMultipleObjects() | |
e436c454 | 817 | switch ( ::WaitForSingleObject(hProcess, waitTimeout) ) |
50567b69 VZ |
818 | { |
819 | case WAIT_OBJECT_0: | |
e436c454 VZ |
820 | // Process terminated: normally this indicates that we |
821 | // successfully killed it but when testing for the process | |
822 | // existence, this means failure. | |
823 | if ( sig == wxSIGNONE ) | |
50567b69 | 824 | { |
e436c454 VZ |
825 | if ( krc ) |
826 | *krc = wxKILL_NO_PROCESS; | |
827 | ||
828 | ok = false; | |
50567b69 VZ |
829 | } |
830 | break; | |
831 | ||
832 | default: | |
9a83f860 | 833 | wxFAIL_MSG( wxT("unexpected WaitForSingleObject() return") ); |
50567b69 VZ |
834 | // fall through |
835 | ||
836 | case WAIT_FAILED: | |
9a83f860 | 837 | wxLogLastError(wxT("WaitForSingleObject")); |
50567b69 VZ |
838 | // fall through |
839 | ||
840 | case WAIT_TIMEOUT: | |
e436c454 VZ |
841 | // Process didn't terminate: normally this is a failure but not |
842 | // when we're just testing for its existence. | |
843 | if ( sig != wxSIGNONE ) | |
844 | { | |
845 | if ( krc ) | |
846 | *krc = wxKILL_ERROR; | |
50567b69 | 847 | |
e436c454 VZ |
848 | ok = false; |
849 | } | |
50567b69 | 850 | break; |
e949bf13 JS |
851 | } |
852 | } | |
853 | ||
b568d04f | 854 | |
50567b69 VZ |
855 | // the return code is the same as from Unix kill(): 0 if killed |
856 | // successfully or -1 on error | |
e436c454 | 857 | if ( !ok ) |
a45fb5b4 | 858 | return -1; |
b1a28eef | 859 | |
a45fb5b4 VZ |
860 | if ( krc ) |
861 | *krc = wxKILL_OK; | |
50567b69 | 862 | |
a45fb5b4 | 863 | return 0; |
2bda0e17 KB |
864 | } |
865 | ||
78a054f6 WS |
866 | typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_t)(DWORD,DWORD); |
867 | typedef BOOL (WINAPI *Process32_t)(HANDLE,LPPROCESSENTRY32); | |
868 | ||
869 | CreateToolhelp32Snapshot_t lpfCreateToolhelp32Snapshot; | |
870 | Process32_t lpfProcess32First, lpfProcess32Next; | |
9d8aca48 | 871 | |
e0f6b731 JS |
872 | static void InitToolHelp32() |
873 | { | |
874 | static bool s_initToolHelpDone = false; | |
9d8aca48 | 875 | |
e0f6b731 JS |
876 | if (s_initToolHelpDone) |
877 | return; | |
9d8aca48 | 878 | |
e0f6b731 JS |
879 | s_initToolHelpDone = true; |
880 | ||
881 | lpfCreateToolhelp32Snapshot = NULL; | |
882 | lpfProcess32First = NULL; | |
883 | lpfProcess32Next = NULL; | |
9d8aca48 | 884 | |
32c0c61c WS |
885 | #if wxUSE_DYNLIB_CLASS |
886 | ||
9a83f860 | 887 | wxDynamicLibrary dllKernel(wxT("kernel32.dll"), wxDL_VERBATIM); |
9d8aca48 | 888 | |
e0f6b731 JS |
889 | // Get procedure addresses. |
890 | // We are linking to these functions of Kernel32 | |
891 | // explicitly, because otherwise a module using | |
892 | // this code would fail to load under Windows NT, | |
893 | // which does not have the Toolhelp32 | |
894 | // functions in the Kernel 32. | |
78a054f6 | 895 | lpfCreateToolhelp32Snapshot = |
9a83f860 | 896 | (CreateToolhelp32Snapshot_t)dllKernel.RawGetSymbol(wxT("CreateToolhelp32Snapshot")); |
9d8aca48 | 897 | |
78a054f6 | 898 | lpfProcess32First = |
9a83f860 | 899 | (Process32_t)dllKernel.RawGetSymbol(wxT("Process32First")); |
e0f6b731 | 900 | |
78a054f6 | 901 | lpfProcess32Next = |
9a83f860 | 902 | (Process32_t)dllKernel.RawGetSymbol(wxT("Process32Next")); |
32c0c61c WS |
903 | |
904 | #endif // wxUSE_DYNLIB_CLASS | |
e0f6b731 JS |
905 | } |
906 | ||
907 | // By John Skiff | |
908 | int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc) | |
909 | { | |
910 | InitToolHelp32(); | |
9d8aca48 | 911 | |
e0f6b731 JS |
912 | if (krc) |
913 | *krc = wxKILL_OK; | |
9d8aca48 | 914 | |
e0f6b731 JS |
915 | // If not implemented for this platform (e.g. NT 4.0), silently ignore |
916 | if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next) | |
917 | return 0; | |
9d8aca48 | 918 | |
e0f6b731 JS |
919 | // Take a snapshot of all processes in the system. |
920 | HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | |
921 | if (hProcessSnap == INVALID_HANDLE_VALUE) { | |
922 | if (krc) | |
923 | *krc = wxKILL_ERROR; | |
924 | return -1; | |
925 | } | |
9d8aca48 | 926 | |
e0f6b731 | 927 | //Fill in the size of the structure before using it. |
04a18b0d WS |
928 | PROCESSENTRY32 pe; |
929 | wxZeroMemory(pe); | |
e0f6b731 | 930 | pe.dwSize = sizeof(PROCESSENTRY32); |
9d8aca48 | 931 | |
e0f6b731 JS |
932 | // Walk the snapshot of the processes, and for each process, |
933 | // kill it if its parent is pid. | |
934 | if (!lpfProcess32First(hProcessSnap, &pe)) { | |
935 | // Can't get first process. | |
936 | if (krc) | |
937 | *krc = wxKILL_ERROR; | |
938 | CloseHandle (hProcessSnap); | |
939 | return -1; | |
940 | } | |
9d8aca48 | 941 | |
e0f6b731 JS |
942 | do { |
943 | if (pe.th32ParentProcessID == (DWORD) pid) { | |
944 | if (wxKill(pe.th32ProcessID, sig, krc)) | |
945 | return -1; | |
946 | } | |
947 | } while (lpfProcess32Next (hProcessSnap, &pe)); | |
9d8aca48 WS |
948 | |
949 | ||
e0f6b731 JS |
950 | return 0; |
951 | } | |
952 | ||
b568d04f VZ |
953 | // Execute a program in an Interactive Shell |
954 | bool wxShell(const wxString& command) | |
2bda0e17 | 955 | { |
7010702f WS |
956 | wxString cmd; |
957 | ||
4676948b | 958 | #ifdef __WXWINCE__ |
7010702f | 959 | cmd = command; |
4676948b | 960 | #else |
b568d04f VZ |
961 | wxChar *shell = wxGetenv(wxT("COMSPEC")); |
962 | if ( !shell ) | |
ba14d986 | 963 | shell = (wxChar*) wxT("\\COMMAND.COM"); |
b568d04f | 964 | |
b568d04f VZ |
965 | if ( !command ) |
966 | { | |
967 | // just the shell | |
968 | cmd = shell; | |
969 | } | |
970 | else | |
971 | { | |
972 | // pass the command to execute to the command processor | |
973 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
974 | } | |
7010702f | 975 | #endif |
b568d04f | 976 | |
011a524e | 977 | return wxExecute(cmd, wxEXEC_SYNC) == 0; |
2bda0e17 KB |
978 | } |
979 | ||
d8c65cf4 | 980 | // Shutdown or reboot the PC |
118a41d9 | 981 | bool wxShutdown(int WXUNUSED_IN_WINCE(flags)) |
f6ba47d9 | 982 | { |
4676948b | 983 | #ifdef __WXWINCE__ |
040e5f77 | 984 | // TODO-CE |
27d2dbbc | 985 | return false; |
4676948b | 986 | #elif defined(__WIN32__) |
27d2dbbc | 987 | bool bOK = true; |
f6ba47d9 | 988 | |
406d283a | 989 | if ( wxGetOsVersion(NULL, NULL) == wxOS_WINDOWS_NT ) // if is NT or 2K |
f6ba47d9 | 990 | { |
d8c65cf4 RD |
991 | // Get a token for this process. |
992 | HANDLE hToken; | |
f6ba47d9 VZ |
993 | bOK = ::OpenProcessToken(GetCurrentProcess(), |
994 | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, | |
995 | &hToken) != 0; | |
996 | if ( bOK ) | |
997 | { | |
d8c65cf4 | 998 | TOKEN_PRIVILEGES tkp; |
f6ba47d9 | 999 | |
d8c65cf4 | 1000 | // Get the LUID for the shutdown privilege. |
a2e50fc2 VZ |
1001 | bOK = ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, |
1002 | &tkp.Privileges[0].Luid) != 0; | |
f6ba47d9 | 1003 | |
a2e50fc2 VZ |
1004 | if ( bOK ) |
1005 | { | |
1006 | tkp.PrivilegeCount = 1; // one privilege to set | |
1007 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
1008 | ||
1009 | // Get the shutdown privilege for this process. | |
1010 | ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, | |
1011 | (PTOKEN_PRIVILEGES)NULL, 0); | |
f6ba47d9 | 1012 | |
a2e50fc2 VZ |
1013 | // Cannot test the return value of AdjustTokenPrivileges. |
1014 | bOK = ::GetLastError() == ERROR_SUCCESS; | |
1015 | } | |
f6ba47d9 | 1016 | |
a2e50fc2 | 1017 | ::CloseHandle(hToken); |
f6ba47d9 VZ |
1018 | } |
1019 | } | |
1020 | ||
1021 | if ( bOK ) | |
1022 | { | |
118a41d9 VZ |
1023 | UINT wFlags = 0; |
1024 | if ( flags & wxSHUTDOWN_FORCE ) | |
1025 | { | |
1026 | wFlags = EWX_FORCE; | |
1027 | flags &= ~wxSHUTDOWN_FORCE; | |
1028 | } | |
1029 | ||
1030 | switch ( flags ) | |
f6ba47d9 VZ |
1031 | { |
1032 | case wxSHUTDOWN_POWEROFF: | |
118a41d9 | 1033 | wFlags |= EWX_POWEROFF; |
f6ba47d9 VZ |
1034 | break; |
1035 | ||
1036 | case wxSHUTDOWN_REBOOT: | |
118a41d9 VZ |
1037 | wFlags |= EWX_REBOOT; |
1038 | break; | |
1039 | ||
1040 | case wxSHUTDOWN_LOGOFF: | |
1041 | wFlags |= EWX_LOGOFF; | |
f6ba47d9 VZ |
1042 | break; |
1043 | ||
1044 | default: | |
9a83f860 | 1045 | wxFAIL_MSG( wxT("unknown wxShutdown() flag") ); |
27d2dbbc | 1046 | return false; |
f6ba47d9 VZ |
1047 | } |
1048 | ||
118a41d9 | 1049 | bOK = ::ExitWindowsEx(wFlags, 0) != 0; |
f6ba47d9 VZ |
1050 | } |
1051 | ||
1052 | return bOK; | |
118a41d9 | 1053 | #endif // WinCE/!WinCE |
f6ba47d9 VZ |
1054 | } |
1055 | ||
b568d04f VZ |
1056 | // ---------------------------------------------------------------------------- |
1057 | // misc | |
1058 | // ---------------------------------------------------------------------------- | |
1059 | ||
1060 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
9d8aca48 | 1061 | wxMemorySize wxGetFreeMemory() |
2bda0e17 | 1062 | { |
9d8aca48 WS |
1063 | #if defined(__WIN64__) |
1064 | MEMORYSTATUSEX memStatex; | |
106d80ad JS |
1065 | memStatex.dwLength = sizeof (memStatex); |
1066 | ::GlobalMemoryStatusEx (&memStatex); | |
1067 | return (wxMemorySize)memStatex.ullAvailPhys; | |
2b7295c9 | 1068 | #else /* if defined(__WIN32__) */ |
b568d04f VZ |
1069 | MEMORYSTATUS memStatus; |
1070 | memStatus.dwLength = sizeof(MEMORYSTATUS); | |
2b7295c9 | 1071 | ::GlobalMemoryStatus(&memStatus); |
9d8aca48 | 1072 | return (wxMemorySize)memStatus.dwAvailPhys; |
b568d04f | 1073 | #endif |
2bda0e17 KB |
1074 | } |
1075 | ||
c1cb4153 VZ |
1076 | unsigned long wxGetProcessId() |
1077 | { | |
c1cb4153 | 1078 | return ::GetCurrentProcessId(); |
c1cb4153 VZ |
1079 | } |
1080 | ||
2bda0e17 | 1081 | // Emit a beeeeeep |
634903fd | 1082 | void wxBell() |
2bda0e17 | 1083 | { |
b568d04f | 1084 | ::MessageBeep((UINT)-1); // default sound |
2bda0e17 KB |
1085 | } |
1086 | ||
c50a4038 VZ |
1087 | bool wxIsDebuggerRunning() |
1088 | { | |
532d575b | 1089 | #if wxUSE_DYNLIB_CLASS |
c50a4038 | 1090 | // IsDebuggerPresent() is not available under Win95, so load it dynamically |
9a83f860 | 1091 | wxDynamicLibrary dll(wxT("kernel32.dll"), wxDL_VERBATIM); |
c50a4038 VZ |
1092 | |
1093 | typedef BOOL (WINAPI *IsDebuggerPresent_t)(); | |
9a83f860 | 1094 | if ( !dll.HasSymbol(wxT("IsDebuggerPresent")) ) |
c50a4038 VZ |
1095 | { |
1096 | // no way to know, assume no | |
1097 | return false; | |
1098 | } | |
1099 | ||
9a83f860 | 1100 | return (*(IsDebuggerPresent_t)dll.GetSymbol(wxT("IsDebuggerPresent")))() != 0; |
532d575b WS |
1101 | #else |
1102 | return false; | |
1103 | #endif | |
c50a4038 VZ |
1104 | } |
1105 | ||
c5ba4851 VZ |
1106 | // ---------------------------------------------------------------------------- |
1107 | // working with MSW resources | |
1108 | // ---------------------------------------------------------------------------- | |
1109 | ||
1110 | bool | |
1111 | wxLoadUserResource(const void **outData, | |
1112 | size_t *outLen, | |
1113 | const wxString& resourceName, | |
18923e36 VS |
1114 | const wxString& resourceType, |
1115 | WXHINSTANCE instance) | |
c5ba4851 VZ |
1116 | { |
1117 | wxCHECK_MSG( outData && outLen, false, "output pointers can't be NULL" ); | |
1118 | ||
18923e36 | 1119 | HRSRC hResource = ::FindResource(instance, |
c5ba4851 VZ |
1120 | resourceName.wx_str(), |
1121 | resourceType.wx_str()); | |
1122 | if ( !hResource ) | |
1123 | return false; | |
1124 | ||
18923e36 | 1125 | HGLOBAL hData = ::LoadResource(instance, hResource); |
c5ba4851 VZ |
1126 | if ( !hData ) |
1127 | { | |
1128 | wxLogSysError(_("Failed to load resource \"%s\"."), resourceName); | |
1129 | return false; | |
1130 | } | |
1131 | ||
1132 | *outData = ::LockResource(hData); | |
1133 | if ( !*outData ) | |
1134 | { | |
1135 | wxLogSysError(_("Failed to lock resource \"%s\"."), resourceName); | |
1136 | return false; | |
1137 | } | |
1138 | ||
18923e36 | 1139 | *outLen = ::SizeofResource(instance, hResource); |
c5ba4851 VZ |
1140 | |
1141 | // Notice that we do not need to call neither UnlockResource() (which is | |
1142 | // obsolete in Win32) nor GlobalFree() (resources are freed on process | |
1143 | // termination only) | |
1144 | ||
1145 | return true; | |
1146 | } | |
1147 | ||
1148 | char * | |
1149 | wxLoadUserResource(const wxString& resourceName, | |
1150 | const wxString& resourceType, | |
18923e36 VS |
1151 | int* pLen, |
1152 | WXHINSTANCE instance) | |
c5ba4851 VZ |
1153 | { |
1154 | const void *data; | |
1155 | size_t len; | |
18923e36 | 1156 | if ( !wxLoadUserResource(&data, &len, resourceName, resourceType, instance) ) |
c5ba4851 VZ |
1157 | return NULL; |
1158 | ||
1159 | char *s = new char[len + 1]; | |
1160 | memcpy(s, data, len); | |
1161 | s[len] = '\0'; // NUL-terminate in case the resource itself wasn't | |
1162 | ||
1163 | if (pLen) | |
1164 | *pLen = len; | |
1165 | ||
1166 | return s; | |
1167 | } | |
1168 | ||
c50a4038 VZ |
1169 | // ---------------------------------------------------------------------------- |
1170 | // OS version | |
1171 | // ---------------------------------------------------------------------------- | |
1172 | ||
66930b35 VZ |
1173 | // check if we're running under a server or workstation Windows system: it |
1174 | // returns true or false with obvious meaning as well as -1 if the system type | |
1175 | // couldn't be determined | |
1176 | // | |
1177 | // this function is currently private but we may want to expose it later if | |
1178 | // it's really useful | |
1179 | namespace | |
1180 | { | |
1181 | ||
1182 | int wxIsWindowsServer() | |
1183 | { | |
1184 | #ifdef VER_NT_WORKSTATION | |
1185 | OSVERSIONINFOEX info; | |
1186 | wxZeroMemory(info); | |
1187 | ||
1188 | info.dwOSVersionInfoSize = sizeof(info); | |
5c33522f | 1189 | if ( ::GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&info)) ) |
66930b35 VZ |
1190 | { |
1191 | switch ( info.wProductType ) | |
1192 | { | |
1193 | case VER_NT_WORKSTATION: | |
1194 | return false; | |
1195 | ||
1196 | case VER_NT_SERVER: | |
1197 | case VER_NT_DOMAIN_CONTROLLER: | |
1198 | return true; | |
1199 | } | |
1200 | } | |
1201 | #endif // VER_NT_WORKSTATION | |
1202 | ||
1203 | return -1; | |
1204 | } | |
1205 | ||
1206 | } // anonymous namespace | |
1207 | ||
bdc72a22 | 1208 | wxString wxGetOsDescription() |
2bda0e17 | 1209 | { |
bdc72a22 VZ |
1210 | wxString str; |
1211 | ||
1212 | OSVERSIONINFO info; | |
1213 | wxZeroMemory(info); | |
1214 | ||
1215 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
1216 | if ( ::GetVersionEx(&info) ) | |
1217 | { | |
1218 | switch ( info.dwPlatformId ) | |
1219 | { | |
84dc0006 WS |
1220 | #ifdef VER_PLATFORM_WIN32_CE |
1221 | case VER_PLATFORM_WIN32_CE: | |
1222 | str.Printf(_("Windows CE (%d.%d)"), | |
1223 | info.dwMajorVersion, | |
1224 | info.dwMinorVersion); | |
1225 | break; | |
1226 | #endif | |
bdc72a22 VZ |
1227 | case VER_PLATFORM_WIN32s: |
1228 | str = _("Win32s on Windows 3.1"); | |
1229 | break; | |
1230 | ||
1231 | case VER_PLATFORM_WIN32_WINDOWS: | |
db1d0193 VZ |
1232 | switch (info.dwMinorVersion) |
1233 | { | |
1234 | case 0: | |
1235 | if ( info.szCSDVersion[1] == 'B' || | |
1236 | info.szCSDVersion[1] == 'C' ) | |
1237 | { | |
1238 | str = _("Windows 95 OSR2"); | |
1239 | } | |
1240 | else | |
1241 | { | |
1242 | str = _("Windows 95"); | |
1243 | } | |
1244 | break; | |
1245 | case 10: | |
1246 | if ( info.szCSDVersion[1] == 'B' || | |
1247 | info.szCSDVersion[1] == 'C' ) | |
1248 | { | |
1249 | str = _("Windows 98 SE"); | |
1250 | } | |
1251 | else | |
1252 | { | |
1253 | str = _("Windows 98"); | |
1254 | } | |
1255 | break; | |
1256 | case 90: | |
1257 | str = _("Windows ME"); | |
1258 | break; | |
1259 | default: | |
1260 | str.Printf(_("Windows 9x (%d.%d)"), | |
1261 | info.dwMajorVersion, | |
1262 | info.dwMinorVersion); | |
1263 | break; | |
1264 | } | |
bdc72a22 VZ |
1265 | if ( !wxIsEmpty(info.szCSDVersion) ) |
1266 | { | |
9a83f860 | 1267 | str << wxT(" (") << info.szCSDVersion << wxT(')'); |
bdc72a22 VZ |
1268 | } |
1269 | break; | |
1270 | ||
1271 | case VER_PLATFORM_WIN32_NT: | |
a284cce8 | 1272 | switch ( info.dwMajorVersion ) |
db1d0193 | 1273 | { |
a284cce8 VZ |
1274 | case 5: |
1275 | switch ( info.dwMinorVersion ) | |
1276 | { | |
1277 | case 0: | |
5f081e28 | 1278 | str = _("Windows 2000"); |
a284cce8 VZ |
1279 | break; |
1280 | ||
66930b35 VZ |
1281 | case 2: |
1282 | // we can't distinguish between XP 64 and 2003 | |
1283 | // as they both are 5.2, so examine the product | |
1284 | // type to resolve this ambiguity | |
575cabba | 1285 | if ( wxIsWindowsServer() == 1 ) |
66930b35 | 1286 | { |
5f081e28 | 1287 | str = _("Windows Server 2003"); |
66930b35 VZ |
1288 | break; |
1289 | } | |
1290 | //else: must be XP, fall through | |
1291 | ||
a284cce8 | 1292 | case 1: |
5f081e28 | 1293 | str = _("Windows XP"); |
a284cce8 | 1294 | break; |
a284cce8 VZ |
1295 | } |
1296 | break; | |
1297 | ||
1298 | case 6: | |
7c7eed56 | 1299 | switch ( info.dwMinorVersion ) |
a284cce8 | 1300 | { |
7c7eed56 | 1301 | case 0: |
5f081e28 VZ |
1302 | str = wxIsWindowsServer() == 1 |
1303 | ? _("Windows Server 2008") | |
1304 | : _("Windows Vista"); | |
7c7eed56 VZ |
1305 | break; |
1306 | ||
1307 | case 1: | |
5f081e28 VZ |
1308 | str = wxIsWindowsServer() == 1 |
1309 | ? _("Windows Server 2008 R2") | |
1310 | : _("Windows 7"); | |
7c7eed56 | 1311 | break; |
a284cce8 VZ |
1312 | } |
1313 | break; | |
db1d0193 | 1314 | } |
a284cce8 | 1315 | |
84dc0006 | 1316 | if ( str.empty() ) |
db1d0193 | 1317 | { |
5f081e28 VZ |
1318 | str.Printf(_("Windows NT %lu.%lu"), |
1319 | info.dwMajorVersion, | |
1320 | info.dwMinorVersion); | |
db1d0193 | 1321 | } |
a284cce8 | 1322 | |
5f081e28 VZ |
1323 | str << wxT(" (") |
1324 | << wxString::Format(_("build %lu"), info.dwBuildNumber); | |
bdc72a22 VZ |
1325 | if ( !wxIsEmpty(info.szCSDVersion) ) |
1326 | { | |
9a83f860 | 1327 | str << wxT(", ") << info.szCSDVersion; |
bdc72a22 | 1328 | } |
9a83f860 | 1329 | str << wxT(')'); |
d4885e10 VZ |
1330 | |
1331 | if ( wxIsPlatform64Bit() ) | |
1332 | str << _(", 64-bit edition"); | |
bdc72a22 VZ |
1333 | break; |
1334 | } | |
1335 | } | |
1336 | else | |
1337 | { | |
9a83f860 | 1338 | wxFAIL_MSG( wxT("GetVersionEx() failed") ); // should never happen |
bdc72a22 VZ |
1339 | } |
1340 | ||
1341 | return str; | |
bdc72a22 | 1342 | } |
6f65e337 | 1343 | |
8bb6b2c0 | 1344 | bool wxIsPlatform64Bit() |
bdc72a22 | 1345 | { |
8bb6b2c0 VZ |
1346 | #if defined(_WIN64) |
1347 | return true; // 64-bit programs run only on Win64 | |
a8ff046b | 1348 | #elif wxUSE_DYNLIB_CLASS // Win32 |
6deb397d VZ |
1349 | // 32-bit programs run on both 32-bit and 64-bit Windows so check |
1350 | typedef BOOL (WINAPI *IsWow64Process_t)(HANDLE, BOOL *); | |
1351 | ||
9a83f860 | 1352 | wxDynamicLibrary dllKernel32(wxT("kernel32.dll")); |
b9d4a1b7 | 1353 | IsWow64Process_t pfnIsWow64Process = |
9a83f860 | 1354 | (IsWow64Process_t)dllKernel32.RawGetSymbol(wxT("IsWow64Process")); |
6deb397d VZ |
1355 | |
1356 | BOOL wow64 = FALSE; | |
1357 | if ( pfnIsWow64Process ) | |
1358 | { | |
b9d4a1b7 | 1359 | pfnIsWow64Process(::GetCurrentProcess(), &wow64); |
6deb397d VZ |
1360 | } |
1361 | //else: running under a system without Win64 support | |
1362 | ||
1363 | return wow64 != FALSE; | |
a8ff046b VZ |
1364 | #else |
1365 | return false; | |
6deb397d | 1366 | #endif // Win64/Win32 |
8bb6b2c0 | 1367 | } |
ec5d7799 | 1368 | |
8bb6b2c0 VZ |
1369 | wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) |
1370 | { | |
5bc3ef05 VZ |
1371 | static struct |
1372 | { | |
1373 | // this may be false, true or -1 if we tried to initialize but failed | |
1374 | int initialized; | |
bdc72a22 | 1375 | |
5bc3ef05 VZ |
1376 | wxOperatingSystemId os; |
1377 | ||
1378 | int verMaj, | |
1379 | verMin; | |
1380 | } s_version; | |
1381 | ||
1382 | // query the OS info only once as it's not supposed to change | |
1383 | if ( !s_version.initialized ) | |
8bb6b2c0 | 1384 | { |
5bc3ef05 VZ |
1385 | OSVERSIONINFO info; |
1386 | wxZeroMemory(info); | |
1387 | info.dwOSVersionInfoSize = sizeof(info); | |
1388 | if ( ::GetVersionEx(&info) ) | |
1389 | { | |
1390 | s_version.initialized = true; | |
1391 | ||
1392 | #if defined(__WXWINCE__) | |
1393 | s_version.os = wxOS_WINDOWS_CE; | |
1394 | #elif defined(__WXMICROWIN__) | |
1395 | s_version.os = wxOS_WINDOWS_MICRO; | |
1396 | #else // "normal" desktop Windows system, use run-time detection | |
1397 | switch ( info.dwPlatformId ) | |
1398 | { | |
1399 | case VER_PLATFORM_WIN32_NT: | |
1400 | s_version.os = wxOS_WINDOWS_NT; | |
1401 | break; | |
1402 | ||
1403 | case VER_PLATFORM_WIN32_WINDOWS: | |
1404 | s_version.os = wxOS_WINDOWS_9X; | |
1405 | break; | |
1406 | } | |
1407 | #endif // Windows versions | |
1408 | ||
1409 | s_version.verMaj = info.dwMajorVersion; | |
1410 | s_version.verMin = info.dwMinorVersion; | |
1411 | } | |
1412 | else // GetVersionEx() failed | |
1413 | { | |
1414 | s_version.initialized = -1; | |
1415 | } | |
8bb6b2c0 | 1416 | } |
bdc72a22 | 1417 | |
5bc3ef05 | 1418 | if ( s_version.initialized == 1 ) |
8bb6b2c0 | 1419 | { |
5bc3ef05 VZ |
1420 | if ( verMaj ) |
1421 | *verMaj = s_version.verMaj; | |
1422 | if ( verMin ) | |
1423 | *verMin = s_version.verMin; | |
bdc72a22 VZ |
1424 | } |
1425 | ||
5bc3ef05 VZ |
1426 | // this works even if we were not initialized successfully as the initial |
1427 | // values of this field is 0 which is wxOS_UNKNOWN and exactly what we need | |
1428 | return s_version.os; | |
2bda0e17 KB |
1429 | } |
1430 | ||
4c5da5e4 VZ |
1431 | wxWinVersion wxGetWinVersion() |
1432 | { | |
1433 | int verMaj, | |
1434 | verMin; | |
1435 | switch ( wxGetOsVersion(&verMaj, &verMin) ) | |
1436 | { | |
406d283a | 1437 | case wxOS_WINDOWS_9X: |
4c5da5e4 VZ |
1438 | if ( verMaj == 4 ) |
1439 | { | |
1440 | switch ( verMin ) | |
1441 | { | |
1442 | case 0: | |
1443 | return wxWinVersion_95; | |
1444 | ||
1445 | case 10: | |
1446 | return wxWinVersion_98; | |
1447 | ||
1448 | case 90: | |
1449 | return wxWinVersion_ME; | |
1450 | } | |
1451 | } | |
1452 | break; | |
1453 | ||
406d283a | 1454 | case wxOS_WINDOWS_NT: |
4c5da5e4 VZ |
1455 | switch ( verMaj ) |
1456 | { | |
1457 | case 3: | |
1458 | return wxWinVersion_NT3; | |
1459 | ||
1460 | case 4: | |
1461 | return wxWinVersion_NT4; | |
1462 | ||
1463 | case 5: | |
1464 | switch ( verMin ) | |
1465 | { | |
1466 | case 0: | |
1467 | return wxWinVersion_2000; | |
1468 | ||
1469 | case 1: | |
1470 | return wxWinVersion_XP; | |
1471 | ||
1472 | case 2: | |
1473 | return wxWinVersion_2003; | |
1474 | } | |
1475 | break; | |
1476 | ||
1477 | case 6: | |
1478 | return wxWinVersion_NT6; | |
1479 | } | |
1480 | break; | |
1481 | ||
449210a4 WS |
1482 | default: |
1483 | // Do nothing just to silence GCC warning | |
1484 | break; | |
4c5da5e4 VZ |
1485 | } |
1486 | ||
1487 | return wxWinVersion_Unknown; | |
1488 | } | |
1489 | ||
b568d04f VZ |
1490 | // ---------------------------------------------------------------------------- |
1491 | // sleep functions | |
1492 | // ---------------------------------------------------------------------------- | |
1493 | ||
08873d36 | 1494 | void wxMilliSleep(unsigned long milliseconds) |
b568d04f VZ |
1495 | { |
1496 | ::Sleep(milliseconds); | |
1497 | } | |
1498 | ||
08873d36 VZ |
1499 | void wxMicroSleep(unsigned long microseconds) |
1500 | { | |
1501 | wxMilliSleep(microseconds/1000); | |
1502 | } | |
1503 | ||
b568d04f VZ |
1504 | void wxSleep(int nSecs) |
1505 | { | |
08873d36 | 1506 | wxMilliSleep(1000*nSecs); |
b568d04f VZ |
1507 | } |
1508 | ||
b568d04f | 1509 | // ---------------------------------------------------------------------------- |
e2478fde | 1510 | // font encoding <-> Win32 codepage conversion functions |
b568d04f VZ |
1511 | // ---------------------------------------------------------------------------- |
1512 | ||
bddd7a8d | 1513 | extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding) |
b568d04f | 1514 | { |
e2478fde | 1515 | switch ( encoding ) |
b568d04f | 1516 | { |
e2478fde VZ |
1517 | // although this function is supposed to return an exact match, do do |
1518 | // some mappings here for the most common case of "standard" encoding | |
1519 | case wxFONTENCODING_SYSTEM: | |
1520 | return DEFAULT_CHARSET; | |
2bda0e17 | 1521 | |
e2478fde VZ |
1522 | case wxFONTENCODING_ISO8859_1: |
1523 | case wxFONTENCODING_ISO8859_15: | |
1524 | case wxFONTENCODING_CP1252: | |
1525 | return ANSI_CHARSET; | |
2bda0e17 | 1526 | |
e2478fde VZ |
1527 | #if !defined(__WXMICROWIN__) |
1528 | // The following four fonts are multi-byte charsets | |
1529 | case wxFONTENCODING_CP932: | |
1530 | return SHIFTJIS_CHARSET; | |
2bda0e17 | 1531 | |
e2478fde VZ |
1532 | case wxFONTENCODING_CP936: |
1533 | return GB2312_CHARSET; | |
2bda0e17 | 1534 | |
90805c24 | 1535 | #ifndef __WXWINCE__ |
e2478fde VZ |
1536 | case wxFONTENCODING_CP949: |
1537 | return HANGUL_CHARSET; | |
90805c24 | 1538 | #endif |
634903fd | 1539 | |
e2478fde VZ |
1540 | case wxFONTENCODING_CP950: |
1541 | return CHINESEBIG5_CHARSET; | |
2bda0e17 | 1542 | |
e2478fde VZ |
1543 | // The rest are single byte encodings |
1544 | case wxFONTENCODING_CP1250: | |
1545 | return EASTEUROPE_CHARSET; | |
bfbd6dc1 | 1546 | |
e2478fde VZ |
1547 | case wxFONTENCODING_CP1251: |
1548 | return RUSSIAN_CHARSET; | |
2bda0e17 | 1549 | |
e2478fde VZ |
1550 | case wxFONTENCODING_CP1253: |
1551 | return GREEK_CHARSET; | |
b07135ba | 1552 | |
e2478fde VZ |
1553 | case wxFONTENCODING_CP1254: |
1554 | return TURKISH_CHARSET; | |
2bda0e17 | 1555 | |
e2478fde VZ |
1556 | case wxFONTENCODING_CP1255: |
1557 | return HEBREW_CHARSET; | |
2bda0e17 | 1558 | |
e2478fde VZ |
1559 | case wxFONTENCODING_CP1256: |
1560 | return ARABIC_CHARSET; | |
b568d04f | 1561 | |
e2478fde VZ |
1562 | case wxFONTENCODING_CP1257: |
1563 | return BALTIC_CHARSET; | |
634903fd | 1564 | |
e2478fde VZ |
1565 | case wxFONTENCODING_CP874: |
1566 | return THAI_CHARSET; | |
5acc0d5f | 1567 | #endif // !__WXMICROWIN__ |
cc2b7472 | 1568 | |
e2478fde VZ |
1569 | case wxFONTENCODING_CP437: |
1570 | return OEM_CHARSET; | |
ea28b885 VZ |
1571 | |
1572 | default: | |
1573 | // no way to translate this encoding into a Windows charset | |
69557827 | 1574 | return -1; |
e2478fde | 1575 | } |
373658eb | 1576 | } |
7f555861 | 1577 | |
e2478fde VZ |
1578 | // we have 2 versions of wxCharsetToCodepage(): the old one which directly |
1579 | // looks up the vlaues in the registry and the new one which is more | |
1580 | // politically correct and has more chances to work on other Windows versions | |
1581 | // as well but the old version is still needed for !wxUSE_FONTMAP case | |
1582 | #if wxUSE_FONTMAP | |
c030b70f | 1583 | |
373658eb | 1584 | #include "wx/fontmap.h" |
c030b70f | 1585 | |
bddd7a8d | 1586 | extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding) |
c030b70f | 1587 | { |
b169661f RR |
1588 | // There don't seem to be symbolic names for |
1589 | // these under Windows so I just copied the | |
1590 | // values from MSDN. | |
04a18b0d | 1591 | |
fa78de82 | 1592 | unsigned int ret; |
04a18b0d | 1593 | |
b169661f | 1594 | switch (encoding) |
373658eb | 1595 | { |
fa78de82 RR |
1596 | case wxFONTENCODING_ISO8859_1: ret = 28591; break; |
1597 | case wxFONTENCODING_ISO8859_2: ret = 28592; break; | |
1598 | case wxFONTENCODING_ISO8859_3: ret = 28593; break; | |
1599 | case wxFONTENCODING_ISO8859_4: ret = 28594; break; | |
1600 | case wxFONTENCODING_ISO8859_5: ret = 28595; break; | |
1601 | case wxFONTENCODING_ISO8859_6: ret = 28596; break; | |
1602 | case wxFONTENCODING_ISO8859_7: ret = 28597; break; | |
1603 | case wxFONTENCODING_ISO8859_8: ret = 28598; break; | |
1604 | case wxFONTENCODING_ISO8859_9: ret = 28599; break; | |
1605 | case wxFONTENCODING_ISO8859_10: ret = 28600; break; | |
5df3dbf9 | 1606 | case wxFONTENCODING_ISO8859_11: ret = 874; break; |
b169661f | 1607 | // case wxFONTENCODING_ISO8859_12, // doesn't exist currently, but put it |
04a18b0d | 1608 | case wxFONTENCODING_ISO8859_13: ret = 28603; break; |
5fdf0363 | 1609 | // case wxFONTENCODING_ISO8859_14: ret = 28604; break; // no correspondence on Windows |
fa78de82 | 1610 | case wxFONTENCODING_ISO8859_15: ret = 28605; break; |
bd5c3968 | 1611 | |
fa78de82 RR |
1612 | case wxFONTENCODING_KOI8: ret = 20866; break; |
1613 | case wxFONTENCODING_KOI8_U: ret = 21866; break; | |
bd5c3968 | 1614 | |
fa78de82 RR |
1615 | case wxFONTENCODING_CP437: ret = 437; break; |
1616 | case wxFONTENCODING_CP850: ret = 850; break; | |
1617 | case wxFONTENCODING_CP852: ret = 852; break; | |
1618 | case wxFONTENCODING_CP855: ret = 855; break; | |
1619 | case wxFONTENCODING_CP866: ret = 866; break; | |
1620 | case wxFONTENCODING_CP874: ret = 874; break; | |
1621 | case wxFONTENCODING_CP932: ret = 932; break; | |
1622 | case wxFONTENCODING_CP936: ret = 936; break; | |
1623 | case wxFONTENCODING_CP949: ret = 949; break; | |
1624 | case wxFONTENCODING_CP950: ret = 950; break; | |
1625 | case wxFONTENCODING_CP1250: ret = 1250; break; | |
1626 | case wxFONTENCODING_CP1251: ret = 1251; break; | |
1627 | case wxFONTENCODING_CP1252: ret = 1252; break; | |
1628 | case wxFONTENCODING_CP1253: ret = 1253; break; | |
1629 | case wxFONTENCODING_CP1254: ret = 1254; break; | |
1630 | case wxFONTENCODING_CP1255: ret = 1255; break; | |
1631 | case wxFONTENCODING_CP1256: ret = 1256; break; | |
1632 | case wxFONTENCODING_CP1257: ret = 1257; break; | |
bd5c3968 | 1633 | |
a50ca843 | 1634 | case wxFONTENCODING_EUC_JP: ret = 20932; break; |
bd5c3968 | 1635 | |
fa78de82 RR |
1636 | case wxFONTENCODING_MACROMAN: ret = 10000; break; |
1637 | case wxFONTENCODING_MACJAPANESE: ret = 10001; break; | |
1638 | case wxFONTENCODING_MACCHINESETRAD: ret = 10002; break; | |
1639 | case wxFONTENCODING_MACKOREAN: ret = 10003; break; | |
1640 | case wxFONTENCODING_MACARABIC: ret = 10004; break; | |
1641 | case wxFONTENCODING_MACHEBREW: ret = 10005; break; | |
1642 | case wxFONTENCODING_MACGREEK: ret = 10006; break; | |
1643 | case wxFONTENCODING_MACCYRILLIC: ret = 10007; break; | |
1644 | case wxFONTENCODING_MACTHAI: ret = 10021; break; | |
1645 | case wxFONTENCODING_MACCHINESESIMP: ret = 10008; break; | |
1646 | case wxFONTENCODING_MACCENTRALEUR: ret = 10029; break; | |
1647 | case wxFONTENCODING_MACCROATIAN: ret = 10082; break; | |
1648 | case wxFONTENCODING_MACICELANDIC: ret = 10079; break; | |
1649 | case wxFONTENCODING_MACROMANIAN: ret = 10009; break; | |
bd5c3968 VZ |
1650 | |
1651 | case wxFONTENCODING_ISO2022_JP: ret = 50222; break; | |
1652 | ||
fa78de82 RR |
1653 | case wxFONTENCODING_UTF7: ret = 65000; break; |
1654 | case wxFONTENCODING_UTF8: ret = 65001; break; | |
bd5c3968 | 1655 | |
fa78de82 | 1656 | default: return -1; |
373658eb | 1657 | } |
04a18b0d | 1658 | |
fa78de82 RR |
1659 | if (::IsValidCodePage(ret) == 0) |
1660 | return -1; | |
04a18b0d | 1661 | |
fa78de82 RR |
1662 | CPINFO info; |
1663 | if (::GetCPInfo(ret, &info) == 0) | |
1664 | return -1; | |
373658eb | 1665 | |
fa78de82 | 1666 | return (long) ret; |
c030b70f | 1667 | } |
373658eb | 1668 | |
86501081 | 1669 | extern long wxCharsetToCodepage(const char *name) |
c030b70f | 1670 | { |
373658eb VZ |
1671 | // first get the font encoding for this charset |
1672 | if ( !name ) | |
1673 | return -1; | |
1674 | ||
267e11c5 | 1675 | wxFontEncoding enc = wxFontMapperBase::Get()->CharsetToEncoding(name, false); |
373658eb VZ |
1676 | if ( enc == wxFONTENCODING_SYSTEM ) |
1677 | return -1; | |
1678 | ||
1679 | // the use the helper function | |
1680 | return wxEncodingToCodepage(enc); | |
c030b70f | 1681 | } |
c030b70f | 1682 | |
e2478fde | 1683 | #else // !wxUSE_FONTMAP |
373658eb VZ |
1684 | |
1685 | #include "wx/msw/registry.h" | |
1686 | ||
1687 | // this should work if Internet Exploiter is installed | |
86501081 | 1688 | extern long wxCharsetToCodepage(const char *name) |
04ef50df | 1689 | { |
373658eb VZ |
1690 | if (!name) |
1691 | return GetACP(); | |
1692 | ||
e2478fde | 1693 | long CP = -1; |
373658eb | 1694 | |
f77c0fe3 | 1695 | #if wxUSE_REGKEY |
e2478fde | 1696 | wxString path(wxT("MIME\\Database\\Charset\\")); |
373658eb | 1697 | wxString cn(name); |
373658eb | 1698 | |
e2478fde VZ |
1699 | // follow the alias loop |
1700 | for ( ;; ) | |
1701 | { | |
1702 | wxRegKey key(wxRegKey::HKCR, path + cn); | |
1703 | ||
1704 | if (!key.Exists()) | |
1705 | break; | |
373658eb VZ |
1706 | |
1707 | // two cases: either there's an AliasForCharset string, | |
1708 | // or there are Codepage and InternetEncoding dwords. | |
1709 | // The InternetEncoding gives us the actual encoding, | |
1710 | // the Codepage just says which Windows character set to | |
1711 | // use when displaying the data. | |
1712 | if (key.HasValue(wxT("InternetEncoding")) && | |
e2478fde VZ |
1713 | key.QueryValue(wxT("InternetEncoding"), &CP)) |
1714 | break; | |
373658eb VZ |
1715 | |
1716 | // no encoding, see if it's an alias | |
1717 | if (!key.HasValue(wxT("AliasForCharset")) || | |
e2478fde VZ |
1718 | !key.QueryValue(wxT("AliasForCharset"), cn)) |
1719 | break; | |
1720 | } | |
f77c0fe3 | 1721 | #endif // wxUSE_REGKEY |
373658eb VZ |
1722 | |
1723 | return CP; | |
04ef50df | 1724 | } |
373658eb | 1725 | |
e2478fde | 1726 | #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP |
c030b70f | 1727 | |
487f2d58 | 1728 | extern "C" WXDLLIMPEXP_BASE HWND |
eccd1992 VZ |
1729 | wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc) |
1730 | { | |
1731 | wxCHECK_MSG( classname && pclassname && wndproc, NULL, | |
9a83f860 | 1732 | wxT("NULL parameter in wxCreateHiddenWindow") ); |
eccd1992 VZ |
1733 | |
1734 | // register the class fi we need to first | |
1735 | if ( *pclassname == NULL ) | |
1736 | { | |
1737 | WNDCLASS wndclass; | |
1738 | wxZeroMemory(wndclass); | |
1739 | ||
1740 | wndclass.lpfnWndProc = wndproc; | |
1741 | wndclass.hInstance = wxGetInstance(); | |
1742 | wndclass.lpszClassName = classname; | |
1743 | ||
1744 | if ( !::RegisterClass(&wndclass) ) | |
1745 | { | |
1746 | wxLogLastError(wxT("RegisterClass() in wxCreateHiddenWindow")); | |
1747 | ||
1748 | return NULL; | |
1749 | } | |
1750 | ||
1751 | *pclassname = classname; | |
1752 | } | |
1753 | ||
1754 | // next create the window | |
1755 | HWND hwnd = ::CreateWindow | |
1756 | ( | |
1757 | *pclassname, | |
1758 | NULL, | |
1759 | 0, 0, 0, 0, | |
1760 | 0, | |
1761 | (HWND) NULL, | |
1762 | (HMENU)NULL, | |
1763 | wxGetInstance(), | |
1764 | (LPVOID) NULL | |
1765 | ); | |
1766 | ||
1767 | if ( !hwnd ) | |
1768 | { | |
1769 | wxLogLastError(wxT("CreateWindow() in wxCreateHiddenWindow")); | |
1770 | } | |
1771 | ||
1772 | return hwnd; | |
1773 | } |