Windows Toast notifications - second try

Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 11:30 am

This is a portion of code required to create a standard Windows Toast notification.

To build it you must use FWH\samples\buildh32.bat toast

Here it is failing in hr = GetActivationFactory( hs, &toastStatics ); so you will get a MessageBox()

Any hint to solve it ? thanks

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   Toast()

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
    ComPtr<IToastNotificationManagerStatics> toastStatics;

    HSTRING hs = 0;
   
    HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                      wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "creating string", "error", 0 );
       return;
    }  

    hr = GetActivationFactory( hs, &toastStatics );

    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "GetActivationFactory", "error", 0 );
       return;
    }  
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 11:43 am

Solved :-)

I continue testing it

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( NumToHex( Toast() ) )

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
    ComPtr<IToastNotificationManagerStatics> toastStatics;

    HSTRING hs = 0;
   
    HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                      wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "creating string", "error", 0 );
       return;
    }  

    CoInitialize( NULL );

    hr = GetActivationFactory( hs, &toastStatics );

    hb_retnl( hr );

    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "GetActivationFactory", "error", 0 );
       return;
    }  
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 11:50 am

Some more code.

Many thanks to Antonino Perricone for his help

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   Toast()

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
    ComPtr<IToastNotificationManagerStatics> toastStatics;

    HSTRING hs = 0;
   
    HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                      wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "creating string", "error", 0 );
       return;
    }  

    CoInitialize( NULL );

    hr = GetActivationFactory( hs, &toastStatics );

    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "GetActivationFactory", "error", 0 );
       return;
    }  
   
   if( toastStatics )
   {
      IXmlDocument * toastXml = 0;
      IToastNotifier * notifier = 0;
     
      toastStatics->CreateToastNotifier( &notifier );
     
      if( toastXml ) toastXml->Release();

      if( notifier ) notifier->Release();

      toastStatics->Release();
     
      MessageBox( 0, "done", "ok", 0 );
   }    
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 12:16 pm

Some more code. Still failing...

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   Toast()

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
    ComPtr<IToastNotificationManagerStatics> toastStatics;

    HSTRING hs = 0;
   
    HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                      wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "creating string", "error", 0 );
       return;
    }  

    CoInitialize( NULL );

    hr = GetActivationFactory( hs, &toastStatics );

    if( ! SUCCEEDED( hr ) )
    {
       MessageBox( 0, "GetActivationFactory", "error", 0 );
       return;
    }  
   
   if( toastStatics )
   {
      ComPtr<IXmlDocument> toastXml;
      ComPtr<IToastNotifier> notifier;
      ComPtr<IToastNotification> notification;
     
      hr = toastStatics->CreateToastNotifier( &notifier );

      if( ! SUCCEEDED( hr ) )
      {
        MessageBox( 0, "CreateToastNotifier", "error", 0 );
        return;
      }  

      notifier->Show( notification.Get() );

      if( toastXml ) toastXml->Release();

      if( notifier ) notifier->Release();

      toastStatics->Release();
   }    
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 12:20 pm

Simplified version, still failing

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   Toast()

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
    ComPtr<IToastNotificationManagerStatics> toastStatics;
    HSTRING hs = 0;
    HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                      wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
    CoInitialize( NULL );

    hr = GetActivationFactory( hs, &toastStatics );
   
   if( toastStatics )
   {
      ComPtr<IXmlDocument> toastXml;
      ComPtr<IToastNotifier> notifier;
      ComPtr<IToastNotification> notification;
     
      hr = toastStatics->CreateToastNotifier( &notifier );

      if( ! SUCCEEDED( hr ) )
      {
        MessageBox( 0, "CreateToastNotifier", "error", 0 );
        return;
      }  

      notifier->Show( notification.Get() );

      if( toastXml ) toastXml->Release();

      if( notifier ) notifier->Release();

      toastStatics->Release();
   }    
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby AntoninoP » Wed Apr 13, 2016 1:35 pm

Hello,
Maybe it fails because need to call the RoInitialize(RO_INIT_MULTITHREADED/*1*/) and RoUninitialize.

Can you add something like:
Code: Select all  Expand view

if( ! SUCCEEDED( hr ) )
{
      char buffer[100];
      sprintf(buffer,"Error: %i",hr);
      MessageBox( 0, "CreateToastNotifier", buffer, 0 );
      return;
}  
 

(not tested)?
I know in my sample it is not present, but I have no time to spend on it...
Anyway I will check this thread ;)
Regards,

PS. If you use ComPtr you don't need to call Release, they are SmartPointer.
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: Windows Toast notifications - second try

