Skip to content

Code Quality: Bump CsWin32 to latest available #15637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Files.App.Server/Files.App.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Manifest Include="app.manifest" />
<TrimmerRootAssembly Include="Files.App.Server" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.7" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.647-beta" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 8 additions & 17 deletions src/Files.App.Server/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,31 @@

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Win32.Foundation;
using Windows.Win32.System.WinRT;
using WinRT;

namespace Files.App.Server;

unsafe partial class Helpers
{
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
public static int GetActivationFactory(void* activatableClassId, void** factory)
public static HRESULT GetActivationFactory(HSTRING activatableClassId, IActivationFactory** factory)
{
const int E_INVALIDARG = unchecked((int)0x80070057);
const int CLASS_E_CLASSNOTAVAILABLE = unchecked((int)0x80040111);
const int S_OK = 0;

if (activatableClassId is null || factory is null)
if (activatableClassId.IsNull || factory is null)
{
return E_INVALIDARG;
return HRESULT.E_INVALIDARG;
}

try
{
IntPtr obj = Module.GetActivationFactory(MarshalString.FromAbi((IntPtr)activatableClassId));

if ((void*)obj is null)
{
return CLASS_E_CLASSNOTAVAILABLE;
}

*factory = (void*)obj;
return S_OK;
*factory = (IActivationFactory*)Module.GetActivationFactory(MarshalString.FromAbi((IntPtr)activatableClassId));
return *factory is null ? HRESULT.CLASS_E_CLASSNOTAVAILABLE : HRESULT.S_OK;
}
catch (Exception e)
{
ExceptionHelpers.SetErrorInfo(e);
return ExceptionHelpers.GetHRForException(e);
return (HRESULT)ExceptionHelpers.GetHRForException(e);
}
}
}
4 changes: 3 additions & 1 deletion src/Files.App.Server/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

CLASS_E_CLASSNOTAVAILABLE
E_INVALIDARG
RoInitialize
RoRegisterActivationFactories
RoRevokeActivationFactories
WindowsCreateString
WindowsDeleteString
RoInitialize
15 changes: 11 additions & 4 deletions src/Files.App.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static async Task Main()
{
AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException;

nint cookie = 0;
RO_REGISTRATION_COOKIE cookie = default;

_ = PInvoke.RoInitialize(RO_INIT_TYPE.RO_INIT_MULTITHREADED);

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

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

if (PInvoke.RoRegisterActivationFactories(classIds, callbacks, out cookie) is HRESULT hr && hr.Value != 0)
fixed (delegate* unmanaged[Stdcall]<HSTRING, IActivationFactory**, HRESULT>* pCallbacks = callbacks)
{
Marshal.ThrowExceptionForHR(hr);
if (PInvoke.RoRegisterActivationFactories(classIds, pCallbacks, out cookie) is HRESULT hr && hr.Value != 0)
{
Marshal.ThrowExceptionForHR(hr);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<PackageReference Include="Vanara.Windows.Shell" Version="4.0.1" />
<PackageReference Include="Microsoft.Management.Infrastructure" Version="3.0.0" />
<PackageReference Include="Microsoft.Management.Infrastructure.Runtime.Win" Version="3.0.0" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.7" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Services/Storage/StorageNetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public bool DisconnectNetworkDrive(ILocatableFolder drive)
return
PInvoke.WNetCancelConnection2W(
drive.Path.TrimEnd('\\'),
(uint)NET_USE_CONNECT_FLAGS.CONNECT_UPDATE_PROFILE,
NET_CONNECT_FLAGS.CONNECT_UPDATE_PROFILE,
true)
is WIN32_ERROR.NO_ERROR;
}
Expand Down
Loading