Barun
Barun

Reputation: 23

How to generate Universally Unique IDentifier, UUID from LoadRunner independent of the OS

I have experienced the need of generating UUID in LoadRunner several times while scripting but there is no in-build function to do so. I am using both linux and windows load generators.

Thanks to Scott Moore for writing the below code which uses windows in-build CoCreateGuid function (dependent on ole32.dll) to generate required UUID. However that code is completely dependent on windows platform and doesn't work in Linux platform.

How can we generate UUID from Loadrunner independent of OS?

    #include "lrun.h"
    #include "web_api.h"
    #include "lrw_custom_body.h"
    #include "stdlib.h"
    #include "stdio.h"
    #include "string.h"
    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];

        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]);

        lr_save_string(buf, "PAR_GUID");

        return 0;
    }

Upvotes: 0

Views: 4407

Answers (2)

Buzzy
Buzzy

Reputation: 2995

You can use the following trick which doesn't require any code. Define a hex parameter as follows: enter image description here

Then use it with this code: lr_eval_string("{MyHex}{MyHex}-{MyHex}-{MyHex}-{MyHex}-{MyHex}{MyHex}{MyHex}")

Upvotes: 1

Barun
Barun

Reputation: 23

I have come up with the below mentioned solution

int lr_guid_gen()
{
    char GUID[40];
    int t = 0;
    char *szTemp = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
    char *szHex = "0123456789abcdef-";
    int nLen = strlen (szTemp);


    for (t=0; t<nLen+1; t++)
    {
        int r = rand () % 16;
        char c = ' ';   

        switch (szTemp[t])
        {
            case 'x' : { c = szHex [r]; } break;
            case 'y' : { c = szHex [r & 0x03 | 0x08]; } break;
            case '-' : { c = '-'; } break;
            case '4' : { c = '4'; } break;
        }

        GUID[t] = ( t < nLen ) ? c : 0x00;
    }

    lr_save_string(GUID,"PAR_GUID");

    return 0;
}

Upvotes: 0

Related Questions