Make your own free website on Tripod.com

MKoD - D Programming Language

Console Program that uses the WinDLL.dll - code-name Main_WinAPI.d

Very Kool! Console Executable that calls WinDLL.dll with Windows' APIs:

/+******************************************************************
 * Program       : Main_WinAPI.exe 
 * Source        : Main_WinAPI.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, std.string, and std.stdio
 *               :   
 * License       : Public Domain 
 *******************************************************************
 *
 * Example of using WinDLL.dll's imported functions
 * To Compiled: C:\dmd\MKoD_ex>dmd Main_WinAPI.d
 +/
private import std.stdio;
private import std.string;
private import std.c.windows.windows;

// set up pointers to the free functions - default parameter work fine!
extern( Windows ) typedef bool ( *pfn_isroman )( in dchar );
extern( Windows ) typedef bool ( *pfn_isromanstr )( in char[] );

/+
 ' Calls in two functions from a 'D' DLL
 ' Needs: std.c.windows.windows for Windows API calls
 '        std.stdio for writefln()
 '        std.string for toStringz(), and find()
 '        WinDLL.dll for isroman() and isromanstr() functions
 +/
int main ( in char[][] args )
{
    HINSTANCE hLibWinDLL = null; 
    char[]   sLibName   = "";  
    char[]   sPath      = "";

    sPath = args[0][ 0 .. std.string.rfind( args[ 0 ], r"\" ) ].dup;
    sLibName = sPath ~ r"\WinDLL.dll";
    
    // Load DLL
    hLibWinDLL = LoadLibraryA( std.string.toStringz( sLibName ) );
    
    writefln("sLibName=\"%s\", hLibWinDLL=%X", sLibName, cast(int)hLibWinDLL );
    
    if ( hLibWinDLL <= cast(HINSTANCE)0)
    {
        writefln( "WinDLL not found!" );
        hLibWinDLL = null;

        return( -1 );
    }
  
    pfn_isroman    isroman    = cast(pfn_isroman)GetProcAddress( hLibWinDLL, "isroman" ); 
    pfn_isromanstr isromanstr = cast(pfn_isromanstr)GetProcAddress( hLibWinDLL, "isromanstr" ); 
   
    writefln( "Testing isroman( \'%s\' ) = %b", 'C', isroman( 'C' ) ); 
    writefln( "Testing isromanstr( \"%s\" ) = %b", "MMVC", isromanstr( "MMVC" ) ); 
    writefln( "Testing isromanstr( \"%s\" ) = %b", "ABCDEF", isromanstr( "ABCDEF" ) );

    // Unload DLL
    FreeLibrary( hLibWinDLL );

    return 0;

} // end int main( in char[][] )
Compile main_winapi.d
C:\dmd\MKOD_ex>..\bin\dmd main_winapi.d
C:\dmd\bin\..\..\dm\bin\link.exe main_winapi,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>main_winapi
sLibName="C:\dmd\MKOD_ex\WinDLL.dll", hLibWinDLL=10000000
Testing isroman( 'C' ) = 1
Testing isromanstr( "MMVC" ) = 1
Testing isromanstr( "ABCDEF" ) = 0

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