Search code examples
c++windowsc++-winrt

IJsonValue.GetObject Method collides with windows.h macro


https://learn.microsoft.com/en-us/uwp/api/windows.data.json.ijsonvalue.getobject?view=winrt-22621

#include <winrt/Windows.Data.Json.h>

#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
//in windows.h
//#define GetObject  GetObjectW

IJsonValue jsonValue = ...;
auto jsonObject = jsonValue.GetObject();

error C2039: 'GetObjectW': is not a member of 'winrt::Windows::Data::Json::IJsonValue'

I tried to use the `int max = (std::numeric_limits::max)();` trick but still got an `GetObjectW` error ``` auto jsonObject = jsonValue.(winrt::Windows::Data::Json::IJsonValue::GetObject)(); ```

Any ideas?


Solution

  • ``` #include #ifdef GetObject #undef GetObject #endif ```

    Raymond guessed correctly in the comments that I wasn't including windows.h before the winrt headers.

    #define WIN32_LEAN_AND_MEAN
    #define NOMINMAX
    #include <windows.h>
    
    #include <winrt/Windows.Foundation.h>
    #include <winrt/Windows.Foundation.Collections.h>
    #include <winrt/Windows.Data.Json.h>
    

    C++/WinRT somehow manages to keep the macro defined, but stops it being applied in jsonValue.GetObject();. I wonder how this works?

    Oh it's because the macro gets applied to the winrt header so JsonObject::GetObjectW is now the actual method defined.

    IJsonValue jsonValue = JsonObject{};
    JsonObject jsonObject = jsonValue.GetObjectW();