]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utils.cpp
977ae5d5c39f29e5baeea2c28bf3a710c2b2be8b
[wxWidgets.git] / src / msw / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #endif //WX_PRECOMP
33
34 #include "wx/apptrait.h"
35 #include "wx/dynlib.h"
36 #include "wx/dynload.h"
37 #include "wx/scopeguard.h"
38
39 #include "wx/confbase.h" // for wxExpandEnvVars()
40
41 #include "wx/msw/private.h" // includes <windows.h>
42 #include "wx/msw/missing.h" // CHARSET_HANGUL
43
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
50 // Doesn't work with Cygwin at present
51 #if wxUSE_SOCKETS && (defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) || defined(__CYGWIN32__))
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
57 #include "wx/timer.h"
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 static const wxChar WX_SECTION[] = wxT("wxWindows");
114 static const wxChar eUSERNAME[] = wxT("UserName");
115
116 // ============================================================================
117 // implementation
118 // ============================================================================
119
120 // ----------------------------------------------------------------------------
121 // get host name and related
122 // ----------------------------------------------------------------------------
123
124 // Get hostname only (without domain name)
125 bool wxGetHostName(wxChar *WXUNUSED_IN_WINCE(buf),
126 int WXUNUSED_IN_WINCE(maxSize))
127 {
128 #if defined(__WXWINCE__)
129 // TODO-CE
130 return false;
131 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
132 DWORD nSize = maxSize;
133 if ( !::GetComputerName(buf, &nSize) )
134 {
135 wxLogLastError(wxT("GetComputerName"));
136
137 return false;
138 }
139
140 return true;
141 #else
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');
150 return *buf ? true : false;
151 #endif
152 }
153
154 // get full hostname (with domain name if possible)
155 bool wxGetFullHostName(wxChar *buf, int maxSize)
156 {
157 #if !defined( __WXMICROWIN__) && wxUSE_DYNAMIC_LOADER && wxUSE_SOCKETS
158 // TODO should use GetComputerNameEx() when available
159
160 // we don't want to always link with Winsock DLL as we might not use it at
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
165 wxDynamicLibrary dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM);
166 if ( dllWinsock.IsLoaded() )
167 {
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);
173
174 #define LOAD_WINSOCK_FUNC(func) \
175 func ## _t \
176 pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
177
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 )
190 {
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 }
217 }
218 }
219
220 LOAD_WINSOCK_FUNC(WSACleanup);
221 if ( pfnWSACleanup )
222 pfnWSACleanup();
223
224
225 if ( !host.empty() )
226 {
227 wxStrncpy(buf, host, maxSize);
228
229 return true;
230 }
231 }
232 }
233 #endif // !__WXMICROWIN__
234
235 return wxGetHostName(buf, maxSize);
236 }
237
238 // Get user ID e.g. jacs
239 bool wxGetUserId(wxChar *WXUNUSED_IN_WINCE(buf),
240 int WXUNUSED_IN_WINCE(maxSize))
241 {
242 #if defined(__WXWINCE__)
243 // TODO-CE
244 return false;
245 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
246 DWORD nSize = maxSize;
247 if ( ::GetUserName(buf, &nSize) == 0 )
248 {
249 // actually, it does happen on Win9x if the user didn't log on
250 DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize);
251 if ( res == 0 )
252 {
253 // not found
254 return false;
255 }
256 }
257
258 return true;
259 #else // __WXMICROWIN__
260 wxChar *user;
261 const wxChar *default_id = wxT("anonymous");
262
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 {
268 // Use wxWidgets configuration data (comming soon)
269 GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1);
270 }
271 else
272 {
273 wxStrncpy(buf, user, maxSize - 1);
274 }
275
276 return *buf ? true : false;
277 #endif
278 }
279
280 // Get user name e.g. Julian Smart
281 bool wxGetUserName(wxChar *WXUNUSED_IN_WINCE(buf),
282 int WXUNUSED_IN_WINCE(maxSize))
283 {
284 #if defined(__WXWINCE__)
285 // TODO-CE
286 return false;
287 #elif defined(USE_NET_API)
288 CHAR szUserName[256];
289 if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
290 return false;
291
292 // TODO how to get the domain name?
293 CHAR *szDomain = "";
294
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;
299
300 USER_INFO_2 *ui2; // User structure
301
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) );
307
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"));
312
313 goto error;
314 }
315
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;
326
327 case NERR_InvalidComputer:
328 wxLogError(wxT("Invalid domain controller name."));
329
330 goto error;
331
332 case NERR_UserNotFound:
333 wxLogError(wxT("Invalid user name '%s'."), szUserName);
334
335 goto error;
336
337 default:
338 wxLogSysError(wxT("Can't get information about user"));
339
340 goto error;
341 }
342
343 // Convert the Unicode full name to ANSI
344 WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
345 buf, maxSize, NULL, NULL );
346
347 return true;
348
349 error:
350 wxLogError(wxT("Couldn't look up full user name."));
351
352 return false;
353 #else // !USE_NET_API
354 // Could use NIS, MS-Mail or other site specific programs
355 // Use wxWidgets configuration data
356 bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxEmptyString, buf, maxSize - 1) != 0;
357 if ( !ok )
358 {
359 ok = wxGetUserId(buf, maxSize);
360 }
361
362 if ( !ok )
363 {
364 wxStrncpy(buf, wxT("Unknown User"), maxSize);
365 }
366
367 return true;
368 #endif // Win32/16
369 }
370
371 const wxChar* wxGetHomeDir(wxString *pstr)
372 {
373 wxString& strDir = *pstr;
374
375 // first branch is for Cygwin
376 #if defined(__UNIX__)
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;
385
386 // add a trailing slash if needed
387 if ( strDir.Last() != wxT('/') )
388 strDir << wxT('/');
389
390 #ifdef __CYGWIN__
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;
395 #endif
396 #elif defined(__WXWINCE__)
397 strDir = wxT("\\");
398 #else
399 strDir.clear();
400
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"));
404
405 if ( szHome != NULL )
406 {
407 strDir = szHome;
408 }
409 else // no HOME, try HOMEDRIVE/PATH
410 {
411 szHome = wxGetenv(wxT("HOMEDRIVE"));
412 if ( szHome != NULL )
413 strDir << szHome;
414 szHome = wxGetenv(wxT("HOMEPATH"));
415
416 if ( szHome != NULL )
417 {
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 )
427 strDir.clear();
428 }
429 }
430
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"));
436
437 if ( szHome != NULL )
438 strDir = szHome;
439 }
440
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 {
449 // extract the directory component of the program file name
450 wxSplitPath(wxGetFullModuleName(), &strDir, NULL, NULL);
451 }
452 #endif // UNIX/Win
453
454 return strDir.c_str();
455 }
456
457 wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
458 {
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;
463
464 return (wxChar *)wxGetHomeDir(&s_home);
465 }
466
467 bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path),
468 wxLongLong *WXUNUSED_IN_WINCE(pTotal),
469 wxLongLong *WXUNUSED_IN_WINCE(pFree))
470 {
471 #ifdef __WXWINCE__
472 // TODO-CE
473 return false;
474 #else
475 if ( path.empty() )
476 return false;
477
478 // old w32api don't have ULARGE_INTEGER
479 #if defined(__WIN32__) && \
480 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
481 // GetDiskFreeSpaceEx() is not available under original Win95, check for
482 // it
483 typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR,
484 PULARGE_INTEGER,
485 PULARGE_INTEGER,
486 PULARGE_INTEGER);
487
488 GetDiskFreeSpaceEx_t
489 pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress
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
511 return false;
512 }
513
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
522 if ( pTotal )
523 {
524 *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart);
525 }
526
527 if ( pFree )
528 {
529 *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart);
530 }
531 }
532 else
533 #endif // Win32
534 {
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
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
554 return false;
555 }
556
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
573 return true;
574 #endif
575 // __WXWINCE__
576 }
577
578 // ----------------------------------------------------------------------------
579 // env vars
580 // ----------------------------------------------------------------------------
581
582 bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var),
583 wxString *WXUNUSED_IN_WINCE(value))
584 {
585 #ifdef __WXWINCE__
586 // no environment variables under CE
587 return false;
588 #else // Win32
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
594 return false;
595 }
596
597 if ( value )
598 {
599 (void)::GetEnvironmentVariable(var, wxStringBuffer(*value, dwRet),
600 dwRet);
601 }
602
603 return true;
604 #endif // WinCE/32
605 }
606
607 bool wxSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
608 const wxChar *WXUNUSED_IN_WINCE(value))
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
612 #ifdef __WXWINCE__
613 // no environment variables under CE
614 return false;
615 #else
616 if ( !::SetEnvironmentVariable(var, value) )
617 {
618 wxLogLastError(_T("SetEnvironmentVariable"));
619
620 return false;
621 }
622
623 return true;
624 #endif
625 }
626
627 // ----------------------------------------------------------------------------
628 // process management
629 // ----------------------------------------------------------------------------
630
631 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
632 struct wxFindByPidParams
633 {
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;
641
642 DECLARE_NO_COPY_CLASS(wxFindByPidParams)
643 };
644
645 // wxKill helper: EnumWindows() callback which is used to find the first (top
646 // level) window belonging to the given process
647 BOOL 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 }
665
666 int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc);
667
668 int wxKill(long pid, wxSignal sig, wxKillError *krc, int flags)
669 {
670 if (flags & wxKILL_CHILDREN)
671 wxKillAllChildren(pid, sig, krc);
672
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 )
680 {
681 if ( krc )
682 {
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;
689 }
690
691 return -1;
692 }
693
694 wxON_BLOCK_EXIT1(::CloseHandle, hProcess);
695
696 bool ok = true;
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 )
706 {
707 // this is not supposed to happen if we could open the
708 // process
709 *krc = wxKILL_ERROR;
710 }
711
712 ok = false;
713 }
714 break;
715
716 case wxSIGNONE:
717 // do nothing, we just want to test for process existence
718 if ( krc )
719 *krc = wxKILL_OK;
720 return 0;
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
729 // something or if an error occurred and non zero if it
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 //
738 // NB: this is the harshest way, the app won't have an
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
751 ok = false;
752 }
753 }
754 else // no windows for this PID
755 {
756 if ( krc )
757 *krc = wxKILL_ERROR;
758
759 ok = false;
760 }
761 }
762 }
763
764 // the return code
765 DWORD rc wxDUMMY_INITIALIZE(0);
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 )
790 *krc = wxKILL_ERROR;
791
792 rc = STILL_ACTIVE;
793 break;
794 }
795 }
796
797
798 // the return code is the same as from Unix kill(): 0 if killed
799 // successfully or -1 on error
800 if ( !ok || rc == STILL_ACTIVE )
801 return -1;
802
803 if ( krc )
804 *krc = wxKILL_OK;
805
806 return 0;
807 }
808
809 typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_t)(DWORD,DWORD);
810 typedef BOOL (WINAPI *Process32_t)(HANDLE,LPPROCESSENTRY32);
811
812 CreateToolhelp32Snapshot_t lpfCreateToolhelp32Snapshot;
813 Process32_t lpfProcess32First, lpfProcess32Next;
814
815 static void InitToolHelp32()
816 {
817 static bool s_initToolHelpDone = false;
818
819 if (s_initToolHelpDone)
820 return;
821
822 s_initToolHelpDone = true;
823
824 lpfCreateToolhelp32Snapshot = NULL;
825 lpfProcess32First = NULL;
826 lpfProcess32Next = NULL;
827
828 wxDynamicLibrary dllKernel(_T("kernel32.dll"), wxDL_VERBATIM);
829
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 (CreateToolhelp32Snapshot_t)dllKernel.RawGetSymbol(_T("CreateToolhelp32Snapshot"));
838
839 lpfProcess32First =
840 (Process32_t)dllKernel.RawGetSymbol(_T("Process32First"));
841
842 lpfProcess32Next =
843 (Process32_t)dllKernel.RawGetSymbol(_T("Process32Next"));
844 }
845
846 // By John Skiff
847 int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc)
848 {
849 InitToolHelp32();
850
851 if (krc)
852 *krc = wxKILL_OK;
853
854 // If not implemented for this platform (e.g. NT 4.0), silently ignore
855 if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next)
856 return 0;
857
858 // Take a snapshot of all processes in the system.
859 HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
860 if (hProcessSnap == INVALID_HANDLE_VALUE) {
861 if (krc)
862 *krc = wxKILL_ERROR;
863 return -1;
864 }
865
866 //Fill in the size of the structure before using it.
867 PROCESSENTRY32 pe;
868 wxZeroMemory(pe);
869 pe.dwSize = sizeof(PROCESSENTRY32);
870
871 // Walk the snapshot of the processes, and for each process,
872 // kill it if its parent is pid.
873 if (!lpfProcess32First(hProcessSnap, &pe)) {
874 // Can't get first process.
875 if (krc)
876 *krc = wxKILL_ERROR;
877 CloseHandle (hProcessSnap);
878 return -1;
879 }
880
881 do {
882 if (pe.th32ParentProcessID == (DWORD) pid) {
883 if (wxKill(pe.th32ProcessID, sig, krc))
884 return -1;
885 }
886 } while (lpfProcess32Next (hProcessSnap, &pe));
887
888
889 return 0;
890 }
891
892 // Execute a program in an Interactive Shell
893 bool wxShell(const wxString& command)
894 {
895 wxString cmd;
896
897 #ifdef __WXWINCE__
898 cmd = command;
899 #else
900 wxChar *shell = wxGetenv(wxT("COMSPEC"));
901 if ( !shell )
902 shell = (wxChar*) wxT("\\COMMAND.COM");
903
904 if ( !command )
905 {
906 // just the shell
907 cmd = shell;
908 }
909 else
910 {
911 // pass the command to execute to the command processor
912 cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
913 }
914 #endif
915
916 return wxExecute(cmd, wxEXEC_SYNC) == 0;
917 }
918
919 // Shutdown or reboot the PC
920 bool wxShutdown(wxShutdownFlags WXUNUSED_IN_WINCE(wFlags))
921 {
922 #ifdef __WXWINCE__
923 // TODO-CE
924 return false;
925 #elif defined(__WIN32__)
926 bool bOK = true;
927
928 if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K
929 {
930 // Get a token for this process.
931 HANDLE hToken;
932 bOK = ::OpenProcessToken(GetCurrentProcess(),
933 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
934 &hToken) != 0;
935 if ( bOK )
936 {
937 TOKEN_PRIVILEGES tkp;
938
939 // Get the LUID for the shutdown privilege.
940 ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
941 &tkp.Privileges[0].Luid);
942
943 tkp.PrivilegeCount = 1; // one privilege to set
944 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
945
946 // Get the shutdown privilege for this process.
947 ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
948 (PTOKEN_PRIVILEGES)NULL, 0);
949
950 // Cannot test the return value of AdjustTokenPrivileges.
951 bOK = ::GetLastError() == ERROR_SUCCESS;
952 }
953 }
954
955 if ( bOK )
956 {
957 UINT flags = EWX_SHUTDOWN | EWX_FORCE;
958 switch ( wFlags )
959 {
960 case wxSHUTDOWN_POWEROFF:
961 flags |= EWX_POWEROFF;
962 break;
963
964 case wxSHUTDOWN_REBOOT:
965 flags |= EWX_REBOOT;
966 break;
967
968 default:
969 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
970 return false;
971 }
972
973 bOK = ::ExitWindowsEx(flags, 0) != 0;
974 }
975
976 return bOK;
977 #endif // Win32/16
978 }
979
980 wxPowerType wxGetPowerType()
981 {
982 // TODO
983 return wxPOWER_UNKNOWN;
984 }
985
986 wxBatteryState wxGetBatteryState()
987 {
988 // TODO
989 return wxBATTERY_UNKNOWN_STATE;
990 }
991
992 // ----------------------------------------------------------------------------
993 // misc
994 // ----------------------------------------------------------------------------
995
996 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
997 wxMemorySize wxGetFreeMemory()
998 {
999 #if defined(__WIN64__)
1000 MEMORYSTATUSEX memStatex;
1001 memStatex.dwLength = sizeof (memStatex);
1002 ::GlobalMemoryStatusEx (&memStatex);
1003 return (wxMemorySize)memStatex.ullAvailPhys;
1004 #else /* if defined(__WIN32__) */
1005 MEMORYSTATUS memStatus;
1006 memStatus.dwLength = sizeof(MEMORYSTATUS);
1007 ::GlobalMemoryStatus(&memStatus);
1008 return (wxMemorySize)memStatus.dwAvailPhys;
1009 #endif
1010 }
1011
1012 unsigned long wxGetProcessId()
1013 {
1014 return ::GetCurrentProcessId();
1015 }
1016
1017 // Emit a beeeeeep
1018 void wxBell()
1019 {
1020 ::MessageBeep((UINT)-1); // default sound
1021 }
1022
1023 bool wxIsDebuggerRunning()
1024 {
1025 #if wxUSE_DYNLIB_CLASS
1026 // IsDebuggerPresent() is not available under Win95, so load it dynamically
1027 wxDynamicLibrary dll(_T("kernel32.dll"), wxDL_VERBATIM);
1028
1029 typedef BOOL (WINAPI *IsDebuggerPresent_t)();
1030 if ( !dll.HasSymbol(_T("IsDebuggerPresent")) )
1031 {
1032 // no way to know, assume no
1033 return false;
1034 }
1035
1036 return (*(IsDebuggerPresent_t)dll.GetSymbol(_T("IsDebuggerPresent")))() != 0;
1037 #else
1038 return false;
1039 #endif
1040 }
1041
1042 // ----------------------------------------------------------------------------
1043 // OS version
1044 // ----------------------------------------------------------------------------
1045
1046 wxString wxGetOsDescription()
1047 {
1048 wxString str;
1049
1050 OSVERSIONINFO info;
1051 wxZeroMemory(info);
1052
1053 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1054 if ( ::GetVersionEx(&info) )
1055 {
1056 switch ( info.dwPlatformId )
1057 {
1058 case VER_PLATFORM_WIN32s:
1059 str = _("Win32s on Windows 3.1");
1060 break;
1061
1062 case VER_PLATFORM_WIN32_WINDOWS:
1063 switch (info.dwMinorVersion)
1064 {
1065 case 0:
1066 if ( info.szCSDVersion[1] == 'B' ||
1067 info.szCSDVersion[1] == 'C' )
1068 {
1069 str = _("Windows 95 OSR2");
1070 }
1071 else
1072 {
1073 str = _("Windows 95");
1074 }
1075 break;
1076 case 10:
1077 if ( info.szCSDVersion[1] == 'B' ||
1078 info.szCSDVersion[1] == 'C' )
1079 {
1080 str = _("Windows 98 SE");
1081 }
1082 else
1083 {
1084 str = _("Windows 98");
1085 }
1086 break;
1087 case 90:
1088 str = _("Windows ME");
1089 break;
1090 default:
1091 str.Printf(_("Windows 9x (%d.%d)"),
1092 info.dwMajorVersion,
1093 info.dwMinorVersion);
1094 break;
1095 }
1096 if ( !wxIsEmpty(info.szCSDVersion) )
1097 {
1098 str << _T(" (") << info.szCSDVersion << _T(')');
1099 }
1100 break;
1101
1102 case VER_PLATFORM_WIN32_NT:
1103 if ( info.dwMajorVersion == 5 )
1104 {
1105 switch ( info.dwMinorVersion )
1106 {
1107 case 0:
1108 str.Printf(_("Windows 2000 (build %lu"),
1109 info.dwBuildNumber);
1110 break;
1111 case 1:
1112 str.Printf(_("Windows XP (build %lu"),
1113 info.dwBuildNumber);
1114 break;
1115 case 2:
1116 str.Printf(_("Windows Server 2003 (build %lu"),
1117 info.dwBuildNumber);
1118 break;
1119 }
1120 }
1121 if ( wxIsEmpty(str) )
1122 {
1123 str.Printf(_("Windows NT %lu.%lu (build %lu"),
1124 info.dwMajorVersion,
1125 info.dwMinorVersion,
1126 info.dwBuildNumber);
1127 }
1128 if ( !wxIsEmpty(info.szCSDVersion) )
1129 {
1130 str << _T(", ") << info.szCSDVersion;
1131 }
1132 str << _T(')');
1133 break;
1134 }
1135 }
1136 else
1137 {
1138 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
1139 }
1140
1141 return str;
1142 }
1143
1144 wxToolkitInfo& wxAppTraits::GetToolkitInfo()
1145 {
1146 // cache the version info, it's not going to change
1147 //
1148 // NB: this is MT-safe, we may use these static vars from different threads
1149 // but as they always have the same value it doesn't matter
1150 static int s_ver = -1,
1151 s_major = -1,
1152 s_minor = -1;
1153
1154 if ( s_ver == -1 )
1155 {
1156 OSVERSIONINFO info;
1157 wxZeroMemory(info);
1158
1159 s_ver = wxWINDOWS;
1160 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1161 if ( ::GetVersionEx(&info) )
1162 {
1163 s_major = info.dwMajorVersion;
1164 s_minor = info.dwMinorVersion;
1165
1166 #ifdef __SMARTPHONE__
1167 s_ver = wxWINDOWS_SMARTPHONE;
1168 #elif defined(__POCKETPC__)
1169 s_ver = wxWINDOWS_POCKETPC;
1170 #else
1171 switch ( info.dwPlatformId )
1172 {
1173 case VER_PLATFORM_WIN32s:
1174 s_ver = wxWIN32S;
1175 break;
1176
1177 case VER_PLATFORM_WIN32_WINDOWS:
1178 s_ver = wxWIN95;
1179 break;
1180
1181 case VER_PLATFORM_WIN32_NT:
1182 s_ver = wxWINDOWS_NT;
1183 break;
1184 #ifdef __WXWINCE__
1185 case VER_PLATFORM_WIN32_CE:
1186 s_ver = wxWINDOWS_CE;
1187 #endif
1188 }
1189 #endif
1190 }
1191 }
1192
1193 static wxToolkitInfo info;
1194 info.versionMajor = s_major;
1195 info.versionMinor = s_minor;
1196 info.os = s_ver;
1197 info.name = _T("wxBase");
1198 return info;
1199 }
1200
1201 wxWinVersion wxGetWinVersion()
1202 {
1203 int verMaj,
1204 verMin;
1205 switch ( wxGetOsVersion(&verMaj, &verMin) )
1206 {
1207 case wxWIN95:
1208 if ( verMaj == 4 )
1209 {
1210 switch ( verMin )
1211 {
1212 case 0:
1213 return wxWinVersion_95;
1214
1215 case 10:
1216 return wxWinVersion_98;
1217
1218 case 90:
1219 return wxWinVersion_ME;
1220 }
1221 }
1222 break;
1223
1224 case wxWINDOWS_NT:
1225 switch ( verMaj )
1226 {
1227 case 3:
1228 return wxWinVersion_NT3;
1229
1230 case 4:
1231 return wxWinVersion_NT4;
1232
1233 case 5:
1234 switch ( verMin )
1235 {
1236 case 0:
1237 return wxWinVersion_2000;
1238
1239 case 1:
1240 return wxWinVersion_XP;
1241
1242 case 2:
1243 return wxWinVersion_2003;
1244 }
1245 break;
1246
1247 case 6:
1248 return wxWinVersion_NT6;
1249 }
1250 break;
1251
1252 }
1253
1254 return wxWinVersion_Unknown;
1255 }
1256
1257 // ----------------------------------------------------------------------------
1258 // sleep functions
1259 // ----------------------------------------------------------------------------
1260
1261 void wxMilliSleep(unsigned long milliseconds)
1262 {
1263 ::Sleep(milliseconds);
1264 }
1265
1266 void wxMicroSleep(unsigned long microseconds)
1267 {
1268 wxMilliSleep(microseconds/1000);
1269 }
1270
1271 void wxSleep(int nSecs)
1272 {
1273 wxMilliSleep(1000*nSecs);
1274 }
1275
1276 // ----------------------------------------------------------------------------
1277 // font encoding <-> Win32 codepage conversion functions
1278 // ----------------------------------------------------------------------------
1279
1280 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding)
1281 {
1282 switch ( encoding )
1283 {
1284 // although this function is supposed to return an exact match, do do
1285 // some mappings here for the most common case of "standard" encoding
1286 case wxFONTENCODING_SYSTEM:
1287 return DEFAULT_CHARSET;
1288
1289 case wxFONTENCODING_ISO8859_1:
1290 case wxFONTENCODING_ISO8859_15:
1291 case wxFONTENCODING_CP1252:
1292 return ANSI_CHARSET;
1293
1294 #if !defined(__WXMICROWIN__)
1295 // The following four fonts are multi-byte charsets
1296 case wxFONTENCODING_CP932:
1297 return SHIFTJIS_CHARSET;
1298
1299 case wxFONTENCODING_CP936:
1300 return GB2312_CHARSET;
1301
1302 #ifndef __WXWINCE__
1303 case wxFONTENCODING_CP949:
1304 return HANGUL_CHARSET;
1305 #endif
1306
1307 case wxFONTENCODING_CP950:
1308 return CHINESEBIG5_CHARSET;
1309
1310 // The rest are single byte encodings
1311 case wxFONTENCODING_CP1250:
1312 return EASTEUROPE_CHARSET;
1313
1314 case wxFONTENCODING_CP1251:
1315 return RUSSIAN_CHARSET;
1316
1317 case wxFONTENCODING_CP1253:
1318 return GREEK_CHARSET;
1319
1320 case wxFONTENCODING_CP1254:
1321 return TURKISH_CHARSET;
1322
1323 case wxFONTENCODING_CP1255:
1324 return HEBREW_CHARSET;
1325
1326 case wxFONTENCODING_CP1256:
1327 return ARABIC_CHARSET;
1328
1329 case wxFONTENCODING_CP1257:
1330 return BALTIC_CHARSET;
1331
1332 case wxFONTENCODING_CP874:
1333 return THAI_CHARSET;
1334 #endif // !__WXMICROWIN__
1335
1336 case wxFONTENCODING_CP437:
1337 return OEM_CHARSET;
1338
1339 default:
1340 // no way to translate this encoding into a Windows charset
1341 return -1;
1342 }
1343 }
1344
1345 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1346 // looks up the vlaues in the registry and the new one which is more
1347 // politically correct and has more chances to work on other Windows versions
1348 // as well but the old version is still needed for !wxUSE_FONTMAP case
1349 #if wxUSE_FONTMAP
1350
1351 #include "wx/fontmap.h"
1352
1353 extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding)
1354 {
1355 // There don't seem to be symbolic names for
1356 // these under Windows so I just copied the
1357 // values from MSDN.
1358
1359 unsigned int ret;
1360
1361 switch (encoding)
1362 {
1363 case wxFONTENCODING_ISO8859_1: ret = 28591; break;
1364 case wxFONTENCODING_ISO8859_2: ret = 28592; break;
1365 case wxFONTENCODING_ISO8859_3: ret = 28593; break;
1366 case wxFONTENCODING_ISO8859_4: ret = 28594; break;
1367 case wxFONTENCODING_ISO8859_5: ret = 28595; break;
1368 case wxFONTENCODING_ISO8859_6: ret = 28596; break;
1369 case wxFONTENCODING_ISO8859_7: ret = 28597; break;
1370 case wxFONTENCODING_ISO8859_8: ret = 28598; break;
1371 case wxFONTENCODING_ISO8859_9: ret = 28599; break;
1372 case wxFONTENCODING_ISO8859_10: ret = 28600; break;
1373 case wxFONTENCODING_ISO8859_11: ret = 28601; break;
1374 // case wxFONTENCODING_ISO8859_12, // doesn't exist currently, but put it
1375 case wxFONTENCODING_ISO8859_13: ret = 28603; break;
1376 case wxFONTENCODING_ISO8859_14: ret = 28604; break;
1377 case wxFONTENCODING_ISO8859_15: ret = 28605; break;
1378 case wxFONTENCODING_KOI8: ret = 20866; break;
1379 case wxFONTENCODING_KOI8_U: ret = 21866; break;
1380 case wxFONTENCODING_CP437: ret = 437; break;
1381 case wxFONTENCODING_CP850: ret = 850; break;
1382 case wxFONTENCODING_CP852: ret = 852; break;
1383 case wxFONTENCODING_CP855: ret = 855; break;
1384 case wxFONTENCODING_CP866: ret = 866; break;
1385 case wxFONTENCODING_CP874: ret = 874; break;
1386 case wxFONTENCODING_CP932: ret = 932; break;
1387 case wxFONTENCODING_CP936: ret = 936; break;
1388 case wxFONTENCODING_CP949: ret = 949; break;
1389 case wxFONTENCODING_CP950: ret = 950; break;
1390 case wxFONTENCODING_CP1250: ret = 1250; break;
1391 case wxFONTENCODING_CP1251: ret = 1251; break;
1392 case wxFONTENCODING_CP1252: ret = 1252; break;
1393 case wxFONTENCODING_CP1253: ret = 1253; break;
1394 case wxFONTENCODING_CP1254: ret = 1254; break;
1395 case wxFONTENCODING_CP1255: ret = 1255; break;
1396 case wxFONTENCODING_CP1256: ret = 1256; break;
1397 case wxFONTENCODING_CP1257: ret = 1257; break;
1398 case wxFONTENCODING_EUC_JP: ret = 20932; break;
1399 case wxFONTENCODING_MACROMAN: ret = 10000; break;
1400 case wxFONTENCODING_MACJAPANESE: ret = 10001; break;
1401 case wxFONTENCODING_MACCHINESETRAD: ret = 10002; break;
1402 case wxFONTENCODING_MACKOREAN: ret = 10003; break;
1403 case wxFONTENCODING_MACARABIC: ret = 10004; break;
1404 case wxFONTENCODING_MACHEBREW: ret = 10005; break;
1405 case wxFONTENCODING_MACGREEK: ret = 10006; break;
1406 case wxFONTENCODING_MACCYRILLIC: ret = 10007; break;
1407 case wxFONTENCODING_MACTHAI: ret = 10021; break;
1408 case wxFONTENCODING_MACCHINESESIMP: ret = 10008; break;
1409 case wxFONTENCODING_MACCENTRALEUR: ret = 10029; break;
1410 case wxFONTENCODING_MACCROATIAN: ret = 10082; break;
1411 case wxFONTENCODING_MACICELANDIC: ret = 10079; break;
1412 case wxFONTENCODING_MACROMANIAN: ret = 10009; break;
1413 case wxFONTENCODING_UTF7: ret = 65000; break;
1414 case wxFONTENCODING_UTF8: ret = 65001; break;
1415 default: return -1;
1416 }
1417
1418 if (::IsValidCodePage(ret) == 0)
1419 return -1;
1420
1421 CPINFO info;
1422 if (::GetCPInfo(ret, &info) == 0)
1423 return -1;
1424
1425 return (long) ret;
1426 }
1427
1428 extern long wxCharsetToCodepage(const wxChar *name)
1429 {
1430 // first get the font encoding for this charset
1431 if ( !name )
1432 return -1;
1433
1434 wxFontEncoding enc = wxFontMapperBase::Get()->CharsetToEncoding(name, false);
1435 if ( enc == wxFONTENCODING_SYSTEM )
1436 return -1;
1437
1438 // the use the helper function
1439 return wxEncodingToCodepage(enc);
1440 }
1441
1442 #else // !wxUSE_FONTMAP
1443
1444 #include "wx/msw/registry.h"
1445
1446 // this should work if Internet Exploiter is installed
1447 extern long wxCharsetToCodepage(const wxChar *name)
1448 {
1449 if (!name)
1450 return GetACP();
1451
1452 long CP = -1;
1453
1454 wxString path(wxT("MIME\\Database\\Charset\\"));
1455 wxString cn(name);
1456
1457 // follow the alias loop
1458 for ( ;; )
1459 {
1460 wxRegKey key(wxRegKey::HKCR, path + cn);
1461
1462 if (!key.Exists())
1463 break;
1464
1465 // two cases: either there's an AliasForCharset string,
1466 // or there are Codepage and InternetEncoding dwords.
1467 // The InternetEncoding gives us the actual encoding,
1468 // the Codepage just says which Windows character set to
1469 // use when displaying the data.
1470 if (key.HasValue(wxT("InternetEncoding")) &&
1471 key.QueryValue(wxT("InternetEncoding"), &CP))
1472 break;
1473
1474 // no encoding, see if it's an alias
1475 if (!key.HasValue(wxT("AliasForCharset")) ||
1476 !key.QueryValue(wxT("AliasForCharset"), cn))
1477 break;
1478 }
1479
1480 return CP;
1481 }
1482
1483 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1484
1485 /*
1486 Creates a hidden window with supplied window proc registering the class for
1487 it if necesssary (i.e. the first time only). Caller is responsible for
1488 destroying the window and unregistering the class (note that this must be
1489 done because wxWidgets may be used as a DLL and so may be loaded/unloaded
1490 multiple times into/from the same process so we cna't rely on automatic
1491 Windows class unregistration).
1492
1493 pclassname is a pointer to a caller stored classname, which must initially be
1494 NULL. classname is the desired wndclass classname. If function successfully
1495 registers the class, pclassname will be set to classname.
1496 */
1497 extern "C" WXDLLIMPEXP_BASE HWND
1498 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc)
1499 {
1500 wxCHECK_MSG( classname && pclassname && wndproc, NULL,
1501 _T("NULL parameter in wxCreateHiddenWindow") );
1502
1503 // register the class fi we need to first
1504 if ( *pclassname == NULL )
1505 {
1506 WNDCLASS wndclass;
1507 wxZeroMemory(wndclass);
1508
1509 wndclass.lpfnWndProc = wndproc;
1510 wndclass.hInstance = wxGetInstance();
1511 wndclass.lpszClassName = classname;
1512
1513 if ( !::RegisterClass(&wndclass) )
1514 {
1515 wxLogLastError(wxT("RegisterClass() in wxCreateHiddenWindow"));
1516
1517 return NULL;
1518 }
1519
1520 *pclassname = classname;
1521 }
1522
1523 // next create the window
1524 HWND hwnd = ::CreateWindow
1525 (
1526 *pclassname,
1527 NULL,
1528 0, 0, 0, 0,
1529 0,
1530 (HWND) NULL,
1531 (HMENU)NULL,
1532 wxGetInstance(),
1533 (LPVOID) NULL
1534 );
1535
1536 if ( !hwnd )
1537 {
1538 wxLogLastError(wxT("CreateWindow() in wxCreateHiddenWindow"));
1539 }
1540
1541 return hwnd;
1542 }
1543