Updated 2015-02-22 12:52:58 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(ClientData clientData,
                    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);

#endif

TclGUID.cpp:
/*
 * TclGUID.cpp, v1.0 12/11/2003, Scott J. Nichols
 * scott.nichols4@comcast.net
 *
 * 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(ClientData clientData,
                    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
        GUID guid = GUID_NULL;

        // create random GUID
        ::CoCreateGuid(&guid);

        // check for failure
        if (guid == GUID_NULL) {
                Tcl_SetObjResult(interp, Tcl_NewStringObj((const char *)"Unable to create GUID.", -1));
                return TCL_ERROR;
        }

        BYTE * str;
        UuidToString((UUID*)&guid, &str);
        Tcl_UtfToUpper((char *)str); 

        // Return the GUID to the Tcl Interpreter
        Tcl_SetObjResult(interp, Tcl_NewStringObj((const char *)str, -1));
        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");
}

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