Skip to content

Commit 9974720

Browse files
committed
Update Vanara dependency. TBC: do not use WebAuthenticationBroker
1 parent c58539d commit 9974720

File tree

8 files changed

+38
-34
lines changed

8 files changed

+38
-34
lines changed

src/Files.FullTrust/ContextMenu.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static ContextMenu GetContextMenuForFiles(ShellItem[] shellItems, Shell32
121121
}
122122

123123
using var sf = shellItems[0].Parent; // HP: the items are all in the same folder
124-
Shell32.IContextMenu menu = sf.GetChildrenUIObjects<Shell32.IContextMenu>(null, shellItems);
124+
Shell32.IContextMenu menu = sf.GetChildrenUIObjects<Shell32.IContextMenu>(HWND.NULL, shellItems);
125125
var hMenu = User32.CreatePopupMenu();
126126
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags);
127127
var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName));
@@ -155,7 +155,7 @@ private static ContextMenu GetContextMenuForFolder(ShellFolder shellFolder, Shel
155155
return null;
156156
}
157157

158-
var sv = shellFolder.GetViewObject<Shell32.IShellView>(null);
158+
var sv = shellFolder.GetViewObject<Shell32.IShellView>(HWND.NULL);
159159
Shell32.IContextMenu menu = sv.GetItemObject<Shell32.IContextMenu>(Shell32.SVGIO.SVGIO_BACKGROUND);
160160
var hMenu = User32.CreatePopupMenu();
161161
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags);

src/Files.FullTrust/Files.FullTrust.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
3+
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
44
<OutputType>WinExe</OutputType>
55
<RootNamespace>FilesFullTrust</RootNamespace>
66
<AssemblyName>FilesFullTrust</AssemblyName>
@@ -28,7 +28,8 @@
2828
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2929
<PackageReference Include="Tulpep.ActiveDirectoryObjectPicker" Version="3.0.11" />
3030
<PackageReference Include="Vanara.PInvoke.Mpr" Version="3.4.6" />
31-
<PackageReference Include="Vanara.Windows.Shell" Version="3.3.15" />
31+
<PackageReference Include="Vanara.PInvoke.UserEnv" Version="3.4.6" />
32+
<PackageReference Include="Vanara.Windows.Shell" Version="3.4.6" />
3233
</ItemGroup>
3334
<ItemGroup>
3435
<ProjectReference Include="..\Files.Shared\Files.Shared.csproj" />

