module winex.dynloader; import windows; enum Linkage { Windows, C, D } alias Linkage.Windows Windows; alias Linkage.C C; alias Linkage.D D; class SharedLibrary { private HMODULE sysModule; protected this( char[] path ) { sysModule = LoadLibraryA( path ); if ( !sysModule ) throw new Exception( "unable to load shared library " ~ path ); } public FARPROC getFunctionAddress( char[] funcName ) { FARPROC fp = GetProcAddress( sysModule, cast(char*)funcName ); if ( !fp ) throw new Exception( "unable to get func address " ~ funcName ); return fp; } private static SharedLibrary[char[]] loaded; public static SharedLibrary loadLibrary( char[] path ) { SharedLibrary sl; if ( (sl = loaded[path]) === null ) return loaded[path] = new SharedLibrary( path ); return sl; } } template Dyn( T ) { T require( char[] lib, char[] name ) { return cast(T)((SharedLibrary.loadLibrary( lib )).getFunctionAddress( name )); } void use( out T ef, char[] lib, char[] name ) { ef = require( lib, name ); } } template DynProc( Linkage T : Linkage.D ) { void erf() { throw new Exception( "Shared Function not loaded" ); } typedef void (*fp)() = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.Windows ) { extern ( Windows ) { void erf() { throw new Exception( "Shared Function not loaded" ); } typedef void (*fp)() = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.C ) { extern (C) { void erf() { throw new Exception( "Shared Function not loaded" ); } typedef void (*fp)() = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.D, P ) { void erf( P p ) { throw new Exception( "Shared Function not loaded" ); } typedef void (*fp)( P ) = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.Windows, P ) { extern (Windows) { void erf( P p ) { throw new Exception( "Shared Function not loaded" ); } typedef void (*fp)( P ) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.C, P ) { extern ( C ) { void erf( P p ) { throw new Exception( "Shared Function not loaded" ); } typedef void (*fp)( P ) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.D, P0, P1 ) { void erf( P0 p0, P1 p1 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef void (*fp)( P0, P1 ) = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.Windows, P0, P1 ) { extern (Windows) { void erf( P0 p0, P1 p1 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef void (*fp)( P0, P1 ) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynProc( Linkage T : Linkage.C, P0, P1 ) { extern ( C ) { void erf( P0 p0, P1 p1 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef void (*fp)( P0, P1 ) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T ) { instance DynProc( T ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T, R : void ) { instance DynProc( T ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.D, R ) { R erf() { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)() = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.C, R ) { extern ( C ) { R erf() { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)() = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.Windows, R ) { extern ( Windows ) { R erf() { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)() = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.D, R : void, P ) { instance DynProc( T, P ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.C, R : void, P ) { instance DynProc( T, P ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.Windows, R : void, P ) { instance DynProc( T, P ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.D, R, P ) { R erf( P p ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)(P) = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.Windows, R, P ) { extern ( Windows ) { R erf( P p ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)(P) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.C, R, P ) { extern( C ) { R erf( P p ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)(P) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.D, R : void, P0, P1 ) { instance DynProc( T, P0, P1 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.Windows, R : void, P0, P1 ) { instance DynProc( T, P0, P1 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.C, R : void, P0, P1 ) { instance DynProc( T, P0, P1 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.D, R, P0, P1 ) { R erf( P0 p0, P1 p1 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (* fp)(P0,P1) = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.Windows, R, P0, P1 ) { extern( Windows ) { R erf( P0 p0, P1 p1 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (* fp)(P0,P1) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.C, R, P0, P1 ) { extern ( C ) { R erf( P0 p0, P1 p1 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (* fp)(P0,P1) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.D, R : void, P0, P1, P2 ) { instance DynProc( T, P0, P1, P2 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.Windows, R : void, P0, P1, P2 ) { instance DynProc( T, P0, P1, P2 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } template DynFunc( Linkage T : Linkage.C, R : void, P0, P1, P2 ) { instance DynProc( T, P0, P1, P2 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } /* template DynFunc( Linkage T, R : void, P0, P1, P2 ) { instance DynProc( T, P0, P1, P2 ) realTemplate; alias realTemplate.erf erf; alias realTemplate.fp fp; alias realTemplate.use use; alias realTemplate.require require; } */ template DynFunc( Linkage T : Linkage.D, R, P0, P1, P2 ) { R erf( P0 p0, P1 p1, P2 p2 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)(P0, P1, P2) = &erf; instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.Windows, R, P0, P1, P2 ) { extern ( Windows ) { R erf( P0 p0, P1 p1, P2 p2 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)(P0, P1, P2) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; } template DynFunc( Linkage T : Linkage.C, R, P0, P1, P2 ) { extern ( C ) { R erf( P0 p0, P1 p1, P2 p2 ) { throw new Exception( "Shared Function not loaded" ); return R.init; } typedef R (*fp)(P0, P1, P2) = &erf; } instance Dyn( fp ) dynFunc; alias dynFunc.use use; alias dynFunc.require require; }