@@ -17,17 +17,9 @@ public sealed class WindowsWallpaperService : IWindowsWallpaperService
1717 /// <inheritdoc/>
1818 public unsafe void SetDesktopWallpaper ( string szPath )
1919 {
20+ // Instantiate IDesktopWallpaper
2021 using ComPtr < IDesktopWallpaper > pDesktopWallpaper = default ;
21- var CLSID_DesktopWallpaper = typeof ( DesktopWallpaper ) . GUID ;
22- var IID_DesktopWallpaper = typeof ( IDesktopWallpaper ) . GUID ;
23-
24- HRESULT hr = PInvoke . CoCreateInstance (
25- & CLSID_DesktopWallpaper ,
26- null ,
27- CLSCTX . CLSCTX_LOCAL_SERVER ,
28- & IID_DesktopWallpaper ,
29- ( void * * ) pDesktopWallpaper . GetAddressOf ( ) )
30- . ThrowOnFailure ( ) ;
22+ HRESULT hr = pDesktopWallpaper . CoCreateInstance < DesktopWallpaper > ( ) . ThrowOnFailure ( ) ;
3123
3224 // Get total count of all available monitors
3325 hr = pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathCount ( out var dwMonitorCount ) . ThrowOnFailure ( ) ;
@@ -51,37 +43,27 @@ public unsafe void SetDesktopWallpaper(string szPath)
5143 /// <inheritdoc/>
5244 public unsafe void SetDesktopSlideshow ( string [ ] aszPaths )
5345 {
46+ // Instantiate IDesktopWallpaper
5447 using ComPtr < IDesktopWallpaper > pDesktopWallpaper = default ;
55- var CLSID_DesktopWallpaper = typeof ( DesktopWallpaper ) . GUID ;
56- var IID_DesktopWallpaper = typeof ( IDesktopWallpaper ) . GUID ;
57-
58- HRESULT hr = PInvoke . CoCreateInstance (
59- & CLSID_DesktopWallpaper ,
60- null ,
61- CLSCTX . CLSCTX_LOCAL_SERVER ,
62- & IID_DesktopWallpaper ,
63- ( void * * ) pDesktopWallpaper . GetAddressOf ( ) )
64- . ThrowOnFailure ( ) ;
48+ HRESULT hr = pDesktopWallpaper . CoCreateInstance < DesktopWallpaper > ( ) . ThrowOnFailure ( ) ;
6549
6650 uint dwCount = ( uint ) aszPaths . Length ;
6751 ITEMIDLIST * * ppItemIdList = stackalloc ITEMIDLIST * [ aszPaths . Length ] ;
6852
69- // Get array of PIDL from the selected image files
53+ // Get an array of PIDL from the selected image files
7054 for ( uint dwIndex = 0u ; dwIndex < dwCount ; dwIndex ++ )
7155 ppItemIdList [ dwIndex ] = PInvoke . ILCreateFromPath ( aszPaths [ dwIndex ] ) ;
7256
73- // Get a shell array of the array of the PIDL
57+ // Get an IShellItemArray from the array of the PIDL
7458 using ComPtr < IShellItemArray > pShellItemArray = default ;
7559 hr = PInvoke . SHCreateShellItemArrayFromIDLists ( dwCount , ppItemIdList , pShellItemArray . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
7660
77- // Set the slideshow
78- hr = pDesktopWallpaper . Get ( ) ->SetSlideshow ( pShellItemArray . Get ( ) ) . ThrowOnFailure ( ) ;
79-
80- // Free the allocated PIDL
61+ // Release the allocated PIDL
8162 for ( uint dwIndex = 0u ; dwIndex < dwCount ; dwIndex ++ )
82- PInvoke . ILFree ( ppItemIdList [ dwIndex ] ) ;
63+ PInvoke . CoTaskMemFree ( ( void * ) ppItemIdList [ dwIndex ] ) ;
8364
84- // Set wallpaper position to fill the monitor.
65+ // Set the slideshow and its position
66+ hr = pDesktopWallpaper . Get ( ) ->SetSlideshow ( pShellItemArray . Get ( ) ) . ThrowOnFailure ( ) ;
8567 hr = pDesktopWallpaper . Get ( ) ->SetPosition ( DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) . ThrowOnFailure ( ) ;
8668 }
8769
0 commit comments