Move Dynamic library code out of util in to dynamic library file

This commit is contained in:
zilmar 2021-04-21 10:03:04 +09:30
parent cdb7cdcfd5
commit 3a038b2eb3
9 changed files with 73 additions and 54 deletions

View File

@ -39,6 +39,7 @@
<ItemGroup>
<ClCompile Include="CriticalSection.cpp" />
<ClCompile Include="DateTime.cpp" />
<ClCompile Include="DynamicLibrary.cpp" />
<ClCompile Include="File.cpp" />
<ClCompile Include="HighResTimeStamp.cpp" />
<ClCompile Include="IniFile.cpp" />
@ -58,6 +59,7 @@
<ItemGroup>
<ClInclude Include="CriticalSection.h" />
<ClInclude Include="DateTime.h" />
<ClInclude Include="DynamicLibrary.h" />
<ClInclude Include="File.h" />
<ClInclude Include="HighResTimeStamp.h" />
<ClInclude Include="IniFile.h" />

View File

@ -62,6 +62,9 @@
<ClCompile Include="Random.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DynamicLibrary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CriticalSection.h">
@ -121,5 +124,8 @@
<ClInclude Include="Random.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DynamicLibrary.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,51 @@
#include "DynamicLibrary.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#endif
DynLibHandle DynamicLibraryOpen(const char *pccLibraryPath, bool ShowErrors)
{
if (pccLibraryPath == nullptr)
{
return nullptr;
}
#ifdef _WIN32
UINT LastErrorMode = SetErrorMode(ShowErrors ? 0 : SEM_FAILCRITICALERRORS);
DynLibHandle Lib = (DynLibHandle)LoadLibraryA(pccLibraryPath);
SetErrorMode(LastErrorMode);
#else
pjutil::DynLibHandle Lib = (pjutil::DynLibHandle)dlopen(pccLibraryPath, RTLD_NOW);
#endif
return Lib;
}
void DynamicLibraryClose(DynLibHandle Lib)
{
if (Lib != nullptr)
{
#ifdef _WIN32
FreeLibrary((HMODULE)Lib);
#else
dlclose(Lib);
#endif
}
}
void * DynamicLibraryGetProc(DynLibHandle Lib, const char * ProcedureName)
{
if (ProcedureName == nullptr)
{
return nullptr;
}
#ifdef _WIN32
return GetProcAddress((HMODULE)Lib, ProcedureName);
#else
return dlsym(Lib, ProcedureName);
#endif
}

View File

@ -0,0 +1,7 @@
#pragma once
typedef void * DynLibHandle;
DynLibHandle DynamicLibraryOpen(const char * LibraryPath, bool ShowErrors = true);
void DynamicLibraryClose(DynLibHandle LibHandle);
void * DynamicLibraryGetProc(DynLibHandle LibHandle, const char * ProcedureName);

View File

@ -5,51 +5,9 @@
#include <windows.h>
#include <Tlhelp32.h>
#else
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <time.h>
#endif
pjutil::DynLibHandle pjutil::DynLibOpen(const char *pccLibraryPath, bool ShowErrors)
{
if (pccLibraryPath == nullptr)
{
return nullptr;
}
#ifdef _WIN32
UINT LastErrorMode = SetErrorMode(ShowErrors ? 0 : SEM_FAILCRITICALERRORS);
pjutil::DynLibHandle lib = (pjutil::DynLibHandle)LoadLibraryA(pccLibraryPath);
SetErrorMode(LastErrorMode);
#else
pjutil::DynLibHandle lib = (pjutil::DynLibHandle)dlopen(pccLibraryPath, RTLD_NOW);
#endif
return lib;
}
void * pjutil::DynLibGetProc(pjutil::DynLibHandle LibHandle, const char * ProcedureName)
{
if (ProcedureName == nullptr)
return nullptr;
#ifdef _WIN32
return GetProcAddress((HMODULE)LibHandle, ProcedureName);
#else
return dlsym(LibHandle, ProcedureName);
#endif
}
void pjutil::DynLibClose(pjutil::DynLibHandle LibHandle)
{
if (LibHandle != nullptr)
{
#ifdef _WIN32
FreeLibrary((HMODULE)LibHandle);
#else
dlclose(LibHandle);
#endif
}
}
void pjutil::Sleep(uint32_t timeout)
{
#ifdef _WIN32

View File

@ -4,11 +4,6 @@
class pjutil
{
public:
typedef void * DynLibHandle;
static DynLibHandle DynLibOpen(const char *pccLibraryPath, bool ShowErrors = true);
static void * DynLibGetProc(DynLibHandle LibHandle, const char * ProcedureName);
static void DynLibClose(DynLibHandle LibHandle);
static void Sleep(uint32_t timeout);
static bool TerminatedExistingExe();

View File

@ -263,7 +263,7 @@ void CGfxPlugin::UnloadPluginDetails(void)
WriteTrace(TraceGFXPlugin, TraceDebug, "start");
if (m_LibHandle != nullptr)
{
pjutil::DynLibClose(m_LibHandle);
DynamicLibraryClose(m_LibHandle);
m_LibHandle = nullptr;
}
memset(&m_GFXDebug, 0, sizeof(m_GFXDebug));

View File

@ -38,7 +38,7 @@ bool CPlugin::Load(const char * FileName)
// Try to load the plugin DLL
//Try to load the DLL library
m_LibHandle = pjutil::DynLibOpen(FileName, HaveDebugger());
m_LibHandle = DynamicLibraryOpen(FileName, HaveDebugger());
WriteTrace(PluginTraceType(), TraceDebug, "Loaded: %s LibHandle: %X", FileName, m_LibHandle);
if (m_LibHandle == nullptr)
@ -238,7 +238,7 @@ void CPlugin::UnloadPlugin()
}
if (m_LibHandle != nullptr)
{
pjutil::DynLibClose(m_LibHandle);
DynamicLibraryClose(m_LibHandle);
m_LibHandle = nullptr;
}

View File

@ -3,7 +3,7 @@
#include <Project64-core/Settings/DebugSettings.h>
#include <Project64-core/TraceModulesProject64.h>
#include <Project64-core/Plugins/Plugin.h>
#include <Common/Util.h>
#include <Common/DynamicLibrary.h>
#if defined(_WIN32)
#define CALL __cdecl
@ -53,14 +53,14 @@ protected:
void(CALL *SetSettingNotificationInfo)(PLUGIN_SETTINGS_NOTIFICATION *);
void(CALL *SetPluginNotification)(PLUGIN_NOTIFICATION *);
pjutil::DynLibHandle m_LibHandle;
DynLibHandle m_LibHandle;
bool m_Initialized, m_RomOpen;
PLUGIN_INFO m_PluginInfo;
// Loads a function pointer from the currently loaded DLL
void _LoadFunctionVoid(const char * szFunctionName, void ** functionPointer)
{
*functionPointer = pjutil::DynLibGetProc(m_LibHandle, szFunctionName);
*functionPointer = DynamicLibraryGetProc(m_LibHandle, szFunctionName);
}
// Simple wrapper around _LoadFunction() to avoid having to specify the same two arguments