Comparing two GUIDs for equality in C++
Asked Answered
S

3

5

I am looking for the easiest way to compare two GUIDs for equality in C++. Surely there is a predefined function for that.

The solution needs to work with Visual C++ 2010.

I am talking of GUID as defined in Guiddef.h:

typedef struct _GUID {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    unsigned char  Data4[ 8 ];
} GUID;
Selfesteem answered 6/8, 2012 at 20:10 Comment(3)
Have you looked at boost?Benedictus
Related: How to compare two GUID? — I posted my anser there as srgloureiroTrelu
Referenced in a meta question (in a comment and an answer).Graniela
V
12

Perhaps you want IsEqualGUID (which uses memcmp behind the scenes) or just use operator== (which calls IsEqualGUID for you).

Vedic answered 6/8, 2012 at 20:15 Comment(0)
A
2

Is the == operator not overloaded to do this for you? Or use IsEqualGUID.

Aggravate answered 6/8, 2012 at 20:14 Comment(0)
S
-2

Wine implementation in here: https://source.winehq.org/source/dlls/ole32/compobj.c

Is fast simple and obvious:

    /***********************************************************************
 *           IsEqualGUID [OLE32.@]
 *
 * Compares two Unique Identifiers.
 *
 * PARAMS
 *  rguid1 [I] The first GUID to compare.
 *  rguid2 [I] The other GUID to compare.
 *
 * RETURNS
 *  TRUE if equal
 */
#undef IsEqualGUID 
BOOL WINAPI IsEqualGUID(
    REFGUID rguid1, REFGUID rguid2)  {
  return !memcmp(rguid1, rguid2, sizeof(GUID));
}

That will compile with any Visual Studio version out there. That also is C so C++ aficionado might wrap it up in extern "C" { }

Simaroubaceous answered 25/9, 2020 at 13:29 Comment(1)
The OP is asking specifically for a solution that compiles with Visual Studio 2010. constexpr was introduced in C++11. Visual Studio 2010 does not support C++11.Samualsamuel

© 2022 - 2024 — McMap. All rights reserved.