winrt::create_instance<IDesktopWallpaper> error

Neo 421 Reputation points
2023-07-15T02:21:47.8466667+00:00

I am completely new to C++/WinRT. I read some articles on winrt::capture and tried the following code:

#include "pch.h"
#include <Shobjidl.h>

// EDIT: use winrt::guid_of<IDesktopWallpaper>.
// Previously I used CLSID_DesktopWallpaper which did not work.
auto d = winrt::create_instance<IDesktopWallpaper>(winrt::guid_of<IDesktopWallpaper>(), CLSCTX_ALL);

Now there is another weird compile error on undefined identifier:

1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um\shobjidl_core.h(33547,11): error C2061: syntax error: identifier 'CMINVOKECOMMANDINFO'
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um\shobjidl_core.h(33552,61): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

// Shobjidl_core.h:
/* [local] */ HRESULT STDMETHODCALLTYPE IContextMenu_InvokeCommand_Proxy( 
IContextMenu * This,
/* [annotation][in] */ 
_In_  CMINVOKECOMMANDINFO *pici);

But Shobjidl_core.h does have the following declaration:

typedef struct _CMINVOKECOMMANDINFO
{
DWORD cbSize;
DWORD fMask;
HWND hwnd;
LPCSTR lpVerb;
LPCSTR lpParameters;
LPCSTR lpDirectory;
int nShow;
DWORD dwHotKey;
HANDLE hIcon;
} 	CMINVOKECOMMANDINFO;

OT: When I edit my post, "Edit development language" on code blocks never works for me from the beginning. For example, if I select "C++/WinRT", it will strangely change language to XML!

E.O.F

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Junjie Zhu - MSFT 18,736 Reputation points Microsoft Vendor
    2023-07-17T06:00:29.5566667+00:00

    Hi @Neo , Welcome to Microsoft Q&A!

    IDesktopWallpaper Interface only supports desktop apps. If you are using the UWP project, you cannot use it in the UWP project.

    If you want to set lock screen image or wallpaper image in UWP project, it is recommended to use UserProfilePersonalizationSettings.TrySetLockScreenImageAsync and UserProfilePersonalizationSettings.TrySetWallpaperImageAsync.

    Thank you.

    Junjie.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. peter li 6 Reputation points
    2024-06-06T07:44:22.09+00:00

    I just wanted to add that, you use the winrt::create_instance wrong. It's true that UWP does NOT support this interface. But in a winui3 project, this should be

     auto wallpaper = winrt::create_instance<IDesktopWallpaper>(CLSID_DesktopWallpaper, CLSCTX_ALL);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.