@@ -18,32 +18,32 @@ public sealed class WindowsWallpaperService : IWindowsWallpaperService
1818 public unsafe void SetDesktopWallpaper ( string szPath )
1919 {
2020 using ComPtr < IDesktopWallpaper > pDesktopWallpaper = default ;
21+ var CLSID_DesktopWallpaper = typeof ( DesktopWallpaper ) . GUID ;
2122 var IID_DesktopWallpaper = typeof ( IDesktopWallpaper ) . GUID ;
22- var CLSID_desktopWallpaper = typeof ( DesktopWallpaper ) . GUID ;
2323
2424 HRESULT hr = PInvoke . CoCreateInstance (
25- & CLSID_desktopWallpaper ,
25+ & CLSID_DesktopWallpaper ,
2626 null ,
2727 CLSCTX . CLSCTX_LOCAL_SERVER ,
2828 & IID_DesktopWallpaper ,
2929 ( void * * ) pDesktopWallpaper . GetAddressOf ( ) )
3030 . ThrowOnFailure ( ) ;
3131
32- // Get total count of all monitors available
33- hr = pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathCount ( out var dwMonitorCount )
34- . ThrowOnFailure ( ) ;
32+ // Get total count of all available monitors
33+ hr = pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathCount ( out var dwMonitorCount ) . ThrowOnFailure ( ) ;
3534
3635 fixed ( char * pszPath = szPath )
3736 {
38- PWSTR pMonitorID = default ;
37+ PWSTR pMonitorId = default ;
3938
40- // Set the selected image file as wallpaper to all monitors available
39+ // Set the selected image file as wallpaper for all available monitors
4140 for ( uint dwIndex = 0u ; dwIndex < dwMonitorCount ; dwIndex ++ )
4241 {
43- hr = pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathAt ( dwIndex , & pMonitorID ) . ThrowOnFailure ( ) ;
44- hr = pDesktopWallpaper . Get ( ) ->SetWallpaper ( pMonitorID , pszPath ) . ThrowOnFailure ( ) ;
42+ // Set the wallpaper
43+ hr = pDesktopWallpaper . Get ( ) ->GetMonitorDevicePathAt ( dwIndex , & pMonitorId ) . ThrowOnFailure ( ) ;
44+ hr = pDesktopWallpaper . Get ( ) ->SetWallpaper ( pMonitorId , pszPath ) . ThrowOnFailure ( ) ;
4545
46- pMonitorID = default ;
46+ pMonitorId = default ;
4747 }
4848 }
4949 }
@@ -52,37 +52,37 @@ public unsafe void SetDesktopWallpaper(string szPath)
5252 public unsafe void SetDesktopSlideshow ( string [ ] aszPaths )
5353 {
5454 using ComPtr < IDesktopWallpaper > pDesktopWallpaper = default ;
55+ var CLSID_DesktopWallpaper = typeof ( DesktopWallpaper ) . GUID ;
5556 var IID_DesktopWallpaper = typeof ( IDesktopWallpaper ) . GUID ;
56- var CLSID_desktopWallpaper = typeof ( DesktopWallpaper ) . GUID ;
5757
5858 HRESULT hr = PInvoke . CoCreateInstance (
59- & CLSID_desktopWallpaper ,
59+ & CLSID_DesktopWallpaper ,
6060 null ,
6161 CLSCTX . CLSCTX_LOCAL_SERVER ,
6262 & IID_DesktopWallpaper ,
6363 ( void * * ) pDesktopWallpaper . GetAddressOf ( ) )
6464 . ThrowOnFailure ( ) ;
6565
66- var dwCount = ( uint ) aszPaths . Length ;
66+ uint dwCount = ( uint ) aszPaths . Length ;
67+ ITEMIDLIST * * ppItemIdList = stackalloc ITEMIDLIST * [ dwCount ] ;
6768
68- fixed ( ITEMIDLIST * * ppItemIdList = new ITEMIDLIST * [ dwCount ] )
69- {
70- for ( uint dwIndex = 0u ; dwIndex < dwCount ; dwIndex ++ )
71- {
72- var id = PInvoke . ILCreateFromPath ( aszPaths [ dwIndex ] ) ;
73- ppItemIdList [ dwIndex ] = id ;
74- }
69+ // Get array of PIDL from the selected image files
70+ for ( uint dwIndex = 0u ; dwIndex < dwCount ; dwIndex ++ )
71+ ppItemIdList [ dwIndex ] = PInvoke . ILCreateFromPath ( aszPaths [ dwIndex ] ) ;
7572
76- // Get a shell array of the selected image files
77- using ComPtr < IShellItemArray > pShellItemArray = default ;
78- PInvoke . SHCreateShellItemArrayFromIDLists ( dwCount , ppItemIdList , pShellItemArray . GetAddressOf ( ) ) ;
73+ // Get a shell array of the array of the PIDL
74+ using ComPtr < IShellItemArray > pShellItemArray = default ;
75+ hr = PInvoke . SHCreateShellItemArrayFromIDLists ( dwCount , ppItemIdList , pShellItemArray . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
7976
80- // Set the slideshow
81- hr = pDesktopWallpaper . Get ( ) ->SetSlideshow ( pShellItemArray . Get ( ) ) ;
82- }
77+ // Set the slideshow
78+ hr = pDesktopWallpaper . Get ( ) ->SetSlideshow ( pShellItemArray . Get ( ) ) . ThrowOnFailure ( ) ;
79+
80+ // Free the allocated PIDL
81+ for ( uint dwIndex = 0u ; dwIndex < dwCount ; dwIndex ++ )
82+ ILFree ( ppItemIdList [ dwIndex ] ) ;
8383
8484 // Set wallpaper position to fill the monitor.
85- hr = pDesktopWallpaper . Get ( ) ->SetPosition ( DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) ;
85+ hr = pDesktopWallpaper . Get ( ) ->SetPosition ( DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) . ThrowOnFailure ( ) ;
8686 }
8787
8888 /// <inheritdoc/>
0 commit comments