Back to Blog

LoadRunner GUID Creator

If you need to create a GUID on the fly for a unique ID, here is some code to do just that. Thanks to Chris Butts for this.

Here is the GUID function. Set this somewhere outside the Action() function to keep it separate.

GUID()
{
    lr_guid_gen();
    lr_message("%s", lr_eval_string(""));
    lr_message("%s", lr_eval_string(""));
}

int lr_guid_gen()
{
    typedef struct _GUID
    {
        unsigned long Data1;
        unsigned short Data2;
        unsigned short Data3;
        unsigned char Data4[8];
    } GUID;

    GUID m_guid;
    char buf[50];
    char bufx[50];

    lr_load_dll ("ole32.dll");

    CoCreateGuid(&m_guid);

    sprintf (buf, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
    m_guid.Data1, m_guid.Data2, m_guid.Data3,
    m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3],
    m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7]);

    sprintf (bufx, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
    m_guid.Data1, m_guid.Data2, m_guid.Data3,
    m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3],
    m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7]);

    lr_save_string(buf, "lrGUID");
    lr_save_string(bufx, "lrGUIDx");

    lr_output_message(lr_eval_string(buf));
    lr_output_message(lr_eval_string(bufx));

    return 0;
}

Here is is an example of calling it in the Action file:

Action()
{
    lr_guid_gen();
    lr_message("%s", lr_eval_string(""));
    lr_message("%s", lr_eval_string(""));

    //--- Code here ------

    lr_guid_gen();
    lr_message("%s", lr_eval_string(""));
    lr_message("%s", lr_eval_string(""));

    return 0;
}

Here is a sample of the output log when you run the code

Starting action Action.
GUID.c(39): {7CE64D06-3CD7-44DA-927A-28A7A6B7CD9F}
GUID.c(40): {7ce64d06-3cd7-44da-927a-28a7a6b7cd9f}
GUID.c(39): {41C4025C-11FC-4E79-A211-010C12D2BAF7}
GUID.c(40): {41c4025c-11fc-4e79-a211-010c12d2baf7}
Ending action Action.

Back to Blog