main.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. //! \param 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 = mhLib ? (MINIDUMPWRITEDUMP)::GetProcAddress(mhLib, "MiniDumpWriteDump") : 0;
  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. if (pDump)
  49. pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
  50. ::CloseHandle(hFile);
  51. }
  52. LONG WINAPI unhandledHandler(struct _EXCEPTION_POINTERS* apExceptionInfo)
  53. {
  54. createMinidump(apExceptionInfo);
  55. return EXCEPTION_CONTINUE_SEARCH;
  56. }
  57. #ifdef _DEBUG
  58. template<typename TChar, typename TTraits>
  59. class OutputDebugStringBuf : public std::basic_stringbuf<TChar, TTraits>
  60. {
  61. public:
  62. explicit OutputDebugStringBuf() : _buffer(1)
  63. {
  64. __super::setg(nullptr, nullptr, nullptr);
  65. __super::setp(_buffer.data(), _buffer.data(), _buffer.data() + _buffer.size());
  66. }
  67. ~OutputDebugStringBuf()
  68. {}
  69. static_assert(std::is_same<TChar, char>::value || std::is_same<TChar, wchar_t>::value, "OutputDebugStringBuf only supports char and wchar_t types");
  70. int sync() try
  71. {
  72. MessageOutputer<TChar, TTraits>()(__super::pbase(), __super::pptr());
  73. __super::setp(_buffer.data(), _buffer.data(), _buffer.data() + _buffer.size());
  74. return 0;
  75. }
  76. catch (...)
  77. {
  78. return -1;
  79. }
  80. int overflow(int c = TTraits::eof())
  81. {
  82. auto syncRet = sync();
  83. if (c != TTraits::eof())
  84. {
  85. _buffer[0] = c;
  86. __super::setp(_buffer.data(), _buffer.data() + 1, _buffer.data() + _buffer.size());
  87. }
  88. return syncRet == -1 ? TTraits::eof() : 0;
  89. }
  90. private:
  91. std::vector<TChar> _buffer;
  92. template<typename TChar, typename TTraits>
  93. struct MessageOutputer;
  94. template<>
  95. struct MessageOutputer<char, std::char_traits<char>>
  96. {
  97. template<typename TIterator>
  98. void operator()(TIterator begin, TIterator end) const
  99. {
  100. std::string s(begin, end);
  101. OutputDebugStringA(s.c_str());
  102. }
  103. };
  104. template<>
  105. struct MessageOutputer<wchar_t, std::char_traits<wchar_t>>
  106. {
  107. template<typename TIterator>
  108. void operator()(TIterator begin, TIterator end) const
  109. {
  110. std::wstring s(begin, end);
  111. OutputDebugStringW(s.c_str());
  112. }
  113. };
  114. };
  115. #endif
  116. #ifndef NO_MAIN
  117. int WINAPI WinMain(_In_ HINSTANCE hinst, _In_opt_ HINSTANCE hpinst, _In_ LPSTR cmd, int _In_ show)
  118. {
  119. #ifdef _DEBUG
  120. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  121. OutputDebugStringBuf<char, std::char_traits<char>> charDebugOutput;
  122. std::streambuf* buf = std::cout.rdbuf();
  123. std::cout.rdbuf(&charDebugOutput);
  124. #endif
  125. SetUnhandledExceptionFilter(unhandledHandler);
  126. Framework::initFramework(hinst);
  127. Framework::Startparam stp;
  128. stp.hinst = hinst;
  129. stp.hpinst = hpinst;
  130. stp.cmd = cmd;
  131. stp.show = show;
  132. int ret = Framework::Start(stp);
  133. Framework::releaseFramework();
  134. #ifdef _DEBUG
  135. std::cout.rdbuf(buf);
  136. #endif
  137. return ret;
  138. }
  139. #endif
  140. #endif