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