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