Version 15 of TclGetGUID

Updated 2015-02-22 12:45:26 by dkf

Scott Nichols Below is code for a C based Tcl extension GUID generator. The reason I wrote it was a customer of mine required a GUID be generated and sent with each SOAP transaction. The C code below is using two dependent libs: tclstub83.lib and rpcrt4.lib. Rpcrt4.lib is part of the Windows OS, and should come with Visual Studio or .Net. The source code is so short that I went ahead and posted both C source files below. The sources can be compiled into a Tcl library extension (DLL) for all Windows platforms.

TclGUID.h:

/*
 * TclGUID.h v1.0 12-11-2003 Scott Nichols
 *
 */

 /* TCL Function prototype declarations */
 #ifndef TclGUID_H
 #define TclGUID_H

 #define USE_NON_CONST
 #define TCL_USE_STUBS

 #include <tcl.h>
 #include "StdAfx.h"

 extern "C"
 {
    __declspec(dllexport) int Tclguid_Init(Tcl_Interp* interp);

 }

 static int        GetGUID_ObjCmd _ANSI_ARGS_((ClientData clientData,
                    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));

 #endif

TclGUID.cpp:

/*
 * TclGUID.cpp, v1.0 12/11/2003, Scott J. Nichols
 * [email protected]
 *
 * The GetGUID Tcl method returns a 128 bit unique identifier
 * to the Tcl interpreter. Microsoft calls it a globally unique identifier (GUID).
 * The application I am using it for is the transaction ID for SOAP messages. This was a customer
 * requirement of mine.
 *
 */

 #include "TclGUID.h"

 static int        GetGUID_ObjCmd _ANSI_ARGS_((ClientData clientData,
                    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]))
 {

        GUID guid;

        // create random GUID
        guid = GUID_NULL;
        ::CoCreateGuid(&guid);
        
        if (guid == GUID_NULL)
        {
                Tcl_Obj                *obj_result = Tcl_NewStringObj((const char *)"Unable to create GUID.",
                                strlen((const char *) "Unable to create GUID."));
                Tcl_SetObjResult(interp,obj_result);
                
                return TCL_ERROR;
        }
        else
        {
                BYTE * str;
                UuidToString((UUID*)&guid, &str);

                Tcl_UtfToUpper((char *)str); 
                
                // Return the GUID to the Tcl Interpreter
                Tcl_Obj        *obj_result = Tcl_NewStringObj((const char *)str,
                                      strlen((const char *)str));
                Tcl_SetObjResult(interp,obj_result);

                RpcStringFree(&str);

                return TCL_OK;
        }

 }

 /* Main Routine in the TCL Extension DLL */
 int Tclguid_Init(Tcl_Interp *interp)
 {

        #if TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION >= 1
                // Does the TCL interpreter support version 8.3 of TCL?
                if (Tcl_InitStubs(interp,"8.3",0) == NULL)
                        return TCL_ERROR;
        #endif

        Tcl_CreateObjCommand(interp, "GetGUID", GetGUID_ObjCmd,
                      (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

        return (Tcl_PkgProvide(interp,"TclGUID","1.0") == TCL_ERROR ? TCL_ERROR : TCL_OK);
 }

Each time the GetGUID method is called from Tcl a different 128 bit number is returned that looks similar to this: DE4ED408-5200-46E5-8AD1-EEF7351A7C07


PT: 8-July-2004: There is now a pure-Tcl uuid package in tcllib


The TWAPI package also includes commands for GUID and LUID generation using the native Win32 API