MFC中获取GUID

GUID:全局唯一标识 GUID(Global Unique Identifier)

// 获取全局唯一标识 GUID(Global Unique Identifier) 头文件 include <objbase.h>
CString GetGUID()
{
    GUID guid;
    OLECHAR arrGUID[39]={0};
    CoCreateGuid(&guid);
    int ret = StringFromGUID2(guid, arrGUID, 39);
    return CString(arrGUID); // "{C876CF2F-8831-4AE1-AEAB-CFDA78EED36D}"
}

std::string GuidToString(const GUID &guid)
{
    char buf[39] = { 0 };
    sprintf_s(buf, sizeof(buf),
        "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
        guid.Data1, guid.Data2, guid.Data3,
        guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
        guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
    return std::string(buf);
}
//使用
    GUID guid;
    LRESULT ret = ::CoCreateGuid(&guid);//成功,返回0,即 S_OK
    std::string s2 = GuidToString(guid);

 2. 使用Poco,参考Poco学习(二)

// GUID 或者 UUID
#include <Poco/UUIDGenerator.h>
int main()
{
    Poco::UUID uuid;
    try
    {
        uuid = Poco::UUIDGenerator::defaultGenerator().create();//Creates a new time-based UUID
        std::string sRet = uuid.toString();//67440b59-4ad8-3f74-8bab-8b4ae76b4fc3
    }
    catch (Poco::Exception& exc)
    {
        std::cerr << exc.displayText() << std::endl;
        return 1;
    }
    return 0;
}

 

 

 

 

 

 

 

----------------------------

https://blog.csdn.net/semicloud/article/details/89332018

 

posted @ 2020-03-14 20:22  htj10  阅读(781)  评论(0编辑  收藏  举报
TOP