macmade / dlib

Dynamic loading library for C/C++
MIT License
21 stars 8 forks source link

Usage documentation #13

Open IngwiePhoenix opened 5 years ago

IngwiePhoenix commented 5 years ago

Hello!

I am not an expert on programming on Windows, so I am not too familiar with the special things that need to be taken care of when using LoadLibrary, GetProcAddr and FreeLibrary - but I do know that for all the dl*() functions.

While looking for a small abstraction above the two, I came across this library - but I actually closed the page down multiple times, but kept finding back here through Google and Github. Now, I can not really see how to properly utilize this library, as I see no docs...

What I want to do is implement native module loading for (the Wren programming language)[http://wren.io]. Therefore, I would need to do the following:

The two main functions that need to be implemented are just there to properly bootstrap a module - it is supposed to return a struct with a set of functions and configuration, while the de-initialization function does the opposite, and makes sure all handles/pointers are freed properly.

So, how do I realize this quickly using this library? I only want to load one module at a time and store them within a module cache, so I can iterate through that and destruct/free them at that moment?

Kind regards, Ingwie.

macmade commented 5 years ago

You can see a working example in the unit test file: https://github.com/macmade/dlib/blob/master/Unit-Tests/Tests.cpp

Relevant part is:

#include <dlib.hpp>

extern int hv_vcpu_run(  unsigned int cpu );
extern int hv_vm_foobar( uint64_t flags );

DLIB_FUNC_START( Hypervisor, int, hv_vcpu_run, unsigned int cpu )
DLIB_FUNC_RET(   Hypervisor, int, hv_vcpu_run, cpu )

DLIB_FUNC_START( Hypervisor, int, hv_vm_foobar, uint64_t flags )
DLIB_FUNC_RET(   Hypervisor, int, hv_vm_foobar, flags )

DLIB_FUNC_START and DLIB_FUNC_RET will generate stubs that will load the module if necessary, load the function, and call it.