Just copy-paste might save you some time. rand_s is pretty safe on Windows since it uses RtlGenRandom(), but you’ll have to define _CRT_RAND_S first before <stdlib.h>

Assuming that the cpp somewhere in the class has: m_fd = open("/dev/urandom", O_RDONLY);

Original code: (thanks jacketzhong)

sds CRedLock::GetUniqueLockId()

{

unsigned char buffer[20];

if (_read(m_fd, buffer, sizeof(buffer)) == sizeof(buffer)) {

//获取20byte的随机数据

sds s; s = sdsempty();

for (int i = 0; i < 20; i++)

{

s = sdscatprintf(s, "%02X", bufferIdea); } return s;

}

else

{

//读取失败 printf("Error: GetUniqueLockId %d\n", __LINE__); }

return NULL;

}

Windows CPP compatible:

note: rand_s is not an ANSI standard.

sds CRedLock::GetUniqueLockId()
{
    //获取20byte的随机数据
    auto retVal = sdsempty();
    auto max = 20 / sizeof(unsigned int);
    for (int i = 0; i < max; i++)
    {
        unsigned int number;
        if (rand_s(&number) == 0)
        {           
            retVal = sdscatprintf(retVal, "%08X", number);           
        }
    }
    return retVal;
}