Get GUID

Generating a Universal Unique ID.

A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally unique identifier (GUID) is used interchangably, particular to IDs created by Microsoft. When generated according to accepted standards, UUIDs are supposedly, unique. The service program has multiple procedures and was created to generate two distinct types of UUID. The getGUID procedure will return a (version 1-type) formmated UUID. The procedure, getGUIDV4 returns a (version 4-type) UUID.

GETGUID Service Program

This service program binds to a system Machine Instruction, (MI) code to generate the UUID. Another system MI instruction is used to convert the character value to hex code. The service program returns a Version 1 stye UUID, or via a separate procedure, a Version 4 UUID.

       Ctl-Opt Option( *NoDebugIO : *SrcStmt ) thread(*serialize) noMain ;

       //*-----------------------------------------------------------
       //* Module for service program GETGUID
       //* Create using *CALLER and binding directory QC2LE
       //* CRTSRVPGM SRVPGM(GETGUID) EXPORT(*SRCFILE) BNDDIR(QC2LE)
       //*----------------------------------------------------------

      /copy qrpglesrc,getguid_pr

       //*-----------------------------------------
       Dcl-Proc getGUID       Export ;
       //*-----------------------------------------
       Dcl-Pi   getGUID       char(36)      End-Pi ;

       Dcl-DS UUID_template   ;
          UtBytPrv              uns(10) Inz( %Size( UUID_template )) ;
          UtBytAvl              uns(10)                              ;
          *N                    char(8) Inz( *Allx'00' )             ;
          UUID                  char(16)                             ;
          Reply                 char(1)                              ;
       End-DS ;

          Dcl-Pr GenUuid           ExtProc('_GENUUID')   ;
             UUID_template         pointer   Value       ;
          End-Pr ;

       Dcl-s UUIDval            char(16) ;
       Dcl-s hexVal             char(52) ;
       Dcl-s formattedUUID      char(36) ;

       GenUuid( %Addr( UUID_template )) ;
       uuidVal = UUID ;

       hexval = convertCharToHex(UUIDval) ;

       formattedUUID = %subst(hexval :  1 :  8) + '-'
                     + %subst(hexval :  9 :  4) + '-'
                     + %subst(hexval : 13 :  4) + '-'
                     + %subst(hexval : 17 :  4) + '-'
                     + %subst(hexval : 21 : 12) ;

       return formattedUUID ;

       End-Proc getGUID              ;

       //*-----------------------------------------
       Dcl-Proc convertCharToHex     Export ;
       //*-----------------------------------------
       Dcl-Pi   convertCharToHex        Char(52)  ;
         charIn                    Char(26) const ;
       End-Pi                                     ;

       Dcl-S charval            Char(26)  ;
       Dcl-s hexval             Char(52)  ;

       charval = charIN ;

       CharToHex (%addr(hexval)
                  : %addr(charval)
                  : %size(hexval))  ;

       Return hexVal ;

       End-Proc convertCharToHex            ;

       //*-----------------------------------------
       Dcl-Proc convertHexToChar     Export ;
       //*-----------------------------------------
       Dcl-Pi   convertHexToChar        Char(26)  ;
         hexIN                     Char(52) const ;
       End-Pi                                     ;

       Dcl-S charval            Char(26)  ;
       Dcl-s hexval             Char(52)  ;

       hexval = hexIN ;

       HexToChar (%addr(charval)
                  : %addr(hexval)
                  : %size(charval)*2)  ;

       Return charval ;

       End-Proc convertHexToChar            ;

       //=============================================
       Dcl-Proc getGUIDV4     Export ;
       Dcl-Pi   getGUIDV4     char(36)      End-Pi ;
       //=============================================

       //--------------------------------------------------
       // Declare variables
       //--------------------------------------------------

       DCL-S generateUUIDObj OBJECT(*JAVA: 'GenerateUUID' );
       DCL-S getUUIDRet OBJECT(*JAVA : 'java.lang.String' );
       Dcl-S byteString    object(*JAVA:'java.lang.String') ;
       Dcl-S returnString  object(*JAVA:'java.lang.String') ;
       Dcl-S newUUID       varchar(128) ;

       //-------------------------------------------
       // Declare Prototypes for Java and procedures
       //-------------------------------------------

       Dcl-Pr syscmd            int(10)  extproc('system')     ;
          cmd                   pointer value options(*string) ;
       End-Pr                                                  ;

       Dcl-PR getBytes  varChar(65535)   EXTPROC(*JAVA:
                                         'java.lang.String':
                                         'getBytes') ;
       End-PR ;

       DCL-PR generateUUIDObjCtor OBJECT(*JAVA : 'GenerateUUID' )
              EXTPROC(*JAVA : 'GenerateUUID' : *CONSTRUCTOR);
       END-PR ;

       DCL-PR getUUID OBJECT(*JAVA : 'java.lang.String' ) EXTPROC(*JAVA : 'G+
              enerateUUID' : 'getUUID' );
       END-PR ;

       //------------------------------------------------
       // 1. set enviroment variable
       // 2. Instantiate object of Java class GenerateUUID
       // 3. Call Java method:
       //------------------------------------------------

       sysCmd('ADDENVVAR ENVVAR(CLASSPATH) VALUE(''/MISCJAVA'')');

       IF         generateUUIDObj = *NULL;
          EVAL       generateUUIDObj = generateUUIDObjCtor;
       ENDIF;

       getUUIDRet = getUUID( generateUUIDObj );

       newUUID = getBytes(getUUIDRet); // Convert to character

       return newUUID                ;

       End-Proc getGUIDV4            ;



Version 1 UUID

Note:
The _GENGUID value is not a character value. To return a character string the vaule generated must be converted. Hence, the service program includes a procedure for converting the 16-byte value into hex-representation. The hexidecimal value is then used to format the 36-byte UUID string.
A0102C04-E040-19FD-BEE3-0004AC1CADA0
The GETGUID procedure will return a 36-character variable, representing the formatted UUID code. All the calling program needs for the argument list is a 36-byte character variable to receive the UUID.