src/Files.FullTrust/MessageHandlers/FileOperationsHandler.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ await Win32API.StartSTATask(() =>
292292
op.Options = ShellFileOperations.OperationFlags.Silent
293293
| ShellFileOperations.OperationFlags.NoConfirmation
294294
| ShellFileOperations.OperationFlags.NoErrorUI;
295-
op.OwnerWindow = Win32API.Win32Window.FromLong(ownerHwnd);
295+
op.OwnerWindow = new HWND(new IntPtr(ownerHwnd));
296296
if (!permanently)
297297
{
298298
op.Options |= ShellFileOperations.OperationFlags.RecycleOnDelete
@@ -451,7 +451,7 @@ await Win32API.StartSTATask(() =>
451451
op.Options = ShellFileOperations.OperationFlags.NoConfirmMkDir
452452
| ShellFileOperations.OperationFlags.Silent
453453
| ShellFileOperations.OperationFlags.NoErrorUI;
454-
op.OwnerWindow = Win32API.Win32Window.FromLong(ownerHwnd);
454+
op.OwnerWindow = new HWND(new IntPtr(ownerHwnd));
455455
op.Options |= !overwriteOnMove ? ShellFileOperations.OperationFlags.PreserveFileExtensions | ShellFileOperations.OperationFlags.RenameOnCollision
456456
: ShellFileOperations.OperationFlags.NoConfirmation;
457457

@@ -535,7 +535,7 @@ await Win32API.StartSTATask(() =>
535535
op.Options = ShellFileOperations.OperationFlags.NoConfirmMkDir
536536
| ShellFileOperations.OperationFlags.Silent
537537
| ShellFileOperations.OperationFlags.NoErrorUI;
538-
op.OwnerWindow = Win32API.Win32Window.FromLong(ownerHwnd);
538+
op.OwnerWindow = new HWND(new IntPtr(ownerHwnd));
539539
op.Options |= !overwriteOnCopy ? ShellFileOperations.OperationFlags.PreserveFileExtensions | ShellFileOperations.OperationFlags.RenameOnCollision
540540
: ShellFileOperations.OperationFlags.NoConfirmation;
541541

@@ -642,7 +642,7 @@ await Win32API.StartSTATask(() =>
642642
var linkPath = (string)message["filepath"];
643643
if (linkPath.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase))
644644
{
645-
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, null, TimeSpan.FromMilliseconds(100));
645+
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, HWND.NULL, TimeSpan.FromMilliseconds(100));
646646
await Win32API.SendMessageAsync(connection, new ValueSet()
647647
{
648648
{ "ShortcutInfo", JsonConvert.SerializeObject(ShellFolderExtensions.GetShellLinkItem(link)) }
@@ -716,7 +716,7 @@ await Win32API.StartSTATask(() =>
716716
try
717717
{
718718
var linkPath = (string)message["filepath"];
719-
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, null, TimeSpan.FromMilliseconds(100));
719+
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, HWND.NULL, TimeSpan.FromMilliseconds(100));
720720
link.IconLocation = new IconLocation((string)message["iconFile"], (int)message.Get("iconIndex", 0L));
721721
link.SaveAs(linkPath); // Overwrite if exists
722722
await Win32API.SendMessageAsync(connection, new ValueSet() { { "Success", true } }, message.Get("RequestID", (string)null));
@@ -949,7 +949,7 @@ private class OperationWithProgress
949949
private readonly Shell32.ITaskbarList4 taskbar;
950950
private readonly ConcurrentDictionary<string, OperationWithProgress> operations;
951951

952-
public System.Windows.Forms.IWin32Window OwnerWindow { get; set; }
952+
public HWND OwnerWindow { get; set; }
953953

954954
public ProgressHandler(PipeStream conn)
955955
{
@@ -1024,11 +1024,11 @@ private void UpdateTaskbarProgress()
10241024
}
10251025
if (operations.Any())
10261026
{
1027-
taskbar.SetProgressValue(OwnerWindow.Handle, (ulong)Progress, 100);
1027+
taskbar.SetProgressValue(OwnerWindow.DangerousGetHandle(), (ulong)Progress, 100);
10281028
}
10291029
else
10301030
{
1031-
taskbar.SetProgressState(OwnerWindow.Handle, Shell32.TBPFLAG.TBPF_NOPROGRESS);
1031+
taskbar.SetProgressState(OwnerWindow.DangerousGetHandle(), Shell32.TBPFLAG.TBPF_NOPROGRESS);
10321032
}
10331033
}
10341034

src/Files.FullTrust/MessageHandlers/RecentItemsHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private async Task HandleShellRecentItemsMessage(Dictionary<string, object> mess
115115

116116
foreach (var linkFilePath in linkFilePaths)
117117
{
118-
using var link = new ShellLink(linkFilePath, LinkResolution.NoUIWithMsgPump, null, TimeSpan.FromMilliseconds(100));
118+
using var link = new ShellLink(linkFilePath, LinkResolution.NoUIWithMsgPump, HWND.NULL, TimeSpan.FromMilliseconds(100));
119119

120120
try
121121
{

src/Files.FullTrust/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Threading.Tasks;
1717
using Windows.Foundation.Collections;
1818
using Windows.Storage;
19+
using Windows.ApplicationModel;
1920

2021
namespace Files.FullTrust
2122
{
@@ -101,7 +102,10 @@ private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionE
101102

102103
private static async void InitializeAppServiceConnection()
103104
{
104-
var packageSid = ApplicationData.Current.LocalSettings.Values["PackageSid"];
105+
Vanara.PInvoke.UserEnv.DeriveAppContainerSidFromAppContainerName(Package.Current.Id.FamilyName, out var ppsid);
106+
var packageSid = new StringBuilder(2048);
107+
Vanara.PInvoke.Kernel32.GetAppContainerNamedObjectPath(Vanara.PInvoke.HTOKEN.NULL, ppsid, (uint)packageSid.Capacity, packageSid, out _);
108+
ppsid.Dispose();
105109
connection = new NamedPipeClientStream(".",
106110
$"Sessions\\{Process.GetCurrentProcess().SessionId}\\AppContainerNamedObjects\\{packageSid}\\FilesInteropService_ServerPipe",
107111
PipeDirection.InOut, PipeOptions.Asynchronous);

src/Files.FullTrust/Win32API.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ public static (string icon, string overlay) GetFileIconAndOverlay(string path, i
212212
<= 48 => Shell32.SHIL.SHIL_EXTRALARGE,
213213
_ => Shell32.SHIL.SHIL_JUMBO,
214214
};
215-
if (!Shell32.SHGetImageList(imageListSize, typeof(ComCtl32.IImageList).GUID, out var imageList).Succeeded)
215+
if (!Shell32.SHGetImageList(imageListSize, typeof(ComCtl32.IImageList).GUID, out var imlObj).Succeeded
216+
|| imlObj is not ComCtl32.IImageList imageList)
216217
{
217218
return (iconStr, null);
218219
}
@@ -226,8 +227,7 @@ public static (string icon, string overlay) GetFileIconAndOverlay(string path, i
226227
using var hIcon = imageList.GetIcon(iconIdx, ComCtl32.IMAGELISTDRAWFLAGS.ILD_TRANSPARENT);
227228
if (!hIcon.IsNull && !hIcon.IsInvalid)
228229
{
229-
using (var icon = hIcon.ToIcon())
230-
using (var image = icon.ToBitmap())
230+
using (var image = hIcon.ToHBITMAP())
231231
{
232232
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
233233
iconStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
@@ -257,9 +257,7 @@ public static (string icon, string overlay) GetFileIconAndOverlay(string path, i
257257
using var hOverlay = imageList.GetIcon(overlayImage, ComCtl32.IMAGELISTDRAWFLAGS.ILD_TRANSPARENT);
258258
if (!hOverlay.IsNull && !hOverlay.IsInvalid)
259259
{
260-
using var icon = hOverlay.ToIcon();
261-
using var image = icon.ToBitmap();
262-
260+
using var image = hOverlay.ToHBITMAP();
263261
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
264262
overlayStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
265263
}
@@ -313,7 +311,7 @@ public static IList<IconFileInfo> ExtractSelectedIconsFromDLL(string file, IList
313311
// This is merely to pass into the function and is unneeded otherwise
314312
if (Shell32.SHDefExtractIcon(file, -1 * index, 0, out User32.SafeHICON icon, out User32.SafeHICON hIcon2, Convert.ToUInt32(iconSize)) == HRESULT.S_OK)
315313
{
316-
using var image = icon.ToBitmap();
314+
using var image = icon.ToHBITMAP();
317315
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
318316
var icoStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
319317
iconsList.Add(new IconFileInfo(icoStr, index));
@@ -338,7 +336,7 @@ public static IList<IconFileInfo> ExtractIconsFromDLL(string file)
338336
if (maxIndex == 0)
339337
{
340338
using var icon = Shell32.ExtractIcon(currentProc.Handle, file, 0);
341-
using var image = icon.ToBitmap();
339+
using var image = icon.ToHBITMAP();
342340

343341
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
344342
var icoStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
@@ -349,7 +347,7 @@ public static IList<IconFileInfo> ExtractIconsFromDLL(string file)
349347
for (int i = 0; i <= maxIndex; i++)
350348
{
351349
using var icon = Shell32.ExtractIcon(currentProc.Handle, file, i);
352-
using var image = icon.ToBitmap();
350+
using var image = icon.ToHBITMAP();
353351

354352
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
355353
var icoStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);

src/Files.Uwp/Helpers/AppServiceConnectionHelper.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ private static async Task<NamedPipeAsAppServiceConnection> BuildConnection(bool
102102
if (launchFullTrust)
103103
{
104104
// Launch fulltrust process
105-
ApplicationData.Current.LocalSettings.Values["PackageSid"] =
106-
/*
107-
TODO UA306_G: UWP WebAuthenticationBroker : Windows.Security.Authentication.Web.WebAuthenticationBroker is not yet supported in WindowsAppSDK. Read: https://docs.microsoft.com/en-us/windows/apps/windows-app-sdk/migrate-to-windows-app-sdk/what-is-supported
108-
*/WebAuthenticationBroker.GetCurrentApplicationCallbackUri().Host.ToUpperInvariant();
109-
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
105+
// WINUI3: TODO UA306_G: UWP WebAuthenticationBroker : Windows.Security.Authentication.Web.WebAuthenticationBroker is not yet supported in WindowsAppSDK. Read: https://docs.microsoft.com/en-us/windows/apps/windows-app-sdk/migrate-to-windows-app-sdk/what-is-supported
106+
//ApplicationData.Current.LocalSettings.Values["PackageSid"] = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().Host.ToUpperInvariant();
107+
var ftpPath = Path.Combine(Package.Current.InstalledLocation.Path, "Files.FullTrust", "FilesFullTrust.exe");
108+
System.Diagnostics.Process.Start(ftpPath);
110109
}
111110

112111
var connection = new NamedPipeAsAppServiceConnection();

src/Files.Uwp/Program.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static async Task Main()
9797
case ParsedCommandType.ExplorerShellCommand:
9898
if (!CommonPaths.ShellPlaces.ContainsKey(command.Payload.ToUpperInvariant()))
9999
{
100-
await OpenShellCommandInExplorerAsync(command.Payload, proc.Id);
100+
OpenShellCommandInExplorerAsync(command.Payload, proc.Id);
101101
return; // Exit
102102
}
103103
break;
@@ -118,7 +118,7 @@ private static async Task Main()
118118
await instance.RedirectActivationToAsync(activatedArgs);
119119
// Terminate "zombie" Files process which remains in suspended state
120120
// after redirection when launched by command line
121-
await TerminateUwpAppInstance(proc.Id);
121+
//TerminateUwpAppInstance(proc.Id); // WINUI3: check if needed
122122
return;
123123
}
124124
}
@@ -148,19 +148,21 @@ private static void OnActivated(object? sender, AppActivationArguments args)
148148
}
149149
}
150150

151-
public static async Task OpenShellCommandInExplorerAsync(string shellCommand, int pid)
151+
public static void OpenShellCommandInExplorerAsync(string shellCommand, int pid)
152152
{
153153
ApplicationData.Current.LocalSettings.Values["ShellCommand"] = shellCommand;
154154
ApplicationData.Current.LocalSettings.Values["Arguments"] = "ShellCommand";
155155
ApplicationData.Current.LocalSettings.Values["pid"] = pid;
156-
await Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
156+
var ftpPath = System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Files.FullTrust", "FilesFullTrust.exe");
157+
System.Diagnostics.Process.Start(ftpPath);
157158
}
158159

159-
public static async Task TerminateUwpAppInstance(int pid)
160+
public static void TerminateUwpAppInstance(int pid)
160161
{
161162
ApplicationData.Current.LocalSettings.Values["Arguments"] = "TerminateUwp";
162163
ApplicationData.Current.LocalSettings.Values["pid"] = pid;
163-
await Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
164+
var ftpPath = System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Files.FullTrust", "FilesFullTrust.exe");
165+
System.Diagnostics.Process.Start(ftpPath);
164166
}
165167
}
166168
}

0 commit comments

Comments
 (0)