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