1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now.
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/cursor.h"
30 #include "wx/msw/private.h"
38 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
46 #if defined(__GNUWIN32__) && !defined(__TWIN32__)
47 #include <sys/unistd.h>
53 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
54 // this (3.1 I believe) and how to test for it.
55 // If this works for Borland 4.0 as well, then no worries.
71 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
77 //// BEGIN for console support: VC++ only
80 #include "wx/msw/msvcrt.h"
84 #include "wx/ioswrap.h"
87 // N.B. BC++ doesn't have istream.h, ostream.h
94 /* Need to undef new if including crtdbg.h */
103 # if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
104 # define new new(__FILE__,__LINE__)
109 /// END for console support
111 // In the WIN.INI file
112 static const char WX_SECTION
[] = "wxWindows";
113 static const char eHOSTNAME
[] = "HostName";
114 static const char eUSERID
[] = "UserId";
115 static const char eUSERNAME
[] = "UserName";
117 // For the following functions we SHOULD fill in support
118 // for Windows-NT (which I don't know) as I assume it begin
119 // a POSIX Unix (so claims MS) that it has some special
120 // functions beyond those provided by WinSock
122 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
123 bool wxGetHostName(char *buf
, int maxSize
)
125 #if defined(__WIN32__) && !defined(__TWIN32__)
126 DWORD nSize
= maxSize
;
127 return (::GetComputerName(buf
, &nSize
) != 0);
130 const char *default_host
= "noname";
132 if ((sysname
= getenv("SYSTEM_NAME")) == NULL
) {
133 GetProfileString(WX_SECTION
, eHOSTNAME
, default_host
, buf
, maxSize
- 1);
135 strncpy(buf
, sysname
, maxSize
- 1);
137 return *buf
? TRUE
: FALSE
;
141 // Get user ID e.g. jacs
142 bool wxGetUserId(char *buf
, int maxSize
)
144 #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__)
146 // VZ: why should it be so complicated??
148 // Gets the current user's full name according to the MS article PSS ID
150 // Seems to be the same as the login name for me?
151 char *UserName
= new char[256];
152 char *Domain
= new char[256];
153 DWORD maxCharacters
= 255;
154 GetUserName( UserName
, &maxCharacters
);
155 GetComputerName( Domain
, &maxCharacters
);
157 WCHAR wszUserName
[256]; // Unicode user name
158 WCHAR wszDomain
[256];
161 struct _SERVER_INFO_100
*si100
; // Server structure
162 struct _USER_INFO_2
*ui
; // User structure
164 // Convert ASCII user name and domain to Unicode.
166 MultiByteToWideChar( CP_ACP
, 0, UserName
,
167 strlen(UserName
)+1, wszUserName
, sizeof(wszUserName
) );
168 MultiByteToWideChar( CP_ACP
, 0, Domain
,
169 strlen(Domain
)+1, wszDomain
, sizeof(wszDomain
) );
171 // Get the computer name of a DC for the specified domain.
172 // >If you get a link error on this, include netapi32.lib<
174 NetGetDCName( NULL
, wszDomain
, &ComputerName
);
176 // Look up the user on the DC.
178 if(NetUserGetInfo( (LPWSTR
) ComputerName
,
179 (LPWSTR
) &wszUserName
, 2, (LPBYTE
*) &ui
))
181 printf( "Error getting user information.\n" );
185 // Convert the Unicode full name to ASCII.
187 WideCharToMultiByte( CP_ACP
, 0, ui
->usri2_full_name
,
188 -1, buf
, 256, NULL
, NULL
);
192 DWORD nSize
= maxSize
;
193 if ( ::GetUserName(buf
, &nSize
) == 0 )
195 wxLogSysError("Can not get user name");
203 #else // Win16 or Win32s
205 const char *default_id
= "anonymous";
207 // Can't assume we have NIS (PC-NFS) or some other ID daemon
209 if ( (user
= getenv("USER")) == NULL
&&
210 (user
= getenv("LOGNAME")) == NULL
) {
211 // Use wxWindows configuration data (comming soon)
212 GetProfileString(WX_SECTION
, eUSERID
, default_id
, buf
, maxSize
- 1);
214 strncpy(buf
, user
, maxSize
- 1);
215 return *buf
? TRUE
: FALSE
;
219 // Get user name e.g. Julian Smart
220 bool wxGetUserName(char *buf
, int maxSize
)
222 #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__)
223 extern HANDLE g_hPenWin
; // PenWindows Running?
226 // PenWindows Does have a user concept!
227 // Get the current owner of the recognizer
228 GetPrivateProfileString("Current", "User", default_name
, wxBuffer
, maxSize
- 1, "PENWIN.INI");
229 strncpy(buf
, wxBuffer
, maxSize
- 1);
234 // Could use NIS, MS-Mail or other site specific programs
235 // Use wxWindows configuration data
236 bool ok
= GetProfileString(WX_SECTION
, eUSERNAME
, "", buf
, maxSize
- 1) != 0;
239 ok
= wxGetUserId(buf
, maxSize
);
244 strncpy(buf
, "Unknown User", maxSize
);
251 int wxKill(long pid
, int sig
)
257 // Execute a program in an Interactive Shell
260 wxShell(const wxString
& command
)
263 if ((shell
= getenv("COMSPEC")) == NULL
)
264 shell
= "\\COMMAND.COM";
268 sprintf(tmp
, "%s /c %s", shell
, WXSTRINGCAST command
);
272 return (wxExecute((char *)tmp
, FALSE
) != 0);
275 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
276 long wxGetFreeMemory(void)
278 #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__)
279 MEMORYSTATUS memStatus
;
280 memStatus
.dwLength
= sizeof(MEMORYSTATUS
);
281 GlobalMemoryStatus(&memStatus
);
282 return memStatus
.dwAvailPhys
;
284 return (long)GetFreeSpace(0);
288 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
289 static bool inTimer
= FALSE
;
290 class wxSleepTimer
: public wxTimer
293 inline void Notify(void)
300 static wxTimer
*wxTheSleepTimer
= NULL
;
302 void wxSleep(int nSecs
)
304 #if 0 // WIN32 hangs app
310 wxTheSleepTimer
= new wxSleepTimer
;
312 wxTheSleepTimer
->Start(nSecs
*1000);
315 if (wxTheApp
->Pending())
316 wxTheApp
->Dispatch();
318 delete wxTheSleepTimer
;
319 wxTheSleepTimer
= NULL
;
323 // Consume all events until no more left
324 void wxFlushEvents(void)
329 // Output a debug mess., in a system dependent fashion.
330 void wxDebugMsg(const char *fmt
...)
333 static char buffer
[512];
335 if (!wxTheApp
->GetWantDebugOutput())
340 wvsprintf(buffer
,fmt
,ap
) ;
341 OutputDebugString((LPCSTR
)buffer
) ;
346 // Non-fatal error: pop up message box and (possibly) continue
347 void wxError(const wxString
& msg
, const wxString
& title
)
349 sprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST msg
);
350 if (MessageBox(NULL
, (LPCSTR
)wxBuffer
, (LPCSTR
)WXSTRINGCAST title
,
351 MB_ICONSTOP
| MB_YESNO
) == IDNO
)
355 // Fatal error: pop up message box and abort
356 void wxFatalError(const wxString
& msg
, const wxString
& title
)
358 sprintf(wxBuffer
, "%s: %s", WXSTRINGCAST title
, WXSTRINGCAST msg
);
359 FatalAppExit(0, (LPCSTR
)wxBuffer
);
365 // Removed by RD because IHMO syncronous sound is a Bad Thing. MessageBeep
366 // will do a similar thing anyway if there is no sound card...
368 // Beep(1000,1000) ; // 1kHz during 1 sec.
370 MessageBeep((UINT
)-1) ;
374 // Chris Breeze 27/5/98: revised WIN32 code to
375 // detect WindowsNT correctly
376 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
378 extern char *wxOsVersion
;
379 if (majorVsn
) *majorVsn
= 0;
380 if (minorVsn
) *minorVsn
= 0;
382 #if defined(__WIN32__) && !defined(__SC__)
384 memset(&info
, 0, sizeof(OSVERSIONINFO
));
385 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
386 if (GetVersionEx(&info
))
388 if (majorVsn
) *majorVsn
= info
.dwMajorVersion
;
389 if (minorVsn
) *minorVsn
= info
.dwMinorVersion
;
390 switch (info
.dwPlatformId
)
392 case VER_PLATFORM_WIN32s
:
395 case VER_PLATFORM_WIN32_WINDOWS
:
398 case VER_PLATFORM_WIN32_NT
:
403 return wxWINDOWS
; // error if we get here, return generic value
407 # ifdef __WINDOWS_386__
410 # if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
411 extern HANDLE g_hPenWin
;
412 retValue
= g_hPenWin
? wxPENWINDOWS
: wxWINDOWS
;
415 // @@@@ To be completed. I don't have the manual here...
416 if (majorVsn
) *majorVsn
= 3 ;
417 if (minorVsn
) *minorVsn
= 1 ;
422 // Reading and writing resources (eg WIN.INI, .Xdefaults)
424 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
427 return (WritePrivateProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)value
, (LPCSTR
)WXSTRINGCAST file
) != 0);
429 return (WriteProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)WXSTRINGCAST value
) != 0);
432 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
435 sprintf(buf
, "%.4f", value
);
436 return wxWriteResource(section
, entry
, buf
, file
);
439 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
442 sprintf(buf
, "%ld", value
);
443 return wxWriteResource(section
, entry
, buf
, file
);
446 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
449 sprintf(buf
, "%d", value
);
450 return wxWriteResource(section
, entry
, buf
, file
);
453 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
455 static const char defunkt
[] = "$$default";
458 int n
= GetPrivateProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)defunkt
,
459 (LPSTR
)wxBuffer
, 1000, (LPCSTR
)WXSTRINGCAST file
);
460 if (n
== 0 || strcmp(wxBuffer
, defunkt
) == 0)
465 int n
= GetProfileString((LPCSTR
)WXSTRINGCAST section
, (LPCSTR
)WXSTRINGCAST entry
, (LPCSTR
)defunkt
,
466 (LPSTR
)wxBuffer
, 1000);
467 if (n
== 0 || strcmp(wxBuffer
, defunkt
) == 0)
470 if (*value
) delete[] (*value
);
471 *value
= copystring(wxBuffer
);
475 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
478 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
481 *value
= (float)strtod(s
, NULL
);
488 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
491 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
494 *value
= strtol(s
, NULL
, 10);
501 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
504 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
507 *value
= (int)strtol(s
, NULL
, 10);
513 #endif // wxUSE_RESOURCES
516 static HCURSOR wxBusyCursorOld
= 0;
517 static int wxBusyCursorCount
= 0;
519 // Set the cursor to the busy cursor for all windows
520 void wxBeginBusyCursor(wxCursor
*cursor
)
522 wxBusyCursorCount
++;
523 if (wxBusyCursorCount
== 1)
525 wxBusyCursorOld
= ::SetCursor((HCURSOR
) cursor
->GetHCURSOR());
529 (void)::SetCursor((HCURSOR
) cursor
->GetHCURSOR());
533 // Restore cursor to normal
534 void wxEndBusyCursor(void)
536 if (wxBusyCursorCount
== 0)
539 wxBusyCursorCount
--;
540 if (wxBusyCursorCount
== 0)
542 ::SetCursor(wxBusyCursorOld
);
547 // TRUE if we're between the above two calls
550 return (wxBusyCursorCount
> 0);
553 const char* wxGetHomeDir(wxString
*pstr
)
555 wxString
& strDir
= *pstr
;
557 #if defined(__UNIX__) && !defined(__TWIN32__)
558 const char *szHome
= getenv("HOME");
559 if ( szHome
== NULL
) {
561 wxLogWarning(_("can't find user's HOME, using current directory."));
567 // add a trailing slash if needed
568 if ( strDir
.Last() != '/' )
572 const char *szHome
= getenv("HOMEDRIVE");
573 if ( szHome
!= NULL
)
575 szHome
= getenv("HOMEPATH");
576 if ( szHome
!= NULL
) {
579 // the idea is that under NT these variables have default values
580 // of "%systemdrive%:" and "\\". As we don't want to create our
581 // config files in the root directory of the system drive, we will
582 // create it in our program's dir. However, if the user took care
583 // to set HOMEPATH to something other than "\\", we suppose that he
584 // knows what he is doing and use the supplied value.
585 if ( strcmp(szHome
, "\\") != 0 )
586 return strDir
.c_str();
590 // Win16 has no idea about home, so use the working directory instead
593 // 260 was taken from windef.h
599 ::GetModuleFileName(::GetModuleHandle(NULL
),
600 strPath
.GetWriteBuf(MAX_PATH
), MAX_PATH
);
601 strPath
.UngetWriteBuf();
603 // extract the dir name
604 wxSplitPath(strPath
, &strDir
, NULL
, NULL
);
608 return strDir
.c_str();
612 char *wxGetUserHome (const wxString
& user
)
615 wxString
user1(user
);
619 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
620 // Guests belong in the temp dir
621 if (Stricmp(tmp
, "annonymous") == 0) {
622 if ((home
= getenv("TMP")) != NULL
||
623 (home
= getenv("TMPDIR")) != NULL
||
624 (home
= getenv("TEMP")) != NULL
)
625 return *home
? home
: "\\";
627 if (Stricmp(tmp
, WXSTRINGCAST user1
) == 0)
632 if ((home
= getenv("HOME")) != NULL
)
634 strcpy(wxBuffer
, home
);
635 Unix2DosFilename(wxBuffer
);
638 return NULL
; // No home known!
641 // Check whether this window wants to process messages, e.g. Stop button
642 // in long calculations.
643 bool wxCheckForInterrupt(wxWindow
*wnd
)
647 HWND win
= (HWND
) wnd
->GetHWND();
648 while(PeekMessage(&msg
,win
,0,0,PM_REMOVE
)){
649 TranslateMessage(&msg
);
650 DispatchMessage(&msg
);
652 return TRUE
;//*** temporary?
655 wxError("wnd==NULL !!!");
656 return FALSE
;//*** temporary?
660 // MSW only: get user-defined resource from the .res file.
661 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
664 char *wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
667 #if !defined(__WIN32__) || defined(__TWIN32__)
668 HRSRC hResource
= ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
671 HRSRC hResource
= ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
673 HRSRC hResource
= ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName
, WXSTRINGCAST resourceType
);
679 HGLOBAL hData
= ::LoadResource(wxGetInstance(), hResource
);
682 char *theText
= (char *)LockResource(hData
);
686 s
= copystring(theText
);
690 UnlockResource(hData
);
694 // GlobalFree(hData);
700 void wxGetMousePosition( int* x
, int* y
)
703 GetCursorPos( & pt
);
708 // Return TRUE if we have a colour display
709 bool wxColourDisplay(void)
711 HDC dc
= ::GetDC((HWND
) NULL
);
713 int noCols
= GetDeviceCaps(dc
, NUMCOLORS
);
714 if ((noCols
== -1) || (noCols
> 2))
718 ReleaseDC((HWND
) NULL
, dc
);
722 // Returns depth of screen
723 int wxDisplayDepth(void)
725 HDC dc
= ::GetDC((HWND
) NULL
);
726 int planes
= GetDeviceCaps(dc
, PLANES
);
727 int bitsPerPixel
= GetDeviceCaps(dc
, BITSPIXEL
);
728 int depth
= planes
*bitsPerPixel
;
729 ReleaseDC((HWND
) NULL
, dc
);
733 // Get size of display
734 void wxDisplaySize(int *width
, int *height
)
736 HDC dc
= ::GetDC((HWND
) NULL
);
737 *width
= GetDeviceCaps(dc
, HORZRES
); *height
= GetDeviceCaps(dc
, VERTRES
);
738 ReleaseDC((HWND
) NULL
, dc
);
741 bool wxDirExists(const wxString
& dir
)
743 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
744 #if defined(__WIN32__)
745 WIN32_FIND_DATA fileInfo
;
748 struct ffblk fileInfo
;
750 struct find_t fileInfo
;
754 #if defined(__WIN32__)
755 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
757 if (h
==INVALID_HANDLE_VALUE
)
761 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
764 // In Borland findfirst has a different argument
765 // ordering from _dos_findfirst. But _dos_findfirst
766 // _should_ be ok in both MS and Borland... why not?
768 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
770 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
775 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
778 int len
= GetWindowTextLength((HWND
)hWnd
) + 1;
779 GetWindowText((HWND
)hWnd
, str
.GetWriteBuf(len
), len
);
786 //------------------------------------------------------------------------
787 // wild character routines
788 //------------------------------------------------------------------------
790 bool wxIsWild( const wxString
& pattern
)
792 wxString tmp
= pattern
;
793 char *pat
= WXSTRINGCAST(tmp
);
796 case '?': case '*': case '[': case '{':
807 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
810 char *pattern
= WXSTRINGCAST(tmp1
);
811 wxString tmp2
= text
;
812 char *str
= WXSTRINGCAST(tmp2
);
815 bool done
= FALSE
, ret_code
, ok
;
816 // Below is for vi fans
817 const char OB
= '{', CB
= '}';
819 // dot_special means '.' only matches '.'
820 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
823 while ((*pattern
!= '\0') && (!done
)
824 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
828 if (*pattern
!= '\0')
835 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
840 while (*pattern
!= '\0')
847 if ((*pattern
== '\0') || (*pattern
== ']')) {
851 if (*pattern
== '\\') {
853 if (*pattern
== '\0') {
858 if (*(pattern
+ 1) == '-') {
861 if (*pattern
== ']') {
865 if (*pattern
== '\\') {
867 if (*pattern
== '\0') {
872 if ((*str
< c
) || (*str
> *pattern
)) {
876 } else if (*pattern
!= *str
) {
881 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
882 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
886 if (*pattern
!= '\0') {
896 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
899 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
900 && (*pattern
!= ',') && (*pattern
!= CB
)) {
901 if (*pattern
== '\\')
903 ok
= (*pattern
++ == *cp
++);
905 if (*pattern
== '\0') {
911 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
912 if (*++pattern
== '\\') {
913 if (*++pattern
== CB
)
918 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
919 if (*++pattern
== '\\') {
920 if (*++pattern
== CB
|| *pattern
== ',')
925 if (*pattern
!= '\0')
930 if (*str
== *pattern
) {
937 while (*pattern
== '*')
939 return ((*str
== '\0') && (*pattern
== '\0'));
944 #if defined(__WIN95__) && defined(__WXDEBUG__) && wxUSE_DBWIN32
947 When I started programming with Visual C++ v4.0, I missed one of my favorite
948 tools -- DBWIN. Finding the code for a simple debug trace utility, DBMON,
949 on MSDN was a step in the right direction, but it is a console application
950 and thus has limited features and extensibility. DBWIN32 is my creation
951 to solve this problem.
953 The code is essentially a merging of a stripped down version of the DBWIN code
954 from VC 1.5 and DBMON.C with a few 32 bit changes.
956 As of version 1.2B, DBWIN32 supports both Win95 and NT. The NT support is
957 built into the operating system and works just by running DBWIN32. The Win95
958 team decided not to support this hook, so I have provided code that will do
959 this for you. See the file WIN95.TXT for instructions on installing this.
961 If you have questions, problems or suggestions about DBWIN32, I welcome your
962 feedback and plan to actively maintain the code.
967 To download dbwin32, see e.g.:
969 http://ftp.digital.com/pub/micro/NT/WinSite/programr/dbwin32.zip
972 #if !defined(__MWERKS__) && !defined(__SALFORDC__) && !defined(__TWIN32__)
976 void OutputDebugStringW95(const char* lpOutputString
, ...)
978 HANDLE heventDBWIN
; /* DBWIN32 synchronization object */
979 HANDLE heventData
; /* data passing synch object */
980 HANDLE hSharedFile
; /* memory mapped file shared data */
984 /* create the output buffer */
986 va_start(args
, lpOutputString
);
987 vsprintf(achBuffer
, lpOutputString
, args
);
991 Do a regular OutputDebugString so that the output is
992 still seen in the debugger window if it exists.
994 This ifdef is necessary to avoid infinite recursion
995 from the inclusion of W95TRACE.H
998 ::OutputDebugStringW(achBuffer
);
1001 ::OutputDebugString(achBuffer
);
1003 ::OutputDebugStringA(achBuffer
);
1007 /* bail if it's not Win95 */
1009 OSVERSIONINFO VerInfo
;
1010 VerInfo
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1011 GetVersionEx(&VerInfo
);
1012 if ( VerInfo
.dwPlatformId
!= VER_PLATFORM_WIN32_WINDOWS
)
1016 /* make sure DBWIN is open and waiting */
1017 heventDBWIN
= OpenEvent(EVENT_MODIFY_STATE
, FALSE
, "DBWIN_BUFFER_READY");
1020 //MessageBox(NULL, "DBWIN_BUFFER_READY nonexistent", NULL, MB_OK);
1024 /* get a handle to the data synch object */
1025 heventData
= OpenEvent(EVENT_MODIFY_STATE
, FALSE
, "DBWIN_DATA_READY");
1028 // MessageBox(NULL, "DBWIN_DATA_READY nonexistent", NULL, MB_OK);
1029 CloseHandle(heventDBWIN
);
1033 hSharedFile
= CreateFileMapping((HANDLE
)-1, NULL
, PAGE_READWRITE
, 0, 4096, "DBWIN_BUFFER");
1036 //MessageBox(NULL, "DebugTrace: Unable to create file mapping object DBWIN_BUFFER", "Error", MB_OK);
1037 CloseHandle(heventDBWIN
);
1038 CloseHandle(heventData
);
1042 lpszSharedMem
= (LPSTR
)MapViewOfFile(hSharedFile
, FILE_MAP_WRITE
, 0, 0, 512);
1045 //MessageBox(NULL, "DebugTrace: Unable to map shared memory", "Error", MB_OK);
1046 CloseHandle(heventDBWIN
);
1047 CloseHandle(heventData
);
1051 /* wait for buffer event */
1052 WaitForSingleObject(heventDBWIN
, INFINITE
);
1054 /* write it to the shared memory */
1055 #if defined( __BORLANDC__ ) || defined( __MWERKS__ ) || defined(__SALFORDC__)
1056 *((LPDWORD
)lpszSharedMem
) = getpid();
1058 *((LPDWORD
)lpszSharedMem
) = _getpid();
1061 wsprintf(lpszSharedMem
+ sizeof(DWORD
), "%s", achBuffer
);
1063 /* signal data ready event */
1064 SetEvent(heventData
);
1066 /* clean up handles */
1067 CloseHandle(hSharedFile
);
1068 CloseHandle(heventData
);
1069 CloseHandle(heventDBWIN
);
1080 // maximum mumber of lines the output console should have
1081 static const WORD MAX_CONSOLE_LINES
= 500;
1083 BOOL WINAPI
MyConsoleHandler( DWORD dwCtrlType
) { // control signal type
1088 void wxRedirectIOToConsole()
1092 CONSOLE_SCREEN_BUFFER_INFO coninfo
;
1095 // allocate a console for this app
1098 // set the screen buffer to be big enough to let us scroll text
1099 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
),
1101 coninfo
.dwSize
.Y
= MAX_CONSOLE_LINES
;
1102 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
),
1105 // redirect unbuffered STDOUT to the console
1106 lStdHandle
= (long)GetStdHandle(STD_OUTPUT_HANDLE
);
1107 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1108 if(hConHandle
<= 0) return;
1109 fp
= _fdopen( hConHandle
, "w" );
1111 setvbuf( stdout
, NULL
, _IONBF
, 0 );
1113 // redirect unbuffered STDIN to the console
1114 lStdHandle
= (long)GetStdHandle(STD_INPUT_HANDLE
);
1115 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1116 if(hConHandle
<= 0) return;
1117 fp
= _fdopen( hConHandle
, "r" );
1119 setvbuf( stdin
, NULL
, _IONBF
, 0 );
1121 // redirect unbuffered STDERR to the console
1122 lStdHandle
= (long)GetStdHandle(STD_ERROR_HANDLE
);
1123 hConHandle
= _open_osfhandle(lStdHandle
, _O_TEXT
);
1124 if(hConHandle
<= 0) return;
1125 fp
= _fdopen( hConHandle
, "w" );
1127 setvbuf( stderr
, NULL
, _IONBF
, 0 );
1129 // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
1130 // point to console as well
1131 ios::sync_with_stdio();
1133 SetConsoleCtrlHandler(MyConsoleHandler
, TRUE
);
1137 void wxRedirectIOToConsole()