Skip to content

Commit aefc0c8

Browse files
hez2010hishitetsu
andauthored
Code Quality: Various fixes to the WinRT server call (#15229)
Co-authored-by: hishitetsu <[email protected]>
1 parent be16191 commit aefc0c8

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

src/Files.App.Server/NativeMethods.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"$schema": "https://aka.ms/CsWin32.schema.json",
55

66
// Emit COM interfaces instead of structs, and allow generation of non-blittable structs for the sake of an easier to use API.
7-
"allowMarshaling": true,
7+
"allowMarshaling": false,
88

99
// A value indicating whether to generate APIs judged to be unnecessary or redundant given the target framework.
1010
// This is useful for multi-targeting projects that need a consistent set of APIs across target frameworks
@@ -24,5 +24,5 @@
2424
"className": "PInvoke",
2525

2626
// A value indicating whether to expose the generated APIs publicly (as opposed to internally).
27-
"public": true
27+
"public": false
2828
}

src/Files.App.Server/NativeMethods.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ RoRevokeActivationFactories
66
WindowsCreateString
77
WindowsDeleteString
88
RoInitialize
9-
RoUninitialize

src/Files.App.Server/Program.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,36 @@ static async Task Main()
2323

2424
nint cookie = 0;
2525

26-
unsafe
27-
{
28-
_ = PInvoke.RoInitialize(RO_INIT_TYPE.RO_INIT_MULTITHREADED);
26+
_ = PInvoke.RoInitialize(RO_INIT_TYPE.RO_INIT_MULTITHREADED);
2927

30-
var classIds = typeof(Program).Assembly.GetTypes()
31-
.Where(t => t.IsSealed && t.IsPublic && t.IsClass)
32-
.Select(t => t.FullName!)
33-
.Where(name => name.StartsWith("Files.App.Server.", StringComparison.Ordinal))
34-
.Select(name =>
28+
var classIds = typeof(Program).Assembly.GetTypes()
29+
.Where(t => t.IsSealed && t.IsPublic && t.IsClass)
30+
.Select(t => t.FullName!)
31+
.Where(name => name.StartsWith("Files.App.Server.", StringComparison.Ordinal))
32+
.Select(name =>
33+
{
34+
if (PInvoke.WindowsCreateString(name, (uint)name.Length, out var classId) is HRESULT hr && hr.Value is not 0)
3535
{
36-
if (PInvoke.WindowsCreateString(name, (uint)name.Length, out var classId) is HRESULT hr && hr.Value is not 0)
37-
{
38-
Marshal.ThrowExceptionForHR(hr);
39-
}
36+
Marshal.ThrowExceptionForHR(hr);
37+
}
4038

41-
return new HSTRING(classId.DangerousGetHandle());
42-
})
43-
.ToArray();
39+
return new HSTRING(classId.DangerousGetHandle());
40+
})
41+
.ToArray();
4442

43+
unsafe
44+
{
4545
var callbacks = Enumerable.Repeat((nint)(delegate* unmanaged[Stdcall]<void*, void**, int>)&Helpers.GetActivationFactory, classIds.Length).ToArray();
4646

4747
if (PInvoke.RoRegisterActivationFactories(classIds, callbacks, out cookie) is HRESULT hr && hr.Value != 0)
4848
{
4949
Marshal.ThrowExceptionForHR(hr);
5050
}
51+
}
5152

52-
foreach (var str in classIds)
53-
{
54-
_ = PInvoke.WindowsDeleteString(str);
55-
}
53+
foreach (var str in classIds)
54+
{
55+
_ = PInvoke.WindowsDeleteString(str);
5656
}
5757

5858
AppDomain.CurrentDomain.ProcessExit += (_, _) => cancellationTokenSource.Cancel();
@@ -72,8 +72,6 @@ static async Task Main()
7272
{
7373
PInvoke.RoRevokeActivationFactories(cookie);
7474
}
75-
76-
PInvoke.RoUninitialize();
7775
}
7876
}
7977

src/Files.App/Helpers/Layout/LayoutPreferencesManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ public static void SetLayoutPreferencesForPath(string path, LayoutPreferencesIte
505505
{
506506
path = path.TrimPath() ?? string.Empty;
507507

508+
if (path.StartsWith("tag:", StringComparison.Ordinal))
509+
return GetLayoutPreferencesFromDatabase("Home", null);
510+
508511
var folderFRN = NativeFileOperationsHelper.GetFolderFRN(path);
509512

510513
return GetLayoutPreferencesFromDatabase(path, folderFRN)

src/Files.App/Program.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,46 @@ private static void Main()
5757
{
5858
WinRT.ComWrappersSupport.InitializeComWrappers();
5959

60+
// We are about to do the first WinRT server call, in case the WinRT server is hanging
61+
// we need to kill the server if there is no other Files instances already running
62+
63+
static bool ProcessPathPredicate(Process p)
64+
{
65+
try
66+
{
67+
return p.MainModule?.FileName
68+
.StartsWith(Windows.ApplicationModel.Package.Current.EffectivePath, StringComparison.OrdinalIgnoreCase) ?? false;
69+
}
70+
catch
71+
{
72+
return false;
73+
}
74+
}
75+
76+
var processes = Process.GetProcessesByName("Files")
77+
.Where(ProcessPathPredicate)
78+
.Where(p => p.Id != Environment.ProcessId);
79+
80+
if (!processes.Any())
81+
{
82+
foreach (var process in Process.GetProcessesByName("Files.App.Server").Where(ProcessPathPredicate))
83+
{
84+
try
85+
{
86+
process.Kill();
87+
}
88+
catch
89+
{
90+
// ignore any exceptions
91+
}
92+
finally
93+
{
94+
process.Dispose();
95+
}
96+
}
97+
}
98+
99+
// Now we can do the first WinRT server call
60100
Server.AppInstanceMonitor.StartMonitor(Environment.ProcessId);
61101

62102
var OpenTabInExistingInstance = ApplicationData.Current.LocalSettings.Values.Get("OpenTabInExistingInstance", true);

src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
8484
return destFile;
8585
}
8686
}
87-
catch (UnauthorizedAccessException ex) // shortcuts & .url
87+
catch (UnauthorizedAccessException) // shortcuts & .url
8888
{
8989
if (!string.IsNullOrEmpty(destFolder.Path))
9090
{
@@ -102,7 +102,7 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
102102
return new NativeStorageFile(destination, desiredNewName, DateTime.Now);
103103
}
104104
}
105-
throw ex;
105+
throw;
106106
}
107107
});
108108
}

0 commit comments

Comments
 (0)