DEFINE_GUID
uses kind of a semi-ugly hack that works in C, but not in C++.
When you use DEFINE_GUID
, it tacks an extern
on the beginning of the code it generates. In C, this is fairy harmless--you end up with what's called a "tentative definition". A tentative definition is kind of a halfway point between a declaration and a definition. On one hand, you can have multiple tentative definitions of the same name, and as long as they don't conflict (like trying to give it different types) that's all right. But it will also act as a definition if there's no non-tentative definition visible.
The closest equivalent of this in C++ would be an inline
variable definition (which can also have multiple occurrences, as long as they don't conflict, and they all resolve to a single object in the end). But, as it stands right now, the macro expands to something that includes extern
on the beginning, and in C++ that's strictly a declaration, not a definition, so when/if you try to use this in C++, you don't have a definition anywhere, and linking fails.
Reference
https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/defining-and-exporting-new-guids
Putting a GUID definition outside statements that prevent multiple inclusion does not cause multiple instances of the GUID in a driver because DEFINE_GUID defines the GUID as an EXTERN_C variable. Multiple declarations of an EXTERN variable are allowed as long as the types match.