Skip to content

Commit 98a1ed6

Browse files
authored
Code Quality: Bump CsWin32 to latest available (#15637)
1 parent 9b165fa commit 98a1ed6

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

src/Files.App.Server/Files.App.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<Manifest Include="app.manifest" />
4141
<TrimmerRootAssembly Include="Files.App.Server" />
4242
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.7" />
43-
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.647-beta" PrivateAssets="all" />
43+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106" PrivateAssets="all" />
4444
</ItemGroup>
4545

4646
<ItemGroup>

src/Files.App.Server/Helpers.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,31 @@
33

44
using System.Runtime.CompilerServices;
55
using System.Runtime.InteropServices;
6+
using Windows.Win32.Foundation;
7+
using Windows.Win32.System.WinRT;
68
using WinRT;
79

810
namespace Files.App.Server;
911

1012
unsafe partial class Helpers
1113
{
1214
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
13-
public static int GetActivationFactory(void* activatableClassId, void** factory)
15+
public static HRESULT GetActivationFactory(HSTRING activatableClassId, IActivationFactory** factory)
1416
{
15-
const int E_INVALIDARG = unchecked((int)0x80070057);
16-
const int CLASS_E_CLASSNOTAVAILABLE = unchecked((int)0x80040111);
17-
const int S_OK = 0;
18-
19-
if (activatableClassId is null || factory is null)
17+
if (activatableClassId.IsNull || factory is null)
2018
{
21-
return E_INVALIDARG;
19+
return HRESULT.E_INVALIDARG;
2220
}
2321

2422
try
2523
{
26-
IntPtr obj = Module.GetActivationFactory(MarshalString.FromAbi((IntPtr)activatableClassId));
27-
28-
if ((void*)obj is null)
29-
{
30-
return CLASS_E_CLASSNOTAVAILABLE;
31-
}
32-
33-
*factory = (void*)obj;
34-
return S_OK;
24+
*factory = (IActivationFactory*)Module.GetActivationFactory(MarshalString.FromAbi((IntPtr)activatableClassId));
25+
return *factory is null ? HRESULT.CLASS_E_CLASSNOTAVAILABLE : HRESULT.S_OK;
3526
}
3627
catch (Exception e)
3728
{
3829
ExceptionHelpers.SetErrorInfo(e);
39-
return ExceptionHelpers.GetHRForException(e);
30+
return (HRESULT)ExceptionHelpers.GetHRForException(e);
4031
}
4132
}
4233
}

src/Files.App.Server/NativeMethods.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
CLASS_E_CLASSNOTAVAILABLE
5+
E_INVALIDARG
6+
RoInitialize
47
RoRegisterActivationFactories
58
RoRevokeActivationFactories
69
WindowsCreateString
710
WindowsDeleteString
8-
RoInitialize

src/Files.App.Server/Program.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static async Task Main()
2121
{
2222
AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException;
2323

24-
nint cookie = 0;
24+
RO_REGISTRATION_COOKIE cookie = default;
2525

2626
_ = PInvoke.RoInitialize(RO_INIT_TYPE.RO_INIT_MULTITHREADED);
2727

@@ -42,11 +42,18 @@ static async Task Main()
4242

4343
unsafe
4444
{
45-
var callbacks = Enumerable.Repeat((nint)(delegate* unmanaged[Stdcall]<void*, void**, int>)&Helpers.GetActivationFactory, classIds.Length).ToArray();
45+
delegate* unmanaged[Stdcall]<HSTRING, IActivationFactory**, HRESULT>[] callbacks = new delegate* unmanaged[Stdcall]<HSTRING, IActivationFactory**, HRESULT>[classIds.Length];
46+
for (int i = 0; i < callbacks.Length; i++)
47+
{
48+
callbacks[i] = &Helpers.GetActivationFactory;
49+
}
4650

47-
if (PInvoke.RoRegisterActivationFactories(classIds, callbacks, out cookie) is HRESULT hr && hr.Value != 0)
51+
fixed (delegate* unmanaged[Stdcall]<HSTRING, IActivationFactory**, HRESULT>* pCallbacks = callbacks)
4852
{
49-
Marshal.ThrowExceptionForHR(hr);
53+
if (PInvoke.RoRegisterActivationFactories(classIds, pCallbacks, out cookie) is HRESULT hr && hr.Value != 0)
54+
{
55+
Marshal.ThrowExceptionForHR(hr);
56+
}
5057
}
5158
}
5259

src/Files.App/Files.App.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<PackageReference Include="Vanara.Windows.Shell" Version="4.0.1" />
9090
<PackageReference Include="Microsoft.Management.Infrastructure" Version="3.0.0" />
9191
<PackageReference Include="Microsoft.Management.Infrastructure.Runtime.Win" Version="3.0.0" />
92-
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta" PrivateAssets="all" />
92+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106" PrivateAssets="all" />
9393
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.7" />
9494
</ItemGroup>
9595

src/Files.App/Services/Storage/StorageNetworkService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public bool DisconnectNetworkDrive(ILocatableFolder drive)
184184
return
185185
PInvoke.WNetCancelConnection2W(
186186
drive.Path.TrimEnd('\\'),
187-
(uint)NET_USE_CONNECT_FLAGS.CONNECT_UPDATE_PROFILE,
187+
NET_CONNECT_FLAGS.CONNECT_UPDATE_PROFILE,
188188
true)
189189
is WIN32_ERROR.NO_ERROR;
190190
}

0 commit comments

Comments
 (0)