MKoD - D Programming LanguageDemystifying D DLLs - Part I : Exporting Free FunctionsPrologue: Well to start off (as of July 2004), I couldn't find any good sources of information, or examples about D DLLs within the D Community of sites. And although the main DigitalMars D web site does carry a LibMain() basic example with a .DEF file, which does creates a DLL file, it doesn't have an example on how to use it beyond that point. Thus upon the creation of this site, it has become one of my main goals to discover more about how to build and use D created DLLs...and then share that newfound knowledge with my fellow D developers.
Below I'd like to walk thru a few pointers I used when creating the
"D Financial Package" DLL (please download it, as I'll be using both financial_dll.d and
financial_dll.def files in examples) that I was able to then explicitly load all its functions into a D application
.exe for use (much like Windows' does in loading a DLL into a Windows application).
import std.c.windows.windows; HINSTANCE g_hInst; extern( C ) { void gc_init(); void gc_term(); void _minit(); void _moduleCtor(); void _moduleUnitTests(); } extern( Windows ) BOOL DllMain ( HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved ) { switch( ulReason ) { case DLL_PROCESS_ATTACH: gc_init(); // initialize GC _minit(); // initialize module list _moduleCtor(); // run module constructors _moduleUnitTests(); // run module unit tests break; case DLL_PROCESS_DETACH: gc_term(); // shut-down the GC break; case DLL_THREAD_ATTACH: DLL_THREAD_DETACH: // Multiple threads not supported yet break; } g_hInst = hInstance; return true; } and a very basic .def Module Definition File:LIBRARY MYDLL DESCRIPTION 'My DLL written in D' EXETYPE NT CODE PRELOAD DISCARDABLE DATA PRELOAD SINGLE EXPORTS 2) Next, to export the functions into the DLL, the keywords "extern( D )" and "export" (which are in red below) were added to the functions. Please don't add anything to the .def file's "Exports" section at this time.
extern( D )
export real fv
(
in real rRate,
in real rNPer,
in real rPmt,
in real rPV = 0,
in uint uiType = 0
)
{
// code ...
return rValue;
} // end fv( in real, in real, in real, in real = 0, in uint = 0 )
3) Once the above has been done, compile the code: dmd financial_dll.d financial_dll.def
4) In the above screenshot that's pointing to the "real fv(real,real,real,real,uint)" function, we can see in the "Function" column that its D Decorated Name became "D13financial_dll2fvFeeeekZe", and after a little bit of research I discovered some useful information as shown below.Pulling fv()'s "D13financial_dll2fvFeeeekZe" Decorated Name apart, we find that:
5) So what can be done with this information?
|