I have been able to create desktops, get their name, switch, etc, but when I try to modify its name I’m getting an exception, compilable example code:
// desktop.h
#pragma once
#include <Windows.h>
#include <iostream>
#include <objectarray.h>
#include <atlbase.h>
#include <winstring.h>
#pragma comment(lib, "WindowsApp.lib")
const CLSID CLSID_ImmersiveShell = { 0xC2F03A33, 0x21F5, 0x47FA, 0xB4, 0xBB, 0x15, 0x63, 0x62, 0xA2, 0xF2, 0x39 };
const CLSID CLSID_VirtualDesktopManagerInternal = { 0xC5E0CDCA, 0x7B6E, 0x41B2, 0x9F, 0xC4, 0xD9, 0x39, 0x75, 0xCC, 0x46, 0x7B };
MIDL_INTERFACE("372E1D3B-38D3-42E4-A15B-8AB2B178F513")
IApplicationView : public IUnknown
{
public:
};
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;
};
MIDL_INTERFACE("31EBDE3F-6EC3-4CBD-B9FB-0EF6D09B41F4")
IVirtualDesktop2: public IVirtualDesktop
{
public:
virtual HRESULT STDMETHODCALLTYPE GetName(_Out_ HSTRING* pStr) = 0;
};
const IID UUID_IVirtualDesktopManagerInternal_14393 { 0xf31574d6, 0xb682, 0x4cdc, 0xbd, 0x56, 0x18, 0x27, 0x86, 0x0a, 0xbe, 0xc6 };
enum AdjacentDesktopDirection: int
{
AD_LEFT = 3,
AD_RIGHT = 4,
};
class IVirtualDesktopManagerInternal : public IUnknown
{
public:
virtual HRESULT __stdcall GetCount(int* pCount) = 0;
virtual HRESULT __stdcall MoveViewToDesktop(/*IApplicationView*/ IUnknown* pView, IVirtualDesktop* pDesktop) = 0;
virtual HRESULT __stdcall CanViewMoveDesktops(/*IApplicationView*/ IUnknown* pView, BOOL* bCanMove) = 0;
virtual HRESULT __stdcall GetCurrentDesktop(IVirtualDesktop** ppDesktop) = 0;
virtual HRESULT __stdcall GetDesktops(IObjectArray** ppArray) = 0;
virtual HRESULT __stdcall GetAdjacentDesktop(IVirtualDesktop* pDesktopOrigin, AdjacentDesktopDirection nDirection, IVirtualDesktop** ppDesktop) = 0;
virtual HRESULT __stdcall SwitchDesktop(IVirtualDesktop* pDesktop) = 0;
virtual HRESULT __stdcall CreateDesktopW(IVirtualDesktop** ppDesktop) = 0;
virtual HRESULT __stdcall RemoveDesktop(IVirtualDesktop* pRemoveDesktop, IVirtualDesktop* pFallbackDesktop) = 0;
virtual HRESULT __stdcall FindDesktop(GUID* desktopId, IVirtualDesktop** ppDesktop) = 0;
virtual HRESULT __stdcall SetDesktopName(IVirtualDesktop* pDesktop, HSTRING pName) = 0;
};
MIDL_INTERFACE("0F3A72B0-4566-487E-9A33-4ED302F6D6CE")
IVirtualDesktopManagerInternal2 : public IVirtualDesktopManagerInternal
{
public:
virtual HRESULT __stdcall GetDesktopSwitchIncludeExcludeViews(IVirtualDesktop* pDesktop, IObjectArray** ppLeftDesktops, IObjectArray** ppRightDesktops) = 0;
virtual HRESULT STDMETHODCALLTYPE SetDesktopName(_In_ IVirtualDesktop* p0, _In_ HSTRING name) = 0;
};
#include "desktop.h"
int main(int argc, char* argv[])
{
HRESULT hr = ::CoInitialize(NULL);
if (FAILED(hr))
{
std::cerr << "CoInitialize failed: " << std::hex << hr << std::endl;
return FALSE;
}
IServiceProvider* pServiceProvider = nullptr;
hr = ::CoCreateInstance(CLSID_ImmersiveShell, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IServiceProvider), (PVOID*)&pServiceProvider);
if (FAILED(hr))
{
std::cerr << "CoCreateInstance failed: " << std::hex << hr << std::endl;
return FALSE;
}
//CComPtr<IVirtualDesktopManagerInternal> pDesktopManagerInternal = nullptr;
CComPtr<IVirtualDesktopManagerInternal2> pDesktopManagerInternal = nullptr;
hr = pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, UUID_IVirtualDesktopManagerInternal_14393, (void**)&pDesktopManagerInternal);
if (FAILED(hr))
{
std::cerr << "QueryService(DesktopManagerInternal) failed: " << std::hex << hr << std::endl;
pServiceProvider->Release();
pServiceProvider = nullptr;
return FALSE;
}
IVirtualDesktop* desktop = nullptr;
hr = pDesktopManagerInternal->CreateDesktopW(&desktop);
if (FAILED(hr))
return 0;
GUID id;
hr = desktop->GetID(&id);
if (FAILED(hr))
return 0;
HSTRING hString;
LPCWSTR name = L"test123";
WindowsCreateString(name, static_cast<UINT32>(wcslen(name)), &hString);
//hr = static_cast<IVirtualDesktopManagerInternal2*>(pDesktopManagerInternal.p)->SetDesktopName(desktop, hString);
hr = pDesktopManagerInternal->SetDesktopName(desktop, hString); // EXCEPTION
return 0;
}
I’m aware that the ids changes between versions, atm i’m testing on WIN10 22H2 19045
To be more precise I’m getting an exception at this line:
hr = pDesktopManagerInternal->SetDesktopName(desktop, hString);
I have found these two repositories that uses the SetDesktopName:
RMVirtualDesktop
VirtualDesktopAccessor
The first does basically the same thing, the second is on rust, i have no experience with rust but im reading it, he’s calling SetDesktopName with IVirtualDesktop and not the IVirtualDesktop2, i also tried it but its also causing an exception.
Trying to figure out what’s wrong.