我使用以下函数来创建 UUID 并将其以人类可读的形式写入预先分配的缓冲区中。出了问题。

void createUUID(char* pDst)
{
    UUID    lUUIDObj;
    UuidCreate(&lUUIDObj);
    unsigned char*  lDest = reinterpret_cast<unsigned char*>(pDst);
    UuidToStringA(&lUUIDObj, &lDest)
}

在该方法的最后,调试器会说:

  • lDest = 0x01fe4fd8“df4a5ed8-c0d2-495a-84d7-ce0e07cf2113”
  • pDst = 0x0012ec7c " ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ”

我以为两者的内容是相同的,但事实并非如此。

发生了什么? 谢谢。

有帮助吗?

解决方案

看着 文档 为了 UuidToStringA, , 它说:

RPC 运行时库为 StringUuid 参数中返回的字符串分配内存。

这意味着调用后 lDest 不再指向 pDst.

其他提示

void createUUID(char* pDst)
{
    UUID    lUUIDObj;
    UuidCreate(&lUUIDObj);
    unsigned char*  lDest = reinterpret_cast<unsigned char*>(pDst);
    //UuidToStringA(&lUUIDObj, &lDest);
    UuidToStringA(&lUUIDObj, lDest);
}
.

看起来您将lDest堵塞,而不更改其最初的值指向

为了完成 joachim桶轴的答案,这是纠正功能:

void createUUID(char* pDst)
{
    UUID    lUUIDObj;
    UuidCreate(&lUUIDObj);
    unsigned char*  lTmpStr;
    UuidToStringA(&lUUIDObj, &lTmpStr);
    sprintf(pDst, reinterpret_cast<char*>(lTmpStr));
    RpcStringFreeA(&lTmpStr);
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top