+bool wxConsoleStderr::DoInit()
+{
+ HANDLE hStderr = ::GetStdHandle(STD_ERROR_HANDLE);
+
+ if ( hStderr == INVALID_HANDLE_VALUE || !hStderr )
+ return false;
+
+ if ( !m_dllKernel32.Load(wxT("kernel32.dll")) )
+ return false;
+
+ typedef BOOL (WINAPI *AttachConsole_t)(DWORD dwProcessId);
+ AttachConsole_t wxDL_INIT_FUNC(pfn, AttachConsole, m_dllKernel32);
+
+ if ( !pfnAttachConsole || !pfnAttachConsole(ATTACH_PARENT_PROCESS) )
+ return false;
+
+ // console attached, set m_hStderr now to ensure that we free it in the
+ // dtor
+ m_hStderr = hStderr;
+
+ wxDL_INIT_FUNC_AW(m_pfn, GetConsoleCommandHistory, m_dllKernel32);
+ if ( !m_pfnGetConsoleCommandHistory )
+ return false;
+
+ wxDL_INIT_FUNC_AW(m_pfn, GetConsoleCommandHistoryLength, m_dllKernel32);
+ if ( !m_pfnGetConsoleCommandHistoryLength )
+ return false;
+
+ // remember the current command history to be able to compare with it later
+ // in IsHistoryUnchanged()
+ m_historyLen = GetCommandHistory(m_history);
+ if ( !m_history )
+ return false;
+
+
+ // now find the first blank line above the current position
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+ if ( !::GetConsoleScreenBufferInfo(m_hStderr, &csbi) )
+ {
+ wxLogLastError(wxT("GetConsoleScreenBufferInfo"));
+ return false;
+ }
+
+ COORD pos;
+ pos.X = 0;
+ pos.Y = csbi.dwCursorPosition.Y + 1;
+
+ // we decide that a line is empty if first 4 characters are spaces
+ DWORD ret;
+ char buf[4];
+ do
+ {
+ pos.Y--;
+ if ( !::ReadConsoleOutputCharacterA(m_hStderr, buf, WXSIZEOF(buf),
+ pos, &ret) )
+ {
+ wxLogLastError(wxT("ReadConsoleOutputCharacterA"));
+ return false;
+ }
+ } while ( wxStrncmp(" ", buf, WXSIZEOF(buf)) != 0 );
+
+ // calculate line offset and length of data
+ m_dataLine = csbi.dwCursorPosition.Y - pos.Y;
+ m_dataLen = m_dataLine*csbi.dwMaximumWindowSize.X + csbi.dwCursorPosition.X;
+
+ if ( m_dataLen > 0 )
+ {
+ m_data.extend(m_dataLen);
+ if ( !::ReadConsoleOutputCharacterA(m_hStderr, m_data.data(), m_dataLen,
+ pos, &ret) )
+ {
+ wxLogLastError(wxT("ReadConsoleOutputCharacterA"));
+ return false;
+ }
+ }
+
+ return true;
+}
+
+int wxConsoleStderr::GetCommandHistory(wxWxCharBuffer& buf) const
+{
+ // these functions are internal and may only be called by cmd.exe
+ static const wxChar *CMD_EXE = wxT("cmd.exe");
+
+ const int len = m_pfnGetConsoleCommandHistoryLength(CMD_EXE);
+ if ( len )
+ {
+ buf.extend(len);
+
+ int len2 = m_pfnGetConsoleCommandHistory(buf.data(), len, CMD_EXE);
+
+#if !wxUSE_UNICODE
+ // there seems to be a bug in the GetConsoleCommandHistoryA(), it
+ // returns the length of Unicode string and not ANSI one
+ len2 /= 2;
+#endif // !wxUSE_UNICODE
+
+ if ( len2 != len )
+ {
+ wxFAIL_MSG( wxT("failed getting history?") );
+ }
+ }
+
+ return len;
+}
+
+bool wxConsoleStderr::IsHistoryUnchanged() const
+{
+ wxASSERT_MSG( m_ok == 1, wxT("shouldn't be called if not initialized") );
+
+ // get (possibly changed) command history
+ wxWxCharBuffer history;
+ const int historyLen = GetCommandHistory(history);
+
+ // and compare it with the original one
+ return historyLen == m_historyLen && history &&
+ memcmp(m_history, history, historyLen) == 0;
+}
+
+bool wxConsoleStderr::Write(const wxString& text)
+{
+ wxASSERT_MSG( m_hStderr != INVALID_HANDLE_VALUE,
+ wxT("should only be called if Init() returned true") );
+
+ // get current position
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
+ if ( !::GetConsoleScreenBufferInfo(m_hStderr, &csbi) )
+ {
+ wxLogLastError(wxT("GetConsoleScreenBufferInfo"));
+ return false;
+ }
+
+ // and calculate new position (where is empty line)
+ csbi.dwCursorPosition.X = 0;
+ csbi.dwCursorPosition.Y -= m_dataLine;
+
+ if ( !::SetConsoleCursorPosition(m_hStderr, csbi.dwCursorPosition) )
+ {
+ wxLogLastError(wxT("SetConsoleCursorPosition"));
+ return false;
+ }
+
+ DWORD ret;
+ if ( !::FillConsoleOutputCharacter(m_hStderr, wxT(' '), m_dataLen,
+ csbi.dwCursorPosition, &ret) )
+ {
+ wxLogLastError(wxT("FillConsoleOutputCharacter"));
+ return false;
+ }
+
+ if ( !::WriteConsole(m_hStderr, text.wx_str(), text.length(), &ret, NULL) )
+ {
+ wxLogLastError(wxT("WriteConsole"));
+ return false;
+ }
+
+ WriteConsoleA(m_hStderr, m_data, m_dataLen, &ret, 0);
+
+ return true;
+}
+
+wxConsoleStderr s_consoleStderr;
+
+} // anonymous namespace
+
+bool wxGUIAppTraits::CanUseStderr()
+{
+ return s_consoleStderr.IsOkToUse();
+}
+
+bool wxGUIAppTraits::WriteToStderr(const wxString& text)
+{
+ return s_consoleStderr.IsOkToUse() && s_consoleStderr.Write(text);
+}
+
+#else // !wxUSE_DYNLIB_CLASS
+
+bool wxGUIAppTraits::CanUseStderr()
+{
+ return false;
+}
+
+bool wxGUIAppTraits::WriteToStderr(const wxString& WXUNUSED(text))
+{
+ return false;
+}
+
+#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
+
+#endif // !__WXWINCE__
+