Print a GUID variable

前端 未结 11 1682
孤城傲影
孤城傲影 2021-02-01 02:12

I have a GUID variable and I want to write inside a text file its value. GUID definition is:

typedef struct _GUID {          // size is 16
    DWORD Data1;
             


        
相关标签:
11条回答
  • 2021-02-01 02:21

    In case when your code uses ATL/MFC you also could use CComBSTR::CComBSTR(REFGUID guid) from atlbase.h:

    GUID guid = ...;
    const CComBSTR guidBstr(guid);  // Converts from binary GUID to BSTR
    const CString guidStr(guidBstr); // Converts from BSTR to appropriate string, ANSI or Wide
    

    It will make conversion & memory cleanup automatically.

    0 讨论(0)
  • 2021-02-01 02:23

    Use the StringFromCLSID function to convert it to a string

    e.g.:

    GUID guid;
    CoCreateGuid(&guid);
    
    OLECHAR* guidString;
    StringFromCLSID(guid, &guidString);
    
    // use guidString...
    
    // ensure memory is freed
    ::CoTaskMemFree(guidString);
    

    Also see the MSDN definition of a GUID for a description of data4, which is an array containing the last 8 bytes of the GUID

    0 讨论(0)
  • 2021-02-01 02:26

    Use UuidToString function to convert GUID to string. The function accepts UUID type which is typedef of GUID.

    0 讨论(0)
  • 2021-02-01 02:28

    You can eliminate the need for special string allocations/deallocations by using StringFromGUID2()

    GUID guid = <some-guid>;
    // note that OLECHAR is a typedef'd wchar_t
    wchar_t szGUID[64] = {0};
    StringFromGUID2(&guid, szGUID, 64);
    
    0 讨论(0)
  • 2021-02-01 02:29

    Sometimes its useful to roll your own. I liked fdioff's answer but its not quite right. There are 11 elements of different sizes.

    printf("Guid = {%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}", 
      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]);
    
    Output: "Guid = {44332211-1234-ABCD-EFEF-001122334455}"
    

    Refer to Guiddef.h for the GUID layout.

    Same, as a method:

    void printf_guid(GUID guid) {
        printf("Guid = {%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}", 
          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]);
    }
    

    you can also pass a CLSID to this method.

    0 讨论(0)
  • 2021-02-01 02:29

    Courtesy of google's breakpad project:

    std::string ToString(GUID *guid) {
        char guid_string[37]; // 32 hex chars + 4 hyphens + null terminator
        snprintf(
              guid_string, sizeof(guid_string),
              "%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 guid_string;
    }
    
    UUID guid = {0};
    UuidCreate(&guid);
    std::cout << GUIDToString(&guid);
    
    0 讨论(0)
提交回复
热议问题