MKoD - D Programming Language

CreateGUID example - code-name CreateGUID.d

Very Kool! Creates a new GUID from Windows' APIs

/+******************************************************************
 * Program       : CreateGUID.exe (needs dmd v0.95+)
 * Source        : CreateGUID.d
 * Author        : David L. 'SpottedTiger' Davis
 * Created Date  : 15.Jun.04 Compiled with dmd v0.93
 * Modified Date : 08.Jul.04 Compiled and Tested with dmd v0.95
 *               : 02.Oct.04 Compiled and Tested with dmd v0.102
 *               : 28.Feb.05 Compiled and Tested with dmd v0.114
 *               : 04.Jun.06 Compiled and Tested with dmd v0.160
 *               : 10.Jan.07 Compiled and Tested with dmd v1.0 
 * Requirements  : std.c.windows.com, and std.stdio
 * License       : Public Domain 
 *******************************************************************
 *
 * Example of creating a new GUID with Windows 32-bit APIs
 * To Compiled: C:\dmd\MKoD_ex>dmd CreateGUID.d ole32.lib uuid.lib -L/map
 +/

/+
 ' NOTE - Before compiling, you may need to do the following:
 ' --------------------------------------------------------------
 ' In the "C:\dmd\src\phobos\std\c\windows\com.d" file, find
 ' line "int StringFromGUID2(GUID *rguid, LPOLESTR lpsz, int cbMax);"
 ' and add this line below it: "HRESULT CoCreateGuid(GUID *rguid);"
 +/

private import std.stdio;
private import std.c.windows.com;

extern( Windows )
{
    long CoCreateGuid( GUID *pGUID );
    int  StringFromGUID2( GUID *rguid, LPOLESTR lpsz, int cbMax );
}

char[] CreateGUID()
{
    GUID        udtGUID;
    wchar[ 39 ] sGUID = '\0';
    long        lResult;
    char[]      sGUID2 = "";
    
    lResult = CoCreateGuid( &udtGUID );
    
    if ( lResult < 0 )
        return "";
    else
    {
        StringFromGUID2( &udtGUID, cast(wchar *)sGUID, 39 );
    }
     
    for ( int ix = 0; ix < sGUID.length - 1; ix++ )
    {
        //writefln( "sGUID[ %d ] = %s", ix, sGUID[ ix ] );
        sGUID2 ~= sGUID[ ix ];
    } 
  
    return sGUID2; 
    
} // end char[] CreateGUID()

int main()
{
    writefln( "New GUID: %s", CreateGUID() );
    
    return 0;
} // end int main()
C:\dmd\MKOD_ex>..\bin\dmd createguid.d ole32.lib uuid.lib
C:\dmd\bin\..\..\dm\bin\link.exe createguid,,,ole32.lib+uuid.lib+user32+kernel32/noi;

C:\dmd\MKOD_ex>createguid
New GUID: {5E530D37-0AD0-428D-B67C-768D64F75C36}

C:\dmd\MKoD_ex>createguid
New GUID: {B41AD21C-DE4A-4C92-B174-F42CF893F4EF}

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