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/>.
8 /** @file library_loader_win.cpp Implementation of the LibraryLoader for Windows */
10 #include "../../stdafx.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();
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());
36 this->error
= GetLoadError();
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()));
55 this->error
= GetLoadError();