2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
24 #include "platform_specific.h"
27 #include <common/os/windows/windows.h>
28 #include <common/env.h>
29 #include <common/log.h>
38 // NOTE: This is needed in order to make CComObject work since this is not a real ATL project.
39 CComModule _AtlModule;
40 extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;
44 LONG WINAPI UserUnhandledExceptionFilter(EXCEPTION_POINTERS* info)
48 CASPAR_LOG(fatal) << L"#######################\n UNHANDLED EXCEPTION: \n"
49 << L"Adress:" << info->ExceptionRecord->ExceptionAddress << L"\n"
50 << L"Code:" << info->ExceptionRecord->ExceptionCode << L"\n"
51 << L"Flag:" << info->ExceptionRecord->ExceptionFlags << L"\n"
52 << L"Info:" << info->ExceptionRecord->ExceptionInformation << L"\n"
53 << L"Continuing execution. \n#######################";
55 CASPAR_LOG_CALL_STACK();
59 return EXCEPTION_CONTINUE_EXECUTION;
62 void setup_prerequisites()
64 SetUnhandledExceptionFilter(UserUnhandledExceptionFilter);
66 // Increase time precision. This will increase accuracy of function like Sleep(1) from 10 ms to 1 ms.
67 static struct inc_prec
69 inc_prec(){ timeBeginPeriod(1); }
70 ~inc_prec(){ timeEndPeriod(1); }
74 void change_icon(const HICON hNewIcon)
76 auto hMod = ::LoadLibrary(L"Kernel32.dll");
77 typedef DWORD(__stdcall *SCI)(HICON);
78 auto pfnSetConsoleIcon = reinterpret_cast<SCI>(::GetProcAddress(hMod, "SetConsoleIcon"));
79 pfnSetConsoleIcon(hNewIcon);
83 void setup_console_window()
85 auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);
87 // Disable close button in console to avoid shutdown without cleanup.
88 EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_GRAYED);
89 DrawMenuBar(GetConsoleWindow());
90 //SetConsoleCtrlHandler(HandlerRoutine, true);
92 // Configure console size and position.
93 auto coord = GetLargestConsoleWindowSize(hOut);
96 SetConsoleScreenBufferSize(hOut, coord);
98 SMALL_RECT DisplayArea = { 0, 0, 0, 0 };
99 DisplayArea.Right = coord.X - 1;
100 DisplayArea.Bottom = (coord.Y - 1) / 2;
101 SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
103 change_icon(::LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(101)));
105 // Set console title.
106 std::wstringstream str;
107 str << "CasparCG Server " << env::version() << L" x64 ";
108 #ifdef COMPILE_RELEASE
110 #elif COMPILE_PROFILE
112 #elif COMPILE_DEVELOP
117 SetConsoleTitle(str.str().c_str());
120 void increase_process_priority()
122 SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
125 void wait_for_keypress()
127 boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
128 std::system("pause");
131 std::shared_ptr<void> setup_debugging_environment()
135 hLogFile = CreateFile(L"crt_log.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
136 std::shared_ptr<void> crt_log(nullptr, [](HANDLE h){::CloseHandle(h); });
138 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
139 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
140 _CrtSetReportFile(_CRT_WARN, hLogFile);
141 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
142 _CrtSetReportFile(_CRT_ERROR, hLogFile);
143 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
144 _CrtSetReportFile(_CRT_ASSERT, hLogFile);
152 void wait_for_remote_debugging()
155 MessageBox(nullptr, L"Now is the time to connect for remote debugging...", L"Debug", MB_OK | MB_TOPMOST);