Skip to content
No description, website, or topics provided.
C++ C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Mocks
Test
.gitignore
IMsvDll.h
IMsvDllAdapter.h
IMsvDllDecorator.h
IMsvDllFactory.h
IMsvDllList.h
IMsvDllObject.h
LICENSE
MsvDll.cpp
MsvDll.h
MsvDllAdapter.cpp
MsvDllAdapter.h
MsvDllFactory.cpp
MsvDllFactory.h
MsvDllFactory_Factory.h
MsvDllList.cpp
MsvDllList.h
MsvDllMainHelper.h
MsvDll_Factory.h
README.md
doxyfile
mdllfactory.sln
mdllfactory.vcxproj
mdllfactory.vcxproj.filters

README.md

MarsTech DLL Factory

MarsTech DLL factory manages dynamic/shared libraries and provides interface to get DLL objects from loaded dynamic/shared libraries. It loads dynamic/shared libraries on demand and each library is loaded only once per executable binary. It unloads dynamic/shared libraries on demand or when the DLL factory is destroyed.

Installation

Download all MarsTech dependencies and put them to a same directory. Create a new subdirectory "3rdParty" and put there "3rdParty" dependencies (spdlog).

Dependencies

Configuration

No build configuration is needed - just build whole solution.

DLL Factory

DLL Factory is static library which should be linked to executable binary only (do not link it to dynamic/shared libraries). Only one instance should be created per one executable binary and should be injected to all objects which wants to load dynamic/shared libraries and theirs objects.

Example:

#include "mdllfactory/MsvDllFactory.h"
#include "mdllfactory/MsvDllList.h"
#include "msys/msys/MsvSysDll_Interface.h"

std::shared_ptr<MsvLogger> spLogger;

//create and initialize DLL list
std::shared_ptr<IMsvDllList> spDllList(new MsvDllList());
MSV_RETURN_FAILED(spDllList->AddDll(MSV_SYS_OBJECT_ID_LAST, "msys.dll"));

//create DLL Factory instance
std::shared_ptr<IMsvDllFactory> spDllFactory(new MsvDllFactory(spDllList, spLogger));

//get MarsTech C++ SYS Library interface
std::shared_ptr<IMsvSys_Last> spSys;
if (MSV_FAILED(errorCode = spDllFactory->GetDllObject(MSV_SYS_OBJECT_ID_LAST, spSys)))
{
	MSV_LOG_ERROR(spLogger, "Get SYS DLL object failed with error: {0:x}", errorCode);
	return errorCode;
}

DLLs Compatible With DLL Factory

There are two options how to create/get DLLs and theirs objects:

  • DLLs with exported GetDllObject function
  • Decorator for DLLs without GetDllObject function

DLLs With Exported GetDllObject Function

They are DLLs written for MarsTech DLL Factory and can be loaded by DLL Factory without any wrappers/adapters.

Example:

//this example is copied from MarsTech C++ SYS DLL implementation (https://github.com/Mars2004/msys/blob/master/msys/main.cpp)

#include "MsvSysDll_Interface.h"
#include "msys/msys_lib/MsvSys.h"
#include "mdllfactory/MsvDllMainHelper.h"

#include "merror/MsvErrorCodes.h"

std::recursive_mutex g_lock;
//IMsvSys interface inherits from IMsvDllObject
std::weak_ptr<IMsvDllObject> g_spSys;

//GetDllObject implementation (Windows/Linux support)
#ifdef _WIN32
MsvErrorCode GetDllObject(const char* id, std::shared_ptr<IMsvDllObject>& spDllObject)
#else
extern "C" MsvErrorCode GetDllObject(const char* id, std::shared_ptr<IMsvDllObject>& spDllObject)
#endif // _WIN32
{
	std::lock_guard<std::recursive_mutex> lock(g_lock);

	//convert to string for comparison
	std::string idString(id);

	//create and return DLL object when known id was used
	MSV_GETWEAKSHARED_DLLOBJECT_WITH_ID(idString, MSV_SYS_OBJECT_ID, MsvSys(), g_spSys, spDllObject);

	//uknown id (not found) -> error
	return MSV_NOT_FOUND_ERROR;
}

Decorator For DLLs Without GetDllObject Function

Decorator is usefull when you need to load DLL without exported GetDllObject function (3rd party DLLs, DLLs with C interface, etc.).

Example:

//this example is copied from MarsTech C++ SYS DLL implementation (https://github.com/Mars2004/msys/blob/master/msys/main.cpp) and decorates testdll_2 (https://github.com/Mars2004/mdllfactory/blob/master/Test/testdll_2/main.cpp)

#include  "mdllfactory/IMsvDllDecorator.h"

class TestDll2:
	public IMsvDllDecorator
{
public:

	TestDll2():
		m_pIncrementFunction(nullptr),
		m_pDecrementFunction(nullptr),
		m_pGetValueFunction(nullptr)
	{
	}

	int32_t Increment()
	{
		return m_pIncrementFunction();
	}

	int32_t Decrement()
	{
		return m_pDecrementFunction();
	}

	int32_t GetValue()
	{
		return m_pGetValueFunction();
	}

protected:
	virtual MsvErrorCode DecorateDllObject(const char* id, std::shared_ptr<IMsvDllAdapter> spMsvDllAdapter) override
	{
		void* pDllAddress = nullptr;

		MSV_RETURN_FAILED(spMsvDllAdapter->GetDllAddress("Increment", pDllAddress));
		m_pIncrementFunction = reinterpret_cast<int32_t(*)()>(pDllAddress);

		MSV_RETURN_FAILED(spMsvDllAdapter->GetDllAddress("Decrement", pDllAddress));
		m_pDecrementFunction = reinterpret_cast<int32_t(*)()>(pDllAddress);

		MSV_RETURN_FAILED(spMsvDllAdapter->GetDllAddress("GetValue", pDllAddress));
		m_pGetValueFunction = reinterpret_cast<int32_t(*)()>(pDllAddress);

		return MSV_SUCCESS;
	}

protected:
	int32_t(*m_pIncrementFunction)();
	int32_t(*m_pDecrementFunction)();
	int32_t(*m_pGetValueFunction)();
};

Usage Example

There is also an usage example which uses the most of MarsTech projects and libraries. Its source codes and readme can be found at:

Source Code Documentation

You can find generated source code documentation at https://www.marstech.cz/projects/mdllfactory/1.0.1/doc.

License

This project is released under GNU General Public License version 3. If you can not or do not want to accept GNU GPLv3 license and you would like to use this project under another license, please contact me on info@marstech.cz or visit www.marstech.cz.

You can’t perform that action at this time.