Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a GUID variable

Tags:

c++

guid

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;     WORD   Data2;     WORD   Data3;     BYTE  Data4[8]; } GUID; 

But I want to write its value like:

CA04046D-0000-0000-0000-504944564944 

I observed that:

  • Data1 holds the decimal value for CA04046D
  • Data2 holds the decimal value for 0
  • Data3 holds the decimal value for next 0

But what about the others?

I have to interpret myself this values in order to get that output or is there a more direct method to print such a variable?

like image 899
Jeni Avatar asked Nov 04 '09 09:11

Jeni


1 Answers

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.

like image 193
JustinB Avatar answered Sep 27 '22 21:09

JustinB