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