Skip to content

Instantly share code, notes, and snippets.

@ADeltaX
Last active June 16, 2021 08:12
Show Gist options
  • Save ADeltaX/d60a42a2034c9577f1498f7bfcf387c8 to your computer and use it in GitHub Desktop.
Save ADeltaX/d60a42a2034c9577f1498f7bfcf387c8 to your computer and use it in GitHub Desktop.
C++/WinRT snippet for creating or getting a DispatcherQueue (15063+)
#include <winrt/Windows.System.h>
winrt::Windows::System::DispatcherQueue CreateDispatcherQueue()
{
auto coreMsgLib = LoadLibrary(L"CoreMessaging.dll");
typedef HRESULT(WINAPI* CreateDispatcherQueueForCurrentThread)(
_Deref_out_ ABI::Windows::System::IDispatcherQueue** dispatcherQueueController);
auto pCreateDispatcherQueueForCurrentThread =
(CreateDispatcherQueueForCurrentThread)GetProcAddress(coreMsgLib, "CreateDispatcherQueueForCurrentThread");
// if it's NULL means we are on 14393 or older (DispatcherQueue doesn't exists in these versions.)
if (!pCreateDispatcherQueueForCurrentThread)
return nullptr;
winrt::Windows::System::DispatcherQueue controller{ nullptr };
winrt::check_hresult(pCreateDispatcherQueueForCurrentThread(reinterpret_cast<ABI::Windows::System::IDispatcherQueue**>(winrt::put_abi(controller))));
return controller;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment