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

巧了我就是萌 提交于 2019-12-13 02:55:54

问题


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;
    }

回答1:


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;
}



回答2:


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

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



来源:https://stackoverflow.com/questions/58202093/how-to-generate-universally-unique-identifier-uuid-from-loadrunner-independent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!