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