Why is IsEqualGUID () and "operator ==" for a GUID declared to return an int?

The Windows SDK functions the IsEqualGUID () function and operator==() for two GUIDs that return BOOL (equivalent to int ):

 // Guiddef.h #ifdef __cplusplus __inline int IsEqualGUID(REFGUID rguid1, REFGUID rguid2) { return !memcmp(&rguid1, &rguid2, sizeof(GUID)); } #else // ! __cplusplus #define IsEqualGUID(rguid1, rguid2) (!memcmp(rguid1, rguid2, sizeof(GUID))) #endif //also in Guiidef.h #ifdef __cplusplus __inline int operator==(REFGUID guidOne, REFGUID guidOther) { return IsEqualGUID(guidOne,guidOther); } #endif 

What is the point of this int ? I understand that C is not of type BOOL , but there is #ifdef __cplusplus , so this code will only be compiled as C ++, so BOOL will be supported anyway. The presence of negation near memcmp() effectively converts all possible values โ€‹โ€‹returned from memcmp() to zero and non-zero.

In addition, C does not have only user-defined operators. Only C ++ supports them. Thus, operator== will not compile in C code.

Are there any reasons for choosing int instead of BOOL here?

+7
c ++ visual-c ++ com
source share
2 answers

Because the Windows API provides IsEqualGUID() function that returns BOOL . They need to maintain a stable interface. BOOL and BOOL are different sizes, and the Windows API is designed to be compatible with different languages โ€‹โ€‹and compilers. Remember that there are languages โ€‹โ€‹other than C ++ that interact with the Windows API.

In C and C ++, IsEqualGUID() is implemented in terms of memcmp() , but IsEqualGUID() also implemented in ole32.dll . You can get the function from ole32.dll :

 REFGUID guid1 = IID_IUnknown; REFGUID guid2 = IID_AsyncIUnknown; typedef BOOL (WINAPI *IsEqualGUIDFuncType)(REFGUID, REFGUID); HMODULE h = ::LoadLibrary("ole32.dll"); IsEqualGUIDFuncType f = reinterpret_cast<IsEqualGUIDFuncType> (::GetProcAddress(h, "IsEqualGUID")); if(f != NULL) { if(f(guid1, guid2) != 0) ::printf("true\n"); else ::printf("false\n"); } ::FreeLibrary(h); 

Thus, although it is implemented as a built-in function in C ++, other languages โ€‹โ€‹can use the IsEqualGUID() DLL implementation. The C ++ version returns BOOL , so it is compatible with the API.

+7
source share

Are there any reasons for choosing int instead of bool here?

There may be several reasons:

  • The consistency of the interface between C and C ++ may be required by developers.

  • backward compatibility: the Windows SDK was supported (roughly) backwards compatible with the big bang (or right after that). This ensures that the client code will require less effort to connect to the new version of Windows, and that new versions of Windows will have a larger application base when they are released (old applications that work with new Windows with minimal effort).

  • for compatibility with C

    you may need to use only C types,
  • The boolean type may not be supported on some platforms - for example, in the development of Windows CE (I'm not sure if this is true).

+2
source share

All Articles