MKoD - D Programming Language

CreateGUID example - code-name createGUID.d

Creates a new GUID from Windows' APIs
// ** CreateGUID.d ** : Creates a new GUID from Windows.
// compiled with: C:\dmd>bin\dmd createguid.d ole32.lib uuid.lib -L/map

import std.c.stdio;
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, sGUID, 39 );
    }
     
    for ( int ix = 0; ix < sGUID.length - 1; ix++ )
    {
        // printf( "sGUID[ %d ] = %c\n", ix, sGUID[ ix ] );
        sGUID2 ~= sGUID[ ix ];
    } 
  
    return sGUID2; 
    
} // end char[] CreateGUID()

int main()
{
    printf( "New GUID: %.*s\n", CreateGUID() );
    
    return 0;
} // end int main()

C:\dmd\ex1>..\bin\dmd createguid.d ole32.lib uuid.lib -L/map
C:\dmd\bin\..\..\dm\bin\link.exe createguid,,,ole32.lib+uuid.lib+user32+kernel32/noi/map;

C:\dmd\ex1>createGUID
New GUID: {D42898CE-D959-41A8-B751-0A8B2A579ECB}

C:\dmd\ex1>

.............................................................. ................................................................................. ................................................................................. .................................................................................