PIP 5.5.3
Platform-Independent Primitives
Public Member Functions | List of all members
PILibrary Class Reference

Run-time library. More...

#include <pilibrary.h>

Public Member Functions

 PILibrary (const PIString &path_=PIString())
 Constructs PILibrary and load if "path_" not empty.
 
 ~PILibrary ()
 Destroy PILibrary, unload if library was loaded.
 
bool load (const PIString &path_)
 Load library with relative or absolute path "path_".
 
bool load ()
 Load library with path() path.
 
void unload ()
 Unload library if it was loaded.
 
void * resolve (const char *symbol)
 Obtain exported library method with name "symbol". More...
 
bool isLoaded () const
 Returns if library successfully loaded.
 
PIString path () const
 Returns library path.
 
PIString lastError () const
 Returns last occured error in human-readable format.
 

Detailed Description

Run-time library.

Synopsis

PILibrary allow you dynamically load external library and use some methods from it. PILibrary instance contains library pointer and unload library on destructor, so recommended to use it with new creation.

Main method of PILibrary is resolve(const char *), which returns void* pointer to requested method. One should test it to nullptr and convert it in pointer to the required method.

In case of C++ libraries it`s very important to use C-linkage of exported methods! You may also need to mark methods for export, e.g. __declspec(dllexport). You can include <piplugin.h> and use PIP_PLUGIN_EXPORT to mark exports with PIP.

extern "C" {
__declspec(dllexport) int exportedSum(int,int);
__declspec(dllexport) int exportedMul(int,int);
}

Usage

Library:

#include <piplugin.h>
extern "C" {
PIP_PLUGIN_EXPORT int exportedSum(int,int);
PIP_PLUGIN_EXPORT int exportedMul(int,int);
}
int exportedSum(int a, int b) {
return a + b;
}
int exportedMul(int a, int b) {
return a * b;
}
Plugin control.

Program:

int main(int argc, char * argv[]) {
typedef int(*MyFunc)(int,int);
PILibrary * lib = new PILibrary();
if (lib->load("mylib.dll")) {
MyFunc fadd = (MyFunc)lib->resolve("exportedSum");
MyFunc fmul = (MyFunc)lib->resolve("exportedMul");
if (fadd) {
int sum = fadd(1, 2);
piCout << "sum =" << sum;
} else {
piCout << "Can`t resolve" << "exportedSum";
}
if (fmul) {
int mul = fadd(10, 20);
piCout << "mul =" << mul;
} else {
piCout << "Can`t resolve" << "exportedMul";
}
} else {
piCout << lib->lastError();
}
delete lib;
}
// sum = 3
// mul = 30
Run-time library.
Definition: pilibrary.h:37
PILibrary(const PIString &path_=PIString())
Constructs PILibrary and load if "path_" not empty.
Definition: pilibrary.cpp:135
#define piCout
Macro used for conditional (piDebug) output to PICout(StdOut)
Definition: picout.h:35

Member Function Documentation

◆ resolve()

void * PILibrary::resolve ( const char *  symbol)

Obtain exported library method with name "symbol".

Returns exported method with name "symbol" from loaded library. Method have to use C-linkage and marked to export, according to compiler specification.
C-linkage doesn`t provide information about return type and arguments, so you should manually convert obtained pointer to required method format before call.

Returns

void* pointer to method or
nullptr if method doesn`t exists or library not loaded