Skip to content

Commit 9e14906

Browse files
committed
Fix: Fixed potential COMException when setting the wallpaper (#13921)
1 parent 3c20ae1 commit 9e14906

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,4 +3608,7 @@
36083608
<data name="FailedToRestore" xml:space="preserve">
36093609
<value>Failed to restore items from Recycle Bin</value>
36103610
</data>
3611+
<data name="FailedToSetBackground" xml:space="preserve">
3612+
<value>Failed to set the background wallpaper</value>
3613+
</data>
36113614
</root>
Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System;
5-
using System.Linq;
6-
using System.Threading.Tasks;
4+
using Microsoft.UI.Xaml.Controls;
75
using Vanara.PInvoke;
6+
using Windows.Foundation.Metadata;
87
using Windows.Storage;
98
using Windows.System.UserProfile;
109

@@ -14,12 +13,20 @@ public static class WallpaperHelpers
1413
{
1514
public static async Task SetAsBackgroundAsync(WallpaperType type, string filePath)
1615
{
16+
1717
if (type == WallpaperType.Desktop)
1818
{
19-
// Set the desktop background
20-
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
21-
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
22-
wallpaper.SetWallpaper(monitorId, filePath);
19+
try
20+
{
21+
// Set the desktop background
22+
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
23+
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
24+
wallpaper.SetWallpaper(monitorId, filePath);
25+
}
26+
catch (Exception ex)
27+
{
28+
ShowErrorPrompt(ex.Message);
29+
}
2330
}
2431
else if (type == WallpaperType.LockScreen)
2532
{
@@ -34,15 +41,37 @@ public static void SetSlideshow(string[] filePaths)
3441
if (filePaths is null || !filePaths.Any())
3542
return;
3643

37-
var idList = filePaths.Select(Shell32.IntILCreateFromPath).ToArray();
38-
Shell32.SHCreateShellItemArrayFromIDLists((uint)idList.Length, idList.ToArray(), out var shellItemArray);
44+
try
45+
{
46+
var idList = filePaths.Select(Shell32.IntILCreateFromPath).ToArray();
47+
Shell32.SHCreateShellItemArrayFromIDLists((uint)idList.Length, idList.ToArray(), out var shellItemArray);
48+
49+
// Set SlideShow
50+
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
51+
wallpaper.SetSlideshow(shellItemArray);
52+
53+
// Set wallpaper to fill desktop.
54+
wallpaper.SetPosition(Shell32.DESKTOP_WALLPAPER_POSITION.DWPOS_FILL);
55+
}
56+
catch (Exception ex)
57+
{
58+
ShowErrorPrompt(ex.Message);
59+
}
60+
}
61+
62+
private static async void ShowErrorPrompt(string exception)
63+
{
64+
var errorDialog = new ContentDialog()
65+
{
66+
Title = "FailedToSetBackground".GetLocalizedResource(),
67+
Content = exception,
68+
PrimaryButtonText = "OK".GetLocalizedResource(),
69+
};
3970

40-
// Set SlideShow
41-
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
42-
wallpaper.SetSlideshow(shellItemArray);
71+
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
72+
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
4373

44-
// Set wallpaper to fill desktop.
45-
wallpaper.SetPosition(Shell32.DESKTOP_WALLPAPER_POSITION.DWPOS_FILL);
74+
await errorDialog.TryShowAsync();
4675
}
4776
}
4877
}

0 commit comments

Comments
 (0)