Last active
August 29, 2015 14:08
-
-
Save MrSmith33/8750dd43c0843d45ccf8 to your computer and use it in GitHub Desktop.
Module loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmd -debug -g app.d imodule.d | |
del app.obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module app; | |
import std.stdio : writeln, writefln; | |
import std.string : toStringz; | |
import imodule; | |
version (Posix) | |
{ | |
pragma(lib, "dl"); | |
version (Linux) | |
{ | |
private import std.c.linux.linux; | |
} | |
else | |
{ | |
extern( C ) nothrow | |
{ | |
const int RTLD_NOW = 2; | |
void *dlopen( const( char )* file, int mode ); | |
int dlclose( void* handle ); | |
void *dlsym( void* handle, const( char* ) name ); | |
const( char )* dlerror(); | |
} | |
} | |
alias void* SharedLibHandle; | |
private | |
{ | |
SharedLibHandle LoadSharedLib( string libName ) | |
{ | |
return dlopen( libName.toStringz(), RTLD_NOW ); | |
} | |
void UnloadSharedLib( SharedLibHandle hlib ) | |
{ | |
dlclose( hlib ); | |
} | |
void* GetSymbol( SharedLibHandle hlib, string symbolName ) | |
{ | |
return dlsym( hlib, symbolName.toStringz() ); | |
} | |
string GetErrorStr() | |
{ | |
import std.conv : to; | |
auto err = dlerror(); | |
if( err is null ) | |
return "Uknown Error"; | |
return to!string( err ); | |
} | |
} | |
} | |
else version (Windows) | |
{ | |
import core.sys.windows.windows; | |
alias SharedLibHandle = HMODULE; | |
private | |
{ | |
SharedLibHandle LoadSharedLib( string libName ) | |
{ | |
return LoadLibraryA( libName.toStringz() ); | |
} | |
void UnloadSharedLib( SharedLibHandle hlib ) | |
{ | |
FreeLibrary( hlib ); | |
} | |
void* GetSymbol( SharedLibHandle hlib, string symbolName ) | |
{ | |
return GetProcAddress( hlib, symbolName.toStringz() ); | |
} | |
string GetErrorStr() | |
{ | |
import std.windows.syserror; | |
return sysErrorString( GetLastError() ); | |
} | |
} | |
} | |
else | |
{ | |
static assert(0); | |
} | |
IModule loadSharedModule(string file) | |
{ | |
import core.runtime; | |
SharedLibHandle handle; | |
void* farproc; | |
handle = cast(SharedLibHandle) Runtime.loadLibrary(file); | |
if (handle is null) | |
{ | |
writeln("Error loading ", file); | |
GetErrorStr().writeln; | |
return null; | |
} | |
farproc = GetSymbol(handle, "getModuleInstance"); | |
if (farproc is null) | |
{ | |
writeln("Error loading symbol getModuleInstance()"); | |
GetErrorStr().writeln; | |
return null; | |
} | |
alias ModuleFactory = IModule function(); | |
return (*cast(ModuleFactory)farproc)(); | |
} | |
class ModuleManager : IModuleManager | |
{ | |
IModule[string] modules; | |
void registerModule(IModule moduleInstance) | |
{ | |
assert(moduleInstance); | |
modules[moduleInstance.name] = moduleInstance; | |
} | |
void initModules() | |
{ | |
foreach(IModule m; modules) | |
{ | |
m.init(this); | |
writefln("Inited module %s", m.name); | |
} | |
} | |
override IModule findModule(string moduleName) | |
{ | |
return modules[moduleName]; | |
} | |
} | |
void main() | |
{ | |
ModuleManager modman = new ModuleManager; | |
modman.registerModule(loadSharedModule("./sharedmodule1.so")); | |
modman.registerModule(loadSharedModule("./sharedmodule2.so")); | |
modman.initModules; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmd -c -ofapp.o app.d imodule.d -debug -g | |
dmd app.o -L-ldl -defaultlib=libphobos2.so | |
rm app.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module dll; | |
version(Windows): | |
import core.sys.windows.windows; | |
import core.sys.windows.dll; | |
__gshared HINSTANCE g_hInst; | |
extern (Windows) | |
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) | |
{ | |
switch (ulReason) | |
{ | |
case DLL_PROCESS_ATTACH: | |
g_hInst = hInstance; | |
dll_process_attach( hInstance, true ); | |
break; | |
case DLL_PROCESS_DETACH: | |
dll_process_detach( hInstance, true ); | |
break; | |
case DLL_THREAD_ATTACH: | |
dll_thread_attach( true, true ); | |
break; | |
case DLL_THREAD_DETACH: | |
dll_thread_detach( true, true ); | |
break; | |
default: | |
assert(0); | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module imodule; | |
interface IModule | |
{ | |
string name(); | |
void init(IModuleManager moduleman); | |
void* realInterface(); | |
} | |
interface IModuleManager | |
{ | |
IModule findModule(string moduleName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment