How can I create a guid in MFC

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 03:53:24

问题


I need to be able to create guids on the fly. Is there a way to do that in MFC? I see how to do it in .net, but we haven't gone there yet. If not, do you have pointers to some code I can use?


回答1:


GUID guid;
HRESULT hr = CoCreateGuid(&guid); 

// Convert the GUID to a string
_TUCHAR * guidStr;
UuidToString(&guid, &guidStr);

The application is responsible for calling RpcStringFree to deallocate the memory allocated for the string returned in the StringUuid parameter.




回答2:


   //don't forget to add Rpcrt4.lib to your project

    CString m_ListID(L"error");
    RPC_WSTR guidStr;
    GUID guid;
    HRESULT hr = CoCreateGuid(&guid);
    if (hr == S_OK)
    {
        if(UuidToString(&guid, &guidStr) == RPC_S_OK)
        {
            m_ListID = (LPTSTR)guidStr;
            RpcStringFree(&guidStr);
        }
    }



回答3:


You can use the COM function CoCreateGuid, e.g.:

GUID guid;
HRESULT hr = CoCreateGuid(&guid);



回答4:


Use the function UuidCreate to generate GUIDs :

UUID generated;

if (::UuidCreate(&generated) != RPC_S_OK)
throw std::exception(...);



回答5:


You can use this sample

WCHAR       GuidText[250] ={0};
UUID        uuid;

CoCreateGuid (&uuid);
wsprintf(
        GuidText,
        L"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
        uuid.Data1,
        uuid.Data2,
        uuid.Data3,
        uuid.Data4[0], uuid.Data4[1],
        uuid.Data4[2], uuid.Data4[3], uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]
        );


来源:https://stackoverflow.com/questions/556997/how-can-i-create-a-guid-in-mfc

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