Skip to content

Commit 05993b3

Browse files
committed
Improve
1 parent fc97c64 commit 05993b3

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

src/Files.App.CsWin32/Windows.Win32.ComPtr.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Runtime.CompilerServices;
66
using Windows.Win32;
7+
using Windows.Win32.Foundation;
78
using Windows.Win32.System.Com;
89

910
namespace Windows.Win32
@@ -39,11 +40,19 @@ public ComPtr(T* ptr)
3940
}
4041

4142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
42-
public readonly ComPtr<U> As<U>(Guid riid) where U : unmanaged
43+
public readonly ComPtr<U> As<U>() where U : unmanaged
4344
{
44-
ComPtr<U> pNewPtr = default;
45-
((IUnknown*)_ptr)->QueryInterface(&riid, (void**)pNewPtr.GetAddressOf());
46-
return pNewPtr;
45+
ComPtr<U> ptr = default;
46+
Guid iid = typeof(U).GUID;
47+
((IUnknown*)_ptr)->QueryInterface(&iid, (void**)ptr.GetAddressOf());
48+
return ptr;
49+
}
50+
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public readonly HRESULT CoCreateInstance<U>(CLSCTX dwClsContext = CLSCTX.CLSCTX_LOCAL_SERVER)
53+
{
54+
Guid iid = typeof(T).GUID, clsid = typeof(U).GUID;
55+
return PInvoke.CoCreateInstance(&clsid, null, dwClsContext, &iid, (void**)this.GetAddressOf());
4756
}
4857

4958
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/Files.App/Services/Windows/WindowsWallpaperService.cs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)