AverageAtBest
AverageAtBest

Reputation: 31

Usage of GUID in C++:

I want to ask if declaration of GUID in C++ differs from C#. I encountered this in C#:

GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}; 

Is this viable in C++? If not how can it be converted? Thanks.

Upvotes: 1

Views: 10559

Answers (4)

Roman Ryltsov
Roman Ryltsov

Reputation: 69652

The easiest perhaps (and convenient too) will be to leverage MS specific __declspec(uuid(...)):

class __declspec(uuid("{C1F400A4-3F08-11D3-9F0B-006008039E37}")) NullRenderer;
GUID x = __uuidof(NullRenderer);

This way you don't even have to split the GUID components in source code, yet have it as true GUID declaration.

Upvotes: 3

bash.d
bash.d

Reputation: 13207

The GUID-structure differs from C#. It is defined

typedef struct _GUID 
{  
   DWORD Data1;  
   WORD Data2;  
   WORD Data3;  
   BYTE Data[8];
} GUID;

If you want to treat it as a variable you'll do it:

GUID guid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}};

Try it and also look here: SO

Upvotes: 2

MSalters
MSalters

Reputation: 179809

The definition of GUID in <windows.h> is compatible with this syntax. It also works in C.

Upvotes: 3

Arne Mertz
Arne Mertz

Reputation: 24596

There is no GUID type defined in standard C++. However, you could (and maybe some compiler vendors have) define a struct GUID that could be initialized the same way.

edit: Microsoft defines GUID as follows:

typedef struct _GUID {
  DWORD Data1;
  WORD  Data2;
  WORD  Data3;
  BYTE  Data4[8];
} GUID;

With this definition, your code should work as it is.

Upvotes: 2

Related Questions