Postby James Bott » Wed Apr 13, 2016 2:21 pm

Antonio,

I don't know C++, but I did a little web searching and found this article:

Quickstart: Handling toast activations from Win32 apps in Windows 10
https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/

I also saw somewhere that an entry in the manifest file must be made, and that you need to select a toast template.

Hope it helps.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 6:54 pm

Antonino,

I get the same error (0x80070490) if I call to RoInitialize( RO_INIT_MULTITHREADED );

// MessageId: E_PROP_ID_UNSUPPORTED
//
// MessageText:
//
// The specified property ID is not supported for the specified property set.%0
//
#define E_PROP_ID_UNSUPPORTED ((HRESULT)0x80070490L)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Wed Apr 13, 2016 7:24 pm

Better implementation but still getting the same error (0x80070490):

Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( NumToHex( Toast() ) )

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
    ComPtr<IToastNotificationManagerStatics> toastStatics;
    HSTRING hs = 0;
    HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                      wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
    CoInitialize( NULL );
    RoInitialize( RO_INIT_MULTITHREADED );

    hr = GetActivationFactory( hs, &toastStatics );
   
   if( toastStatics )
   {
      ComPtr<IXmlDocument> toastXml;
      ComPtr<IToastNotifier> notifier;
      ComPtr<IToastNotification> notification;
      ComPtr<IToastNotificationFactory> factory;
     
      hr = toastStatics->GetTemplateContent( ToastTemplateType::ToastTemplateType_ToastText03, &toastXml );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "GetTemplateContent", "&toastXml", 0 );
         return;
      }  

      hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotification,
                                wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotification ), &hs );  
                               
      hr = GetActivationFactory( hs, &factory );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "GetActivationFactory", "&factory", 0 );
         return;
      }  

      hr = factory->CreateToastNotification( toastXml.Get(), &notification );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "CreateToastNotification", "&notification", 0 );
         return;
      }  

      hr = toastStatics->CreateToastNotifier( &notifier );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "CreateToastNotifier", "error", 0 );
         hb_retni( hr );
         return;
      }  

      notifier->Show( notification.Get() );
   }    
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Re: Windows Toast notifications - second try

Postby Antonio Linares » Thu Apr 14, 2016 6:51 am

Better implementation but still getting the same error (0x80070490):

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( NumToHex( Toast() ) )

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Windows::Foundation;

HB_FUNC( TOAST )
{
   ComPtr<IToastNotificationManagerStatics> toastStatics;
   HSTRING hs = 0;
   HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                     wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
   CoInitialize( NULL );
   RoInitialize( RO_INIT_MULTITHREADED );

   hr = GetActivationFactory( hs, &toastStatics );

   if( ! SUCCEEDED( hr ) )
   {
      MessageBox( 0, "GetActivationFactory", "&toastStatics", 0 );
      return;
   }  

   if( toastStatics )
   {
      ComPtr<IXmlDocument> toastXml;
      ComPtr<IToastNotifier> notifier;
      ComPtr<IToastNotification> notification;
      ComPtr<IToastNotificationFactory> factory;
     
      hr = toastStatics->GetTemplateContent( ToastTemplateType::ToastTemplateType_ToastText03, &toastXml );
     
      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "GetTemplateContent", "&toastXML", 0 );
         return;
      }  

      ComPtr<IXmlNodeList> toastTextElements, toastImageElements;
      ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot, imageNodeRoot, srcAttribute;

      HSTRING title, msg, imagePath;
      HSTRING textNodeStr, imageNodeStr, srcNodeStr;
      HSTRING_HEADER textHeader, imageHeader, srcHeader;
 
      WindowsCreateStringReference( L"text", 4, &textHeader, &textNodeStr );
      WindowsCreateStringReference( L"image", 5, &imageHeader, &imageNodeStr );
      WindowsCreateStringReference( L"src", 3, &srcHeader, &srcNodeStr );
     
      toastXml->GetElementsByTagName( textNodeStr, &toastTextElements );
      toastXml->GetElementsByTagName( imageNodeStr, &toastImageElements );

      toastTextElements->Item( 0, &titleTextNodeRoot );
      toastTextElements->Item( 1, &msgTextNodeRoot );
      toastImageElements->Item( 0, &imageNodeRoot );

      ComPtr<IXmlNamedNodeMap> attributes;
      // imageNodeRoot->get_Attributes( &attributes );
      // attributes->GetNamedItem( srcNodeStr, &srcAttribute );

      // SetNodeValueString( title, titleTextNodeRoot.Get(), toastXml.Get());
      // SetNodeValueString( msg, msgTextNodeRoot.Get(), toastXml.Get());
      // SetNodeValueString( imagePath, srcAttribute.Get(), toastXml.Get());
 
      hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotification,
                                wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotification ), &hs );  
                               
      hr = GetActivationFactory( hs, &factory );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "GetActivationFactory", "&factory", 0 );
         return;
      }  

      hr = factory->CreateToastNotification( toastXml.Get(), &notification );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "CreateToastNotification", "&notification", 0 );
         return;
      }  

      hr = toastStatics->CreateToastNotifier( &notifier );
     
      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "CreateToastNotifier", "error", 0 );
         hb_retni( hr );
         return;
      }

      hr = notifier->Show( notification.Get() );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "notifier->Show", "error", 0 );
         hb_retni( hr );
         return;
      }

      MessageBox( 0, "done", "ok", 0 );
   }    
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby norberto » Thu Apr 14, 2016 11:33 am

