Make your own free website on Tripod.com

MKoD - D Programming Language

D Windows' DLL Example - code-name WinDLL.d

Very Kool! Building a "D" DLL example:

/+****************************************************************
 * Program       : WinDLL.DLL 
 * Source        : WinDLL.d
 * Author        : David L. 'SpottedTiger' Davis
 * Created Date  : 13.Jun.04 Compiled and Tested with dmd v0.93
 * Modified Date : 08.Jul.04 Compiled and Tested with dmd v0.95
 *               : 22.Jul.04 Compiled and Tested with dmd v0.96
 *               : 02.Oct.04 Compiled and Tested with dmd v0.102
 *               : 22.Dec.04 Compiled and Tested with dmd v0.109
 *               : 28.Feb.05 Compiled and Tested with dmd v0.114
 *               : 17.Jun.05 Compiled and Tested with dmd v0.127
 *               : 04.Jun.06 Compiled and Tested with dmd v0.160
 *               : 10.Jan.07 Compiled and Tested with dmd v1.0 
 *               :
 * Requirements  : std.c.windows.windows and std.string
 *               :
 * License       : Public Domain
 *****************************************************************
 *
 * Note: Needs std.string for the find() function.
 *	
 * Example of creating a Windows' DLL in D
 * To Compiled: C:\dmd\MKoD_ex>dmd WinDLL.d WinDLL.def
 +/

private import std.string;
private 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 GC
          break;

      case DLL_THREAD_ATTACH:
      case DLL_THREAD_DETACH:
          // Multiple threads not supported yet
          return false;
    }
    
    g_hInst = hInstance;
    
    return true;

} // end BOOL DllMain( HINSTANCE, ULONG, LPVOID )

/************************************************
 * Function      : bool isroman( in dchar )
 * Created Date  : 03.Jun.04 
 * Modified Date : (none)
 * Requirements  : std.string
 ************************************************
 *
 * Note: Needs std.string for the find() function.
 */
extern( Windows ) 
export bool isroman
(
    in dchar dc
)
{
    const char[] ROMAN = "IVXLCDMivxlcdm";

    return ( std.string.find( ROMAN, dc ) != -1 ) ? true : false;

} // end bool isroman( in dchar )

/************************************************
 * Function      : bool isromanstr( in char[] )
 * Created Date  : 03.Jun.04 
 * Modified Date : (none)
 * Requirements  : std.string
 ************************************************
 *
 * Note: Needs std.string for the find() function.
 */
extern( Windows ) 
export bool isromanstr
(
    in char[] s
)
{
    const char[] ROMAN = "IVXLCDMivxlcdm";
     
    foreach( int isp, char c; s )
    {
        if ( std.string.find( ROMAN, cast(char)c ) == -1 ) 
            return false;
    } 
      
    return true;
        
} // end bool isromanstr( in char[] )
; ** WinDLL.def **
LIBRARY      WinDLL
DESCRIPTION  'Windows example DLL written in D'

EXETYPE      NT
SUBSYSTEM    WINDOWS
CODE         PRELOAD DISCARDABLE
DATA         PRELOAD SINGLE

EXPORTS      isroman    @1
             isromanstr @2
Compiling WinDLL.d
C:\dmd\MKoD_ex>..\bin\dmd WinDLL.d WinDLL.def
C:\dmd\bin\..\..\dm\bin\link.exe WinDLL,,,user32+kernel32,WinDLL.def/noi;

C:\dmd\MKoD_ex>
Mars: fourth Rock from the Sun.