From 6c80168e8c57172728350af844449b172950f648 Mon Sep 17 00:00:00 2001 From: Patedam Date: Sun, 12 Jan 2025 17:26:58 -0500 Subject: [PATCH] Can load dynamic library (dll) instead of linking against. Allow to not link against d3d12 lib --- Juliet/Juliet.vcxproj | 57 +++++++++++++- Juliet/include/Core/DynLib/DynamicLibrary.h | 14 ++++ .../src/Core/DynLib/Win32/DynamicLibrary.cpp | 75 +++++++++++++++++++ 3 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 Juliet/include/Core/DynLib/DynamicLibrary.h create mode 100644 Juliet/src/Core/DynLib/Win32/DynamicLibrary.cpp diff --git a/Juliet/Juliet.vcxproj b/Juliet/Juliet.vcxproj index 2cc8c2e..7279447 100644 --- a/Juliet/Juliet.vcxproj +++ b/Juliet/Juliet.vcxproj @@ -70,7 +70,7 @@ true - ws2_32.lib;d3d12.lib;dxgi.lib;dxguid.lib;$(CoreLibraryDependencies);%(AdditionalDependencies) + ws2_32.lib;dxgi.lib;dxguid.lib;$(CoreLibraryDependencies);%(AdditionalDependencies) @@ -112,6 +112,7 @@ + @@ -155,6 +156,60 @@ + + MultiThreadedDebugDll + Disabled + true + NoListing + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\ + false + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\ + Default + Default + Column + false + false + false + NotSet + Fast + Default + false + stdcpp20 + Default + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\ + false + Neither + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\ + Cdecl + Use + pch.h + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\Juliet.pch + false + false + false + false + false + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\ + true + true + false + Default + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\Juliet.tlog\ + true + false + Level3 + W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\ + ProgramDatabase + false + false + true + _DEBUG;JULIET_EXPORT;JULIET_WIN32;_WINDLL;_UNICODE;UNICODE; + false + true + true + Default + --target=amd64-pc-windows-msvc + diff --git a/Juliet/include/Core/DynLib/DynamicLibrary.h b/Juliet/include/Core/DynLib/DynamicLibrary.h new file mode 100644 index 0000000..aeeb0df --- /dev/null +++ b/Juliet/include/Core/DynLib/DynamicLibrary.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace Juliet +{ + struct DynamicLibrary + { + }; + + extern JULIET_API DynamicLibrary* LoadDynamicLibrary(const char* filename); + extern JULIET_API FunctionPtr LoadFunction(NonNullPtr lib, const char* functionName); + extern JULIET_API void UnloadDynamicLibrary(NonNullPtr lib); +} // namespace Juliet diff --git a/Juliet/src/Core/DynLib/Win32/DynamicLibrary.cpp b/Juliet/src/Core/DynLib/Win32/DynamicLibrary.cpp new file mode 100644 index 0000000..9367bfd --- /dev/null +++ b/Juliet/src/Core/DynLib/Win32/DynamicLibrary.cpp @@ -0,0 +1,75 @@ +#include + +#include +#include + +namespace Juliet +{ + namespace + { + // TODO : Move into string file + // Use portable code + pass the memory array into parameter and not use new + // This is from http://www.rohitab.com/discuss/topic/41257-char-to-lpcwstr/ + wchar_t* UTF8ToWideChar(const char* utf8) + { + wchar_t* w; + + int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, 0, 0); + if (len <= 0) + { + return nullptr; + } + + w = new wchar_t[len]; + + if (!w) + { + return nullptr; + } + + if (MultiByteToWideChar(CP_UTF8, 0, utf8, -1, w, len) <= 0) + { + delete[] w; + return nullptr; + } + + return w; + } + } // namespace + + DynamicLibrary* LoadDynamicLibrary(const char* filename) + { + if (!filename) + { + Log(LogLevel::Error, LogCategory::Core, "Library filename is invalid (empty)"); + return nullptr; + } + + LPWSTR wstr = UTF8ToWideChar(filename); + HMODULE handle = LoadLibraryW(wstr); + delete[] wstr; + + // Generate an error message if all loads failed + if (!handle) + { + Log(LogLevel::Error, LogCategory::Core, "Failed loading %s", filename); + return nullptr; + } + return reinterpret_cast(handle); + } + + FunctionPtr LoadFunction(NonNullPtr lib, const char* functionName) + { + auto function = (FunctionPtr)GetProcAddress(reinterpret_cast(lib.Get()), functionName); + if (!function) + { + Log(LogLevel::Error, LogCategory::Core, "Failed loading %s", functionName); + } + return function; + } + + void UnloadDynamicLibrary(NonNullPtr lib) + { + FreeLibrary(reinterpret_cast(lib.Get())); + } +} // namespace Juliet