HI, where i found

#include <wrl\client.h>
#include <windows.ui.notifications.h>

thanks
norberto
 
Posts: 566
Joined: Thu Aug 30, 2007 3:40 pm
Location: BR

Re: Windows Toast notifications - second try

Postby Antonio Linares » Thu Apr 14, 2016 1:17 pm

Norberto,

You have to install Visual Studio Community 2015

Select "custom" setup and mark Visual C++. If you select "default" the Visual C++ is not installed
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Thu Apr 14, 2016 6:34 pm

Without a valid shortcut installed in the Start screen or in All Programs, you cannot raise a toast notification from a desktop app


https://msdn.microsoft.com/en-us/library/windows/desktop/hh802762(v=vs.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Windows Toast notifications - second try

Postby Antonio Linares » Thu Apr 14, 2016 8:06 pm

Here we are creating a valid shortcut but the error remains the same (0x80070490):

https://msdn.microsoft.com/en-us/library/windows/desktop/hh802762(v=vs.85).aspx

To test this example using buildh32.bat remember to add these libs:

echo runtimeobject.lib >> msvc.tmp
echo shlwapi.lib >> msvc.tmp

toast.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()

   MsgInfo( NumToHex( Toast() ) )

return nil

#pragma BEGINDUMP

#include <Windows.h>
#include <psapi.h>
#include <Shobjidl.h>
#include <Propvarutil.h>
#include <propkey.h>
#include <wrl\client.h>
#include <windows.ui.notifications.h>
#include <hbapi.h>

using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Windows::Foundation;

static HRESULT InstallShortcut(_In_z_ wchar_t *shortcutPath)
{
    wchar_t exePath[MAX_PATH];
   
    DWORD charWritten = GetModuleFileNameEx(GetCurrentProcess(), nullptr, ( LPSTR ) exePath, ARRAYSIZE(exePath));

    HRESULT hr = charWritten > 0 ? S_OK : E_FAIL;
   
    if (SUCCEEDED(hr))
    {
        ComPtr<IShellLink> shellLink;
        hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));

        if (SUCCEEDED(hr))
        {
            hr = shellLink->SetPath((LPCSTR)exePath);
            if (SUCCEEDED(hr))
            {
                hr = shellLink->SetArguments((LPCSTR)L"");
                if (SUCCEEDED(hr))
                {
                    ComPtr<IPropertyStore> propertyStore;

                    hr = shellLink.As(&propertyStore);
                    if (SUCCEEDED(hr))
                    {
                        PROPVARIANT appIdPropVar;
                        hr = InitPropVariantFromString(L"FiveWin", &appIdPropVar);
                        if (SUCCEEDED(hr))
                        {
                            hr = propertyStore->SetValue(PKEY_AppUserModel_ID, appIdPropVar);
                            if (SUCCEEDED(hr))
                            {
                                hr = propertyStore->Commit();
                                if (SUCCEEDED(hr))
                                {
                                    ComPtr<IPersistFile> persistFile;
                                    hr = shellLink.As(&persistFile);
                                    if (SUCCEEDED(hr))
                                    {
                                        hr = persistFile->Save(shortcutPath, TRUE);
                                    }
                                }
                            }
                            PropVariantClear(&appIdPropVar);
                        }
                    }
                }
            }
        }
    }
    return hr;
}

