c++windows-runtimec++-winrt

How to use WinAPi in a C++ WinRT Component?


I'm creating a C++ WinRT runtime component using Visual Studio 2022. I want to use functions like PeekMessage() to handle a basic message loop. However, I get the following errors:

error C3861: 'PeekMessage': identifier not found

I already have #include <Windows.h> at the top of the file, and I've also added #pragma comment(lib, "User32.lib").

Are there any limitations that prevent PeekMessage() from being used in a C++ WinRT component? If so, is there any workaround or recommended approach to use these APIs in this environment?


Solution

  • #undef WINAPI_FAMILY  
    #define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
    

    in the WinRT component, it worked correctly. The original setting was for UWP, but I want to use it with WinUI 3 in C#.