Update: Translations from eints
[openttd-github.git] / src / os / windows / library_loader_win.cpp
blob608b9349c98d9968f1734b1c245be44911e89f1b
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file library_loader_win.cpp Implementation of the LibraryLoader for Windows */
10 #include "../../stdafx.h"
12 #include <windows.h>
14 #include "../../library_loader.h"
15 #include "../../3rdparty/fmt/format.h"
17 #include "../../safeguards.h"
19 static std::string GetLoadError()
21 auto error_code = GetLastError();
23 wchar_t buffer[512];
24 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error_code,
25 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, static_cast<DWORD>(std::size(buffer)), nullptr) == 0) {
26 return fmt::format("Unknown error {}", error_code);
29 return FS2OTTD(buffer);
32 void *LibraryLoader::OpenLibrary(const std::string &filename)
34 void *h = ::LoadLibraryW(OTTD2FS(filename).c_str());
35 if (h == nullptr) {
36 this->error = GetLoadError();
39 return h;
42 void LibraryLoader::CloseLibrary()
44 HMODULE handle = static_cast<HMODULE>(this->handle);
46 ::FreeLibrary(handle);
49 void *LibraryLoader::GetSymbol(const std::string &symbol_name)
51 HMODULE handle = static_cast<HMODULE>(this->handle);
53 void *p = reinterpret_cast<void *>(::GetProcAddress(handle, symbol_name.c_str()));
54 if (p == nullptr) {
55 this->error = GetLoadError();
58 return p;