static HRESULT TryCreateShortcut( void )
{
   wchar_t shortcutPath[MAX_PATH];
   DWORD charWritten = GetEnvironmentVariableW( L"APPDATA", shortcutPath, MAX_PATH );
   HRESULT hr = charWritten > 0 ? S_OK : E_INVALIDARG;

   // MessageBoxW( 0, L"path", shortcutPath, 0 );

   if( ! SUCCEEDED( hr ) )
   {
      MessageBox( 0, "TryCreateShortcut", ( LPSTR ) shortcutPath, 0 );
      hb_retni( hr );
      return hr;
   }  

    if (SUCCEEDED(hr))
    {
        errno_t concatError = wcscat_s(shortcutPath, ARRAYSIZE(shortcutPath), L"\\Microsoft\\Windows\\Start Menu\\Programs\\Desktop Toasts App.lnk");
 
        hr = concatError == 0 ? S_OK : E_INVALIDARG;
        if (SUCCEEDED(hr))
        {
            DWORD attributes = GetFileAttributes( ( LPCSTR ) shortcutPath );
            bool fileExists = attributes < 0xFFFFFFF;

            if (!fileExists)
            {
                hr = InstallShortcut(shortcutPath);  // See step 2.
            }
            else
            {
                hr = S_FALSE;
            }
        }
    }
    return hr;
}

HB_FUNC( TOAST )
{
   ComPtr<IToastNotificationManagerStatics> toastStatics;
   HSTRING hs = 0;
   HRESULT hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
                                     wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager ), &hs );
                         
   CoInitialize( NULL );
   // RoInitialize( RO_INIT_MULTITHREADED );

   hr = TryCreateShortcut();

   if( ! SUCCEEDED( hr ) )
   {
      MessageBox( 0, "TryCreateShortcut", "error", 0 );
      hb_retni( hr );
      return;
   }  

   hr = GetActivationFactory( hs, toastStatics.GetAddressOf() );

   if( ! SUCCEEDED( hr ) )
   {
      MessageBox( 0, "GetActivationFactory", "&toastStatics", 0 );
      return;
   }  

   if( toastStatics )
   {
      ComPtr<IXmlDocument> toastXml;
      ComPtr<IToastNotifier> notifier;
      ComPtr<IToastNotification> notification;
      ComPtr<IToastNotificationFactory> factory;
     
      hr = toastStatics->GetTemplateContent( ToastTemplateType::ToastTemplateType_ToastText01, &toastXml );
     
      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "GetTemplateContent", "&toastXML", 0 );
         return;
      }  

      ComPtr<IXmlNodeList> toastTextElements, toastImageElements;
      ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot, imageNodeRoot, srcAttribute;

      // HSTRING title, msg, imagePath;
      HSTRING textNodeStr, imageNodeStr, srcNodeStr;
      HSTRING_HEADER textHeader, imageHeader, srcHeader;
 
      WindowsCreateStringReference( L"text", 4, &textHeader, &textNodeStr );
      WindowsCreateStringReference( L"image", 5, &imageHeader, &imageNodeStr );
      WindowsCreateStringReference( L"src", 3, &srcHeader, &srcNodeStr );
     
      toastXml->GetElementsByTagName( textNodeStr, &toastTextElements );
      toastXml->GetElementsByTagName( imageNodeStr, &toastImageElements );

      toastTextElements->Item( 0, &titleTextNodeRoot );
      toastTextElements->Item( 1, &msgTextNodeRoot );
      toastImageElements->Item( 0, &imageNodeRoot );

      ComPtr<IXmlNamedNodeMap> attributes;
      // imageNodeRoot->get_Attributes( &attributes );
      // attributes->GetNamedItem( srcNodeStr, &srcAttribute );

      // SetNodeValueString( title, titleTextNodeRoot.Get(), toastXml.Get());
      // SetNodeValueString( msg, msgTextNodeRoot.Get(), toastXml.Get());
      // SetNodeValueString( imagePath, srcAttribute.Get(), toastXml.Get());
 
      hr = WindowsCreateString( RuntimeClass_Windows_UI_Notifications_ToastNotification,
                                wcslen( RuntimeClass_Windows_UI_Notifications_ToastNotification ), &hs );  
                               
      hr = GetActivationFactory( hs, &factory );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "GetActivationFactory", "&factory", 0 );
         return;
      }  

      hr = factory->CreateToastNotification( toastXml.Get(), &notification );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "CreateToastNotification", "&notification", 0 );
         return;
      }  

      hr = toastStatics->CreateToastNotifier( &notifier );
     
      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "CreateToastNotifier", "&notifier", 0 );
         hb_retni( hr );
         return;
      }

      hr = notifier->Show( notification.Get() );

      if( ! SUCCEEDED( hr ) )
      {
         MessageBox( 0, "notifier->Show", "error", 0 );
         hb_retni( hr );
         return;
      }

      MessageBox( 0, "done", "ok", 0 );
   }    
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 102 guests