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