| 1 |
#include "UNsmPlugin.h" |
| 2 |
|
| 3 |
#include <array> |
| 4 |
|
| 5 |
#include "../../Common/UNsmConsts.h" |
| 6 |
#include "../../Common/dummy.h" |
| 7 |
|
| 8 |
#include "DllUtils.h" |
| 9 |
|
| 10 |
namespace Regnessem |
| 11 |
{ |
| 12 |
namespace System |
| 13 |
{ |
| 14 |
// NsmPlugin::FindByModuleName ----------------------------------------------- |
| 15 |
|
| 16 |
NsmPlugin::FindByModuleName::FindByModuleName(const tstring &s) |
| 17 |
:x(s) |
| 18 |
{} |
| 19 |
|
| 20 |
bool NsmPlugin::FindByModuleName::operator ()(const NsmPluginListSub &v) const |
| 21 |
{ |
| 22 |
return v->PluginInfo.ModuleName == x; |
| 23 |
} |
| 24 |
|
| 25 |
// NsmPlugin ------------------------------------------------------------------ |
| 26 |
|
| 27 |
NsmPlugin::NsmPlugin(const tstring &filePath) |
| 28 |
:DllHandle(LoadLibrary(filePath.c_str())) |
| 29 |
,HGetPluginInfo() |
| 30 |
,HInitialize() |
| 31 |
,HTerminate() |
| 32 |
{ |
| 33 |
if(!DllHandle) |
| 34 |
return; |
| 35 |
|
| 36 |
#if defined(_WINDOWS) |
| 37 |
std::array<TCHAR, MAX_PATH> buf; |
| 38 |
if(GetModuleFileName(DllHandle, buf.data(), std::tuple_size<decltype(buf)>::value)) |
| 39 |
FileName = buf.data(); |
| 40 |
else |
| 41 |
FileName = filePath; |
| 42 |
#else |
| 43 |
FileName = filePath; |
| 44 |
#endif |
| 45 |
|
| 46 |
HGetPluginInfo = reinterpret_cast<TGetPluginInfo*>(ProcAddress("GetPluginInfo")); |
| 47 |
HInitialize = reinterpret_cast<TInitialize*>(ProcAddress("Initialize")); |
| 48 |
HTerminate = reinterpret_cast<TTerminate*>(ProcAddress("Terminate")); |
| 49 |
|
| 50 |
PluginInfo.ApiVersion = GetPluginInfoEx(NMPI_APIVER); |
| 51 |
PluginInfo.ModuleName = GetPluginInfoEx(NMPI_MODULENAME); |
| 52 |
PluginInfo.DisplayName = GetPluginInfoEx(NMPI_TITLE); |
| 53 |
PluginInfo.Description = GetPluginInfoEx(NMPI_DESCRIPTION); |
| 54 |
PluginInfo.Author = GetPluginInfoEx(NMPI_AUTHOR); |
| 55 |
PluginInfo.Copyright = GetPluginInfoEx(NMPI_COPYRIGHT); |
| 56 |
PluginInfo.PluginVersion = GetPluginInfoEx(NMPI_PLUGINVER); |
| 57 |
} |
| 58 |
|
| 59 |
NsmPlugin::~NsmPlugin() |
| 60 |
{ |
| 61 |
if(DllHandle) |
| 62 |
FreeLibrary(DllHandle); |
| 63 |
} |
| 64 |
|
| 65 |
FARPROC NsmPlugin::ProcAddress(LPCSTR procName) const |
| 66 |
{ |
| 67 |
#ifdef WINCE |
| 68 |
return GetProcAddress(DllHandle, c2w(procName).c_str()); |
| 69 |
#else |
| 70 |
return GetProcAddress(DllHandle, procName); |
| 71 |
#endif |
| 72 |
} |
| 73 |
|
| 74 |
bool NsmPlugin::IsLoaded() const |
| 75 |
{ |
| 76 |
return DllHandle != NULL; |
| 77 |
} |
| 78 |
|
| 79 |
int NsmPlugin::GetPluginInfo(int infoNo, LPSTR lpBuffer, int nSize) const |
| 80 |
{ |
| 81 |
if(HGetPluginInfo) |
| 82 |
return HGetPluginInfo(infoNo, lpBuffer, nSize); |
| 83 |
else |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
tstring NsmPlugin::GetPluginInfoEx(int infoNo) const |
| 88 |
{ |
| 89 |
std::array<char, 256> buf; |
| 90 |
if(0 < GetPluginInfo(infoNo, buf.data(), std::tuple_size<decltype(buf)>::value)) |
| 91 |
return c2t(buf.data()); |
| 92 |
else |
| 93 |
return tstring(); |
| 94 |
} |
| 95 |
|
| 96 |
int NsmPlugin::Initialize(PNsmPluginInitInfo lpPluginInitInfo) const |
| 97 |
{ |
| 98 |
if(HInitialize) |
| 99 |
return HInitialize(lpPluginInitInfo); |
| 100 |
else |
| 101 |
return NsmPluginState::NotImplementedError; |
| 102 |
} |
| 103 |
|
| 104 |
int NsmPlugin::Terminate() const |
| 105 |
{ |
| 106 |
if(HTerminate) |
| 107 |
return HTerminate(); |
| 108 |
else |
| 109 |
return NsmPluginState::NotImplementedError; |
| 110 |
} |
| 111 |
} |
| 112 |
} |