Virtual Desktop API does not work any more for notifications

%3CLINGO-SUB%20id%3D%22lingo-sub-2614296%22%20slang%3D%22en-US%22%3EVirtual%20Desktop%20API%20does%20not%20work%20any%20more%20for%20notifications%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2614296%22%20slang%3D%22en-US%22%3E%3CP%3EI%20can%20use%20the%20follow%20code%20to%20receive%20notifications%20about%20virtual%20desktop%20changes%20when%20I%20switch%20between%20different%20virtual%20desktops%20on%20Windows%2010%2C%20but%20it%20does%20not%20work%20on%20Windows%2011.%20On%20Windows%2010%20it%20will%20print%20out%3A%3C%2FP%3E%3CP%3EViewVirtualDesktopChanged%3CBR%20%2F%3ECurrentVirtualDesktopChanged%3CBR%20%2F%3ECurrentVirtualDesktopChanged%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20on%20Windows%2011%2C%20it%20will%20print%20out%3CBR%20%2F%3EUnable%20to%20create%20notificator%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E%23include%20%22stdafx.h%22%0A%23include%20%3CWINDOWS.H%3E%0A%0A%23include%20%3COBJBASE.H%3E%0A%23include%20%3COBJECTARRAY.H%3E%0A%23include%20%3CIOSTREAM%3E%0A%0Aconst%20CLSID%20CLSID_ImmersiveShell%20%3D%20%7B%200xC2F03A33%2C%200x21F5%2C%200x47FA%2C%200xB4%2C%200xBB%2C%200x15%2C%200x63%2C%200x62%2C%200xA2%2C%200xF2%2C%200x39%20%7D%3B%0Aconst%20CLSID%20CLSID_VirtualDesktopAPI_Unknown%20%3D%20%7B%200xC5E0CDCA%2C%200x7B6E%2C%200x41B2%2C%200x9F%2C%200xC4%2C%200xD9%2C%200x39%2C%200x75%2C%200xCC%2C%200x46%2C%200x7B%20%7D%3B%0Aconst%20CLSID%20CLSID_IVirtualNotificationService%20%3D%20%7B%200xA501FDEC%2C%200x4A09%2C%200x464C%2C%200xAE%2C%200x4E%2C%200x1B%2C%200x9C%2C%200x21%2C%200xB8%2C%200x49%2C%200x18%20%7D%3B%0A%0Aconst%20IID%20IID_IVirtualDesktopManagerInternal%20%3D%20%7B%200xEF9F1A6C%2C%200xD3CC%2C%200x4358%2C%200xB7%2C%200x12%2C%200xF8%2C%200x4B%2C%200x63%2C%200x5B%2C%200xEB%2C%200xE7%20%7D%3B%0Aconst%20IID%20IID_IVirtualDesktopNotification%20%3D%20%7B%200xC179334C%2C%200x4295%2C%200x40D3%2C%200xBE%2C%200xA1%2C%200xC6%2C%200x54%2C%200xD9%2C%200x65%2C%200x60%2C%200x5A%20%7D%3B%0A%0Astruct%20IApplicationView%20%3A%20public%20IUnknown%0A%7B%0Apublic%3A%0A%0A%7D%3B%0A%0AEXTERN_C%20const%20IID%20IID_IVirtualDesktop%3B%0A%0AMIDL_INTERFACE(%22FF72FFDD-BE7E-43FC-9C03-AD81681E88E4%22)%0AIVirtualDesktop%20%3A%20public%20IUnknown%0A%7B%0Apublic%3A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20IsViewVisible(%0A%20%20IApplicationView%20*%20pView%2C%0A%20%20int*%20pfVisible)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20GetID(%0A%20%20GUID*%20pGuid)%20%3D%200%3B%0A%7D%3B%0A%0Aenum%20AdjacentDesktop%0A%7B%0A%20LeftDirection%20%3D%203%2C%0A%20RightDirection%20%3D%204%0A%7D%3B%0A%0AEXTERN_C%20const%20IID%20IID_IVirtualDesktopManagerInternal%3B%0A%0A%2F%2FMIDL_INTERFACE(%22EF9F1A6C-D3CC-4358-B712-F84B635BEBE7%22)%0AMIDL_INTERFACE(%22AF8DA486-95BB-4460-B3B7-6E7A6B2962B5%22)%0AIVirtualDesktopManagerInternal%20%3A%20public%20IUnknown%0A%7B%0Apublic%3A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20GetCount(%0A%20%20UINT%20*%20pCount)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20MoveViewToDesktop(%0A%20%20IApplicationView*%20pView%2C%0A%20%20IVirtualDesktop*%20pDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20CanViewMoveDesktops(%0A%20%20IApplicationView*%20pView%2C%0A%20%20int*%20pfCanViewMoveDesktops)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20GetCurrentDesktop(%0A%20%20IVirtualDesktop**%20desktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20GetDesktops(%0A%20%20IObjectArray**%20ppDesktops)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20GetAdjacentDesktop(%0A%20%20IVirtualDesktop*%20pDesktopReference%2C%0A%20%20AdjacentDesktop%20uDirection%2C%0A%20%20IVirtualDesktop**%20ppAdjacentDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20SwitchDesktop(%0A%20%20IVirtualDesktop*%20pDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20CreateDesktopW(%0A%20%20IVirtualDesktop**%20ppNewDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20RemoveDesktop(%0A%20%20IVirtualDesktop*%20pRemove%2C%0A%20%20IVirtualDesktop*%20pFallbackDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20FindDesktop(%0A%20%20GUID*%20desktopId%2C%0A%20%20IVirtualDesktop**%20ppDesktop)%20%3D%200%3B%0A%7D%3B%0A%0A%23if%20(_MSC_VER%20%26lt%3B%201900)%0A%0AEXTERN_C%20const%20IID%20IID_IVirtualDesktopManager%3B%0A%0AMIDL_INTERFACE(%22a5cd92ff-29be-454c-8d04-d82879fb3f1b%22)%0AIVirtualDesktopManager%20%3A%20public%20IUnknown%0A%7B%0Apublic%3A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20IsWindowOnCurrentVirtualDesktop(%0A%20%20%2F*%20%5Bin%5D%20*%2F%20__RPC__in%20HWND%20topLevelWindow%2C%0A%20%20%2F*%20%5Bout%5D%20*%2F%20__RPC__out%20BOOL%20*%20onCurrentDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20GetWindowDesktopId(%0A%20%20%2F*%20%5Bin%5D%20*%2F%20__RPC__in%20HWND%20topLevelWindow%2C%0A%20%20%2F*%20%5Bout%5D%20*%2F%20__RPC__out%20GUID*%20desktopId)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20MoveWindowToDesktop(%0A%20%20%2F*%20%5Bin%5D%20*%2F%20__RPC__in%20HWND%20topLevelWindow%2C%0A%20%20%2F*%20%5Bin%5D%20*%2F%20__RPC__in%20REFGUID%20desktopId)%20%3D%200%3B%0A%7D%3B%0A%23else%0A%23include%20%3CSHOBJIDL_CORE.H%3E%0A%23endif%20%2F%2Fif%20(_MSC_VER%20%26lt%3B%201900)%0A%0AEXTERN_C%20const%20IID%20IID_IVirtualDesktopNotification%3B%0A%0AMIDL_INTERFACE(%22C179334C-4295-40D3-BEA1-C654D965605A%22)%0AIVirtualDesktopNotification%20%3A%20public%20IUnknown%0A%7B%0Apublic%3A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopCreated(%0A%20%20IVirtualDesktop%20*%20pDesktop)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyBegin(%0A%20%20IVirtualDesktop*%20pDesktopDestroyed%2C%0A%20%20IVirtualDesktop*%20pDesktopFallback)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyFailed(%0A%20%20IVirtualDesktop*%20pDesktopDestroyed%2C%0A%20%20IVirtualDesktop*%20pDesktopFallback)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyed(%0A%20%20IVirtualDesktop*%20pDesktopDestroyed%2C%0A%20%20IVirtualDesktop*%20pDesktopFallback)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20ViewVirtualDesktopChanged(%0A%20%20IApplicationView*%20pView)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20CurrentVirtualDesktopChanged(%0A%20%20IVirtualDesktop*%20pDesktopOld%2C%0A%20%20IVirtualDesktop*%20pDesktopNew)%20%3D%200%3B%0A%0A%7D%3B%0A%0AEXTERN_C%20const%20IID%20IID_IVirtualDesktopNotificationService%3B%0A%0AMIDL_INTERFACE(%220CD45E71-D927-4F15-8B0A-8FEF525337BF%22)%0AIVirtualDesktopNotificationService%20%3A%20public%20IUnknown%0A%7B%0Apublic%3A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20Register(%0A%20%20IVirtualDesktopNotification%20*%20pNotification%2C%0A%20%20DWORD%20*%20pdwCookie)%20%3D%200%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20Unregister(%0A%20%20DWORD%20dwCookie)%20%3D%200%3B%0A%7D%3B%0A%0Aclass%20CSimpleVirtualDesktopNotification%20%3A%20public%20IVirtualDesktopNotification%0A%7B%0Apublic%3A%0A%20CSimpleVirtualDesktopNotification()%3B%0A%20~CSimpleVirtualDesktopNotification()%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20QueryInterface(%0A%20%20%2F*%20%5Bin%5D%20*%2F%20REFIID%20riid%2C%0A%20%20%2F*%20%5Biid_is%5D%5Bout%5D%20*%2F%20_COM_Outptr_%20void%20__RPC_FAR*%20__RPC_FAR*%20ppvObject)%3B%0A%0A%20virtual%20ULONG%20STDMETHODCALLTYPE%20AddRef(void)%3B%0A%0A%20virtual%20ULONG%20STDMETHODCALLTYPE%20Release(void)%3B%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopCreated(%0A%20%20IVirtualDesktop*%20pDesktop)%20%7B%0A%20%20return%200%3B%0A%20%7D%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyBegin(%0A%20%20IVirtualDesktop*%20pDesktopDestroyed%2C%0A%20%20IVirtualDesktop*%20pDesktopFallback)%20%7B%0A%20%20return%200%3B%0A%20%7D%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyFailed(%0A%20%20IVirtualDesktop*%20pDesktopDestroyed%2C%0A%20%20IVirtualDesktop*%20pDesktopFallback)%20%7B%0A%20%20return%200%3B%0A%20%7D%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyed(%0A%20%20IVirtualDesktop*%20pDesktopDestroyed%2C%0A%20%20IVirtualDesktop*%20pDesktopFallback)%20%7B%0A%20%20return%200%3B%0A%20%7D%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20ViewVirtualDesktopChanged(%0A%20%20IApplicationView*%20pView)%20%7B%0A%20%20return%200%3B%0A%20%7D%0A%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20CurrentVirtualDesktopChanged(%0A%20%20IVirtualDesktop*%20pDesktopOld%2C%0A%20%20IVirtualDesktop*%20pDesktopNew)%20%7B%0A%20%20return%200%3B%0A%20%7D%0A%0Aprivate%3A%0A%20ULONG%20m_refCount%3B%0A%7D%3B%0A%0Aclass%20Notifications%20%3A%20public%20IVirtualDesktopNotification%20%7B%0Aprivate%3A%0A%20ULONG%20_referenceCount%3B%0Apublic%3A%0A%20%2F%2F%20Inherited%20via%20IVirtualDesktopNotification%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20QueryInterface(REFIID%20riid%2C%20void**%20ppvObject)%20override%0A%20%7B%0A%20%20%2F%2F%20Always%20set%20out%20parameter%20to%20NULL%2C%20validating%20it%20first.%0A%20%20if%20(!ppvObject)%0A%20%20%20return%20E_INVALIDARG%3B%0A%20%20*ppvObject%20%3D%20NULL%3B%0A%0A%20%20if%20(riid%20%3D%3D%20IID_IUnknown%20%7C%7C%20riid%20%3D%3D%20IID_IVirtualDesktopNotification)%0A%20%20%7B%0A%20%20%20%2F%2F%20Increment%20the%20reference%20count%20and%20return%20the%20pointer.%0A%20%20%20*ppvObject%20%3D%20(LPVOID)this%3B%0A%20%20%20AddRef()%3B%0A%20%20%20return%20S_OK%3B%0A%20%20%7D%0A%20%20return%20E_NOINTERFACE%3B%0A%20%7D%0A%20virtual%20ULONG%20STDMETHODCALLTYPE%20AddRef()%20override%0A%20%7B%0A%20%20return%20InterlockedIncrement(%26amp%3B_referenceCount)%3B%0A%20%7D%0A%0A%20virtual%20ULONG%20STDMETHODCALLTYPE%20Release()%20override%0A%20%7B%0A%20%20ULONG%20result%20%3D%20InterlockedDecrement(%26amp%3B_referenceCount)%3B%0A%20%20if%20(result%20%3D%3D%200)%0A%20%20%7B%0A%20%20%20delete%20this%3B%0A%20%20%7D%0A%20%20return%200%3B%0A%20%7D%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopCreated(IVirtualDesktop*%20pDesktop)%20override%0A%20%7B%0A%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22VirtualDesktopCreated%5Cr%5Cn%22%3B%0A%20%20return%20S_OK%3B%0A%20%7D%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyBegin(IVirtualDesktop*%20pDesktopDestroyed%2C%20IVirtualDesktop*%20pDesktopFallback)%20override%0A%20%7B%0A%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22VirtualDesktopDestroyBegin%5Cr%5Cn%22%3B%0A%20%20return%20S_OK%3B%0A%20%7D%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyFailed(IVirtualDesktop*%20pDesktopDestroyed%2C%20IVirtualDesktop*%20pDesktopFallback)%20override%0A%20%7B%0A%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22VirtualDesktopDestroyFailed%5Cr%5Cn%22%3B%0A%20%20return%20S_OK%3B%0A%20%7D%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20VirtualDesktopDestroyed(IVirtualDesktop*%20pDesktopDestroyed%2C%20IVirtualDesktop*%20pDesktopFallback)%20override%0A%20%7B%0A%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22VirtualDesktopDestroyed%5Cr%5Cn%22%3B%0A%20%20return%20S_OK%3B%0A%20%7D%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20ViewVirtualDesktopChanged(IApplicationView*%20pView)%20override%0A%20%7B%0A%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22ViewVirtualDesktopChanged%5Cr%5Cn%22%3B%0A%20%20return%20S_OK%3B%0A%20%7D%0A%20virtual%20HRESULT%20STDMETHODCALLTYPE%20CurrentVirtualDesktopChanged(%0A%20%20IVirtualDesktop*%20pDesktopOld%2C%0A%20%20IVirtualDesktop*%20pDesktopNew)%20override%0A%20%7B%0A%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22CurrentVirtualDesktopChanged%5Cr%5Cn%22%3B%0A%20%20return%20S_OK%3B%0A%20%7D%0A%7D%3B%0A%0ANotifications*%20nf%3B%0A%0Aint%20main()%0A%7B%0A%20%3A%3ACoInitialize(NULL)%3B%0A%0A%20nf%20%3D%20new%20Notifications()%3B%0A%0A%20IServiceProvider*%20pServiceProvider%20%3D%20nullptr%3B%0A%20HRESULT%20hr%20%3D%20%3A%3ACoCreateInstance(%0A%20%20CLSID_ImmersiveShell%2C%20NULL%2C%20CLSCTX_LOCAL_SERVER%2C%0A%20%20__uuidof(IServiceProvider)%2C%20(PVOID*)%26amp%3BpServiceProvider)%3B%0A%0A%20IVirtualDesktopNotificationService*%20pDesktopNotificationService%20%3D%20nullptr%3B%0A%0A%20HRESULT%20hrNotificationService%20%3D%20pServiceProvider-%26gt%3BQueryService(%0A%20%20CLSID_IVirtualNotificationService%2C%0A%20%20__uuidof(IVirtualDesktopNotificationService)%2C%0A%20%20(PVOID*)%26amp%3BpDesktopNotificationService)%3B%0A%0A%20DWORD%20pdwCookie%20%3D%200%3B%0A%0A%20if%20(SUCCEEDED(hrNotificationService))%20%7B%0A%20%20HRESULT%20registered%20%3D%20pDesktopNotificationService-%26gt%3BRegister(nf%2C%20%26amp%3BpdwCookie)%3B%0A%20%20if%20(SUCCEEDED(registered))%20%7B%0A%20%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22Notificator%20created%3A%20%22%20%26lt%3B%26lt%3B%20pdwCookie%20%26lt%3B%26lt%3B%20std%3A%3Aendl%3B%0A%20%20%7D%0A%20%20else%20%7B%0A%20%20%20std%3A%3Awcout%20%26lt%3B%26lt%3B%20L%22Unable%20to%20create%20notificator%22%3B%0A%20%20%7D%0A%20%7D%0A%0A%20MSG%20msg%20%3D%20%7B0%7D%3B%0A%20while%20(%3A%3AGetMessage(%26amp%3Bmsg%2C%20NULL%2C%200%2C%200))%0A%20%7B%0A%20%20%3A%3ATranslateMessage(%26amp%3Bmsg)%3B%0A%20%20%3A%3ADispatchMessage(%26amp%3Bmsg)%3B%0A%20%7D%0A%20%0A%20if%20(pdwCookie%20%26gt%3B%200)%20%7B%0A%20%20pDesktopNotificationService-%26gt%3BUnregister(pdwCookie)%3B%0A%20%7D%0A%0A%20delete%20nf%3B%0A%0A%20return%200%3B%0A%7D%3C%2FSHOBJIDL_CORE.H%3E%3C%2FIOSTREAM%3E%3C%2FOBJECTARRAY.H%3E%3C%2FOBJBASE.H%3E%3C%2FWINDOWS.H%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2618022%22%20slang%3D%22en-US%22%3ERe%3A%20Virtual%20Desktop%20API%20does%20not%20work%20any%20more%20for%20notifications%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2618022%22%20slang%3D%22en-US%22%3EThis%20issue%20was%20reported%20on%20Windows%2011%20build%2022000.100.%20And%20it%20can%20also%20be%20reproduced%20100%25%20on%20build%2022000.120.%3C%2FLINGO-BODY%3E
Occasional Contributor

I can use the follow code to receive notifications about virtual desktop changes when I switch between different virtual desktops on Windows 10, but it does not work on Windows 11. On Windows 10 it will print out:

ViewVirtualDesktopChanged
CurrentVirtualDesktopChanged
CurrentVirtualDesktopChanged

 

But on Windows 11, it will print out
Unable to create notificator

 

#include "stdafx.h"
#include <windows.h>

#include <objbase.h>
#include <ObjectArray.h>
#include <iostream>

const CLSID CLSID_ImmersiveShell = { 0xC2F03A33, 0x21F5, 0x47FA, 0xB4, 0xBB, 0x15, 0x63, 0x62, 0xA2, 0xF2, 0x39 };
const CLSID CLSID_VirtualDesktopAPI_Unknown = { 0xC5E0CDCA, 0x7B6E, 0x41B2, 0x9F, 0xC4, 0xD9, 0x39, 0x75, 0xCC, 0x46, 0x7B };
const CLSID CLSID_IVirtualNotificationService = { 0xA501FDEC, 0x4A09, 0x464C, 0xAE, 0x4E, 0x1B, 0x9C, 0x21, 0xB8, 0x49, 0x18 };

const IID IID_IVirtualDesktopManagerInternal = { 0xEF9F1A6C, 0xD3CC, 0x4358, 0xB7, 0x12, 0xF8, 0x4B, 0x63, 0x5B, 0xEB, 0xE7 };
const IID IID_IVirtualDesktopNotification = { 0xC179334C, 0x4295, 0x40D3, 0xBE, 0xA1, 0xC6, 0x54, 0xD9, 0x65, 0x60, 0x5A };

struct IApplicationView : public IUnknown
{
public:

};

EXTERN_C const IID IID_IVirtualDesktop;

MIDL_INTERFACE("FF72FFDD-BE7E-43FC-9C03-AD81681E88E4")
IVirtualDesktop : public IUnknown
{
public:
	virtual HRESULT STDMETHODCALLTYPE IsViewVisible(
		IApplicationView * pView,
		int* pfVisible) = 0;

	virtual HRESULT STDMETHODCALLTYPE GetID(
		GUID* pGuid) = 0;
};

enum AdjacentDesktop
{
	LeftDirection = 3,
	RightDirection = 4
};

EXTERN_C const IID IID_IVirtualDesktopManagerInternal;

//MIDL_INTERFACE("EF9F1A6C-D3CC-4358-B712-F84B635BEBE7")
MIDL_INTERFACE("AF8DA486-95BB-4460-B3B7-6E7A6B2962B5")
IVirtualDesktopManagerInternal : public IUnknown
{
public:
	virtual HRESULT STDMETHODCALLTYPE GetCount(
		UINT * pCount) = 0;

	virtual HRESULT STDMETHODCALLTYPE MoveViewToDesktop(
		IApplicationView* pView,
		IVirtualDesktop* pDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE CanViewMoveDesktops(
		IApplicationView* pView,
		int* pfCanViewMoveDesktops) = 0;

	virtual HRESULT STDMETHODCALLTYPE GetCurrentDesktop(
		IVirtualDesktop** desktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE GetDesktops(
		IObjectArray** ppDesktops) = 0;

	virtual HRESULT STDMETHODCALLTYPE GetAdjacentDesktop(
		IVirtualDesktop* pDesktopReference,
		AdjacentDesktop uDirection,
		IVirtualDesktop** ppAdjacentDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE SwitchDesktop(
		IVirtualDesktop* pDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE CreateDesktopW(
		IVirtualDesktop** ppNewDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE RemoveDesktop(
		IVirtualDesktop* pRemove,
		IVirtualDesktop* pFallbackDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE FindDesktop(
		GUID* desktopId,
		IVirtualDesktop** ppDesktop) = 0;
};

#if (_MSC_VER < 1900)

EXTERN_C const IID IID_IVirtualDesktopManager;

MIDL_INTERFACE("a5cd92ff-29be-454c-8d04-d82879fb3f1b")
IVirtualDesktopManager : public IUnknown
{
public:
	virtual HRESULT STDMETHODCALLTYPE IsWindowOnCurrentVirtualDesktop(
		/* [in] */ __RPC__in HWND topLevelWindow,
		/* [out] */ __RPC__out BOOL * onCurrentDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE GetWindowDesktopId(
		/* [in] */ __RPC__in HWND topLevelWindow,
		/* [out] */ __RPC__out GUID* desktopId) = 0;

	virtual HRESULT STDMETHODCALLTYPE MoveWindowToDesktop(
		/* [in] */ __RPC__in HWND topLevelWindow,
		/* [in] */ __RPC__in REFGUID desktopId) = 0;
};
#else
#include <ShObjIdl_core.h>
#endif //if (_MSC_VER < 1900)

EXTERN_C const IID IID_IVirtualDesktopNotification;

MIDL_INTERFACE("C179334C-4295-40D3-BEA1-C654D965605A")
IVirtualDesktopNotification : public IUnknown
{
public:
	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopCreated(
		IVirtualDesktop * pDesktop) = 0;

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyBegin(
		IVirtualDesktop* pDesktopDestroyed,
		IVirtualDesktop* pDesktopFallback) = 0;

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyFailed(
		IVirtualDesktop* pDesktopDestroyed,
		IVirtualDesktop* pDesktopFallback) = 0;

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyed(
		IVirtualDesktop* pDesktopDestroyed,
		IVirtualDesktop* pDesktopFallback) = 0;

	virtual HRESULT STDMETHODCALLTYPE ViewVirtualDesktopChanged(
		IApplicationView* pView) = 0;

	virtual HRESULT STDMETHODCALLTYPE CurrentVirtualDesktopChanged(
		IVirtualDesktop* pDesktopOld,
		IVirtualDesktop* pDesktopNew) = 0;

};

EXTERN_C const IID IID_IVirtualDesktopNotificationService;

MIDL_INTERFACE("0CD45E71-D927-4F15-8B0A-8FEF525337BF")
IVirtualDesktopNotificationService : public IUnknown
{
public:
	virtual HRESULT STDMETHODCALLTYPE Register(
		IVirtualDesktopNotification * pNotification,
		DWORD * pdwCookie) = 0;

	virtual HRESULT STDMETHODCALLTYPE Unregister(
		DWORD dwCookie) = 0;
};

class CSimpleVirtualDesktopNotification : public IVirtualDesktopNotification
{
public:
	CSimpleVirtualDesktopNotification();
	~CSimpleVirtualDesktopNotification();

	virtual HRESULT STDMETHODCALLTYPE QueryInterface(
		/* [in] */ REFIID riid,
		/* [iid_is][out] */ _COM_Outptr_ void __RPC_FAR* __RPC_FAR* ppvObject);

	virtual ULONG STDMETHODCALLTYPE AddRef(void);

	virtual ULONG STDMETHODCALLTYPE Release(void);

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopCreated(
		IVirtualDesktop* pDesktop) {
		return 0;
	}

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyBegin(
		IVirtualDesktop* pDesktopDestroyed,
		IVirtualDesktop* pDesktopFallback) {
		return 0;
	}

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyFailed(
		IVirtualDesktop* pDesktopDestroyed,
		IVirtualDesktop* pDesktopFallback) {
		return 0;
	}

	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyed(
		IVirtualDesktop* pDesktopDestroyed,
		IVirtualDesktop* pDesktopFallback) {
		return 0;
	}

	virtual HRESULT STDMETHODCALLTYPE ViewVirtualDesktopChanged(
		IApplicationView* pView) {
		return 0;
	}

	virtual HRESULT STDMETHODCALLTYPE CurrentVirtualDesktopChanged(
		IVirtualDesktop* pDesktopOld,
		IVirtualDesktop* pDesktopNew) {
		return 0;
	}

private:
	ULONG m_refCount;
};

class Notifications : public IVirtualDesktopNotification {
private:
	ULONG _referenceCount;
public:
	// Inherited via IVirtualDesktopNotification
	virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) override
	{
		// Always set out parameter to NULL, validating it first.
		if (!ppvObject)
			return E_INVALIDARG;
		*ppvObject = NULL;

		if (riid == IID_IUnknown || riid == IID_IVirtualDesktopNotification)
		{
			// Increment the reference count and return the pointer.
			*ppvObject = (LPVOID)this;
			AddRef();
			return S_OK;
		}
		return E_NOINTERFACE;
	}
	virtual ULONG STDMETHODCALLTYPE AddRef() override
	{
		return InterlockedIncrement(&_referenceCount);
	}

	virtual ULONG STDMETHODCALLTYPE Release() override
	{
		ULONG result = InterlockedDecrement(&_referenceCount);
		if (result == 0)
		{
			delete this;
		}
		return 0;
	}
	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopCreated(IVirtualDesktop* pDesktop) override
	{
		std::wcout << L"VirtualDesktopCreated\r\n";
		return S_OK;
	}
	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyBegin(IVirtualDesktop* pDesktopDestroyed, IVirtualDesktop* pDesktopFallback) override
	{
		std::wcout << L"VirtualDesktopDestroyBegin\r\n";
		return S_OK;
	}
	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyFailed(IVirtualDesktop* pDesktopDestroyed, IVirtualDesktop* pDesktopFallback) override
	{
		std::wcout << L"VirtualDesktopDestroyFailed\r\n";
		return S_OK;
	}
	virtual HRESULT STDMETHODCALLTYPE VirtualDesktopDestroyed(IVirtualDesktop* pDesktopDestroyed, IVirtualDesktop* pDesktopFallback) override
	{
		std::wcout << L"VirtualDesktopDestroyed\r\n";
		return S_OK;
	}
	virtual HRESULT STDMETHODCALLTYPE ViewVirtualDesktopChanged(IApplicationView* pView) override
	{
		std::wcout << L"ViewVirtualDesktopChanged\r\n";
		return S_OK;
	}
	virtual HRESULT STDMETHODCALLTYPE CurrentVirtualDesktopChanged(
		IVirtualDesktop* pDesktopOld,
		IVirtualDesktop* pDesktopNew) override
	{
		std::wcout << L"CurrentVirtualDesktopChanged\r\n";
		return S_OK;
	}
};

Notifications* nf;

int main()
{
	::CoInitialize(NULL);

	nf = new Notifications();

	IServiceProvider* pServiceProvider = nullptr;
	HRESULT hr = ::CoCreateInstance(
		CLSID_ImmersiveShell, NULL, CLSCTX_LOCAL_SERVER,
		__uuidof(IServiceProvider), (PVOID*)&pServiceProvider);

	IVirtualDesktopNotificationService* pDesktopNotificationService = nullptr;

	HRESULT hrNotificationService = pServiceProvider->QueryService(
		CLSID_IVirtualNotificationService,
		__uuidof(IVirtualDesktopNotificationService),
		(PVOID*)&pDesktopNotificationService);

	DWORD pdwCookie = 0;

	if (SUCCEEDED(hrNotificationService)) {
		HRESULT registered = pDesktopNotificationService->Register(nf, &pdwCookie);
		if (SUCCEEDED(registered)) {
			std::wcout << L"Notificator created: " << pdwCookie << std::endl;
		}
		else {
			std::wcout << L"Unable to create notificator";
		}
	}

	MSG msg = {0};
	while (::GetMessage(&msg, NULL, 0, 0))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
	
	if (pdwCookie > 0) {
		pDesktopNotificationService->Unregister(pdwCookie);
	}

	delete nf;

	return 0;
}

  

1 Reply
This issue was reported on Windows 11 build 22000.100. And it can also be reproduced 100% on build 22000.120.