Merge branch 'master' into jgrpp
# Conflicts: # src/os/windows/crashlog_win.cpp # src/os/windows/font_win32.cpp # src/os/windows/win32.cpp # src/os/windows/win32.h # src/video/win32_v.cpp # src/video/win32_v.h
This commit is contained in:
59
src/os/windows/library_loader_win.cpp
Normal file
59
src/os/windows/library_loader_win.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file library_loader_win.cpp Implementation of the LibraryLoader for Windows */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "../../library_loader.h"
|
||||
#include "../../core/format.hpp"
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
static std::string GetLoadError()
|
||||
{
|
||||
auto error_code = GetLastError();
|
||||
|
||||
char buffer[512];
|
||||
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error_code,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, sizeof(buffer), nullptr) == 0) {
|
||||
return fmt::format("Unknown error {}", error_code);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void *LibraryLoader::OpenLibrary(const std::string &filename)
|
||||
{
|
||||
void *h = ::LoadLibraryW(OTTD2FS(filename).c_str());
|
||||
if (h == nullptr) {
|
||||
this->error = GetLoadError();
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
void LibraryLoader::CloseLibrary()
|
||||
{
|
||||
HMODULE handle = static_cast<HMODULE>(this->handle);
|
||||
|
||||
::FreeLibrary(handle);
|
||||
}
|
||||
|
||||
void *LibraryLoader::GetSymbol(const std::string &symbol_name)
|
||||
{
|
||||
HMODULE handle = static_cast<HMODULE>(this->handle);
|
||||
|
||||
void *p = reinterpret_cast<void *>(::GetProcAddress(handle, symbol_name.c_str()));
|
||||
if (p == nullptr) {
|
||||
this->error = GetLoadError();
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
Reference in New Issue
Block a user