1
votes

I have found an example how make a Toast Notifications in Windows 10 on c++

https://github.com/WindowsNotifications/desktop-toasts/blob/master/CPP/DesktopToastsSample.cpp

In my opinion this code has C style rather than C++ which doesn't look very good to me.

I've found a wrapper over winrt for с++ https://github.com/Microsoft/cppwinrt

Now I am trying to write similar code like in the example but with winrt.

In example There are lines

ComPtr<IToastNotificationManagerStatics> toastStatics;
HRESULT hr = Windows::Foundation::GetActivationFactory(
   HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(),
&toastStatics);

I started writing code

#include "winrt/Windows.UI.Notifications.h"
#include "winrt/Windows.Foundation.h"
using namespace winrt::Windows::UI::Notifications;
using namespace winrt::Windows::Foundation;
void main() {
    auto igetaf = IGetActivationFactory();
    igetaf.GetActivationFactory(???);
  1. How to convert string to winrt::hstring
  2. How to convert RuntimeClass_Windows_UI_Notifications_ToastNotificationManager hstring

The documentation doesn't even say what strings I can use

docs.microsoft.com/en-us/uwp/api/windows.foundation.igetactivationfactory (Not enough reputation :()

Can you give me an example of working code. ?

1

1 Answers

5
votes

There are currently three main ways of consuming Windows Runtime classes from C++. First is using the ABI, which is what you found. As you have discovered, it's very cumbersome.

Second is C++/CX, which uses the ^ symbol to represent Windows Runtime classes. You can see an example of this style in the UWP samples repo.

Third is C++/WinRT. In C++/WinRT, objects are represented directly as classes. There's an introduction and a migration guide.

In C++/WinRT, you don't operate with class factories at all. (Hence no need to create an hstring for the class name, and no need for IGetActivationFactory.)

Covering all of C++/WinRT is beyond the scope of this site, but here's a sketch:

using winrt::Windows::UI::Notifications::ToastNotification;
using winrt::Windows::UI::Notifications::ToastNotificationManager;
using winrt::Windows::Data::Xml::Dom::XmlDocument;

XmlDocument doc;
doc.LoadXml(L"<toast...>...</toast>");
ToastNotification toast(doc);
ToastNotificationManager::CreateToastNotifier(L"appid").Show(toast);