main.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef main_H
  2. #define main_H
  3. #ifdef _DEBUG
  4. #include <iostream>
  5. #include <sstream>
  6. #include <vector>
  7. #ifndef _LTMDB
  8. #define _CRTDBG_MAP_ALLOC
  9. #include <stdlib.h>
  10. #include <crtdbg.h>
  11. #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
  12. #define new DEBUG_CLIENTBLOCK
  13. #define _LTMDB
  14. #endif
  15. #endif
  16. #define WIN32_LEAN_AND_MEAN
  17. #include <Windows.h>
  18. #pragma warning( push )
  19. #pragma warning( disable : 4091 )
  20. #include <DbgHelp.h>
  21. #pragma warning( pop )
  22. #define KSGStart __stdcall
  23. #include "Globals.h"
  24. namespace Framework
  25. {
  26. // Speichert die dem Programm vom Betriebssystem beim Start übergebenen Parameter
  27. struct Startparam
  28. {
  29. HINSTANCE hinst, hpinst;
  30. LPSTR cmd;
  31. int show;
  32. };
  33. // Überschreibe diese Funktion. Sie wird vom Framework automatisch beim Start des Programmes aufgerufen
  34. // p: Die Parameter, die dem Programm beim Start vom Betriebssystem übergeben wurden
  35. int KSGStart Start( Startparam p );
  36. }
  37. typedef BOOL( __stdcall *MINIDUMPWRITEDUMP )( HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType, CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam );
  38. void createMinidump( struct _EXCEPTION_POINTERS *apExceptionInfo )
  39. {
  40. HMODULE mhLib = ::LoadLibrary( "dbghelp.dll" );
  41. MINIDUMPWRITEDUMP pDump = ( MINIDUMPWRITEDUMP )::GetProcAddress( mhLib, "MiniDumpWriteDump" );
  42. HANDLE hFile = ::CreateFile( "error_core_memory_dump.dmp", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
  43. FILE_ATTRIBUTE_NORMAL, NULL );
  44. _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
  45. ExInfo.ThreadId = ::GetCurrentThreadId();
  46. ExInfo.ExceptionPointers = apExceptionInfo;
  47. ExInfo.ClientPointers = FALSE;
  48. pDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL );
  49. ::CloseHandle( hFile );
  50. }
  51. LONG WINAPI unhandledHandler( struct _EXCEPTION_POINTERS *apExceptionInfo )
  52. {
  53. createMinidump( apExceptionInfo );
  54. return EXCEPTION_CONTINUE_SEARCH;
  55. }
  56. #ifdef _DEBUG
  57. template<typename TChar, typename TTraits>
  58. class OutputDebugStringBuf : public std::basic_stringbuf<TChar, TTraits>
  59. {
  60. public:
  61. explicit OutputDebugStringBuf() : _buffer( 1 )
  62. {
  63. setg( nullptr, nullptr, nullptr );
  64. setp( _buffer.data(), _buffer.data(), _buffer.data() + _buffer.size() );
  65. }
  66. ~OutputDebugStringBuf()
  67. {}
  68. static_assert( std::is_same<TChar, char>::value || std::is_same<TChar, wchar_t>::value, "OutputDebugStringBuf only supports char and wchar_t types" );
  69. int sync() try
  70. {
  71. MessageOutputer<TChar, TTraits>()( pbase(), pptr() );
  72. setp( _buffer.data(), _buffer.data(), _buffer.data() + _buffer.size() );
  73. return 0;
  74. } catch( ... )
  75. {
  76. return -1;
  77. }
  78. int overflow( int c = TTraits::eof() )
  79. {
  80. auto syncRet = sync();
  81. if( c != TTraits::eof() )
  82. {
  83. _buffer[ 0 ] = c;
  84. setp( _buffer.data(), _buffer.data() + 1, _buffer.data() + _buffer.size() );
  85. }
  86. return syncRet == -1 ? TTraits::eof() : 0;
  87. }
  88. private:
  89. std::vector<TChar> _buffer;
  90. template<typename TChar, typename TTraits>
  91. struct MessageOutputer;
  92. template<>
  93. struct MessageOutputer<char, std::char_traits<char>>
  94. {
  95. template<typename TIterator>
  96. void operator()( TIterator begin, TIterator end ) const
  97. {
  98. std::string s( begin, end );
  99. OutputDebugStringA( s.c_str() );
  100. }
  101. };
  102. template<>
  103. struct MessageOutputer<wchar_t, std::char_traits<wchar_t>>
  104. {
  105. template<typename TIterator>
  106. void operator()( TIterator begin, TIterator end ) const
  107. {
  108. std::wstring s( begin, end );
  109. OutputDebugStringW( s.c_str() );
  110. }
  111. };
  112. };
  113. #endif
  114. int WINAPI WinMain( _In_ HINSTANCE hinst, _In_opt_ HINSTANCE hpinst, _In_ LPSTR cmd, int _In_ show )
  115. {
  116. #ifdef _DEBUG
  117. _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  118. OutputDebugStringBuf<char, std::char_traits<char>> charDebugOutput;
  119. std::streambuf * buf = std::cout.rdbuf();
  120. std::cout.rdbuf( &charDebugOutput );
  121. #endif
  122. SetUnhandledExceptionFilter( unhandledHandler );
  123. Framework::initFramework( hinst );
  124. Framework::Startparam stp;
  125. stp.hinst = hinst;
  126. stp.hpinst = hpinst;
  127. stp.cmd = cmd;
  128. stp.show = show;
  129. int ret = Framework::Start( stp );
  130. Framework::releaseFramework();
  131. #ifdef _DEBUG
  132. std::cout.rdbuf( buf );
  133. #endif
  134. return ret;
  135. }
  136. #endif