Skip to content

Commit

Permalink
Update NuGet packages and migrate from PInvoke to CsWin32
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lerch committed Dec 19, 2023
1 parent 91efd72 commit 7165d0f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Vocup.Packaging/Vocup.Packaging.wapproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@
</ItemGroup>
<Import Project="$(WapProjPath)\Microsoft.DesktopBridge.targets" />
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" PrivateAssets="all" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/Vocup/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SetForegroundWindow
GetCurrentPackageFullName
4 changes: 3 additions & 1 deletion src/Vocup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Vocup.Properties;
using Vocup.Settings;
using Vocup.Util;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Vocup;

Expand Down Expand Up @@ -101,7 +103,7 @@ private static void SwitchFocus()
Process? process = Process.GetProcessesByName(AppInfo.ProductName).OrderBy(x => x.StartTime).FirstOrDefault();
if (process != null && process.MainWindowHandle != IntPtr.Zero)
{
PInvoke.User32.SetForegroundWindow(process.MainWindowHandle);
PInvoke.SetForegroundWindow((HWND)process.MainWindowHandle);
}
else
{
Expand Down
30 changes: 13 additions & 17 deletions src/Vocup/Util/AppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;
using Windows.Management.Deployment;
using Windows.Win32.Foundation;

namespace Vocup.Util;

Expand All @@ -27,9 +26,14 @@ public static class AppInfo
/// </summary>
public static string SpecialCharDirectory => Path.Combine(Program.Settings.VhrPath, "specialchar");

public static Version Version { get; } = Version.Parse(
Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? throw new ApplicationException("Assembly version is undefined"));
private static readonly Lazy<Version> version = new(() =>
{
string? versionString = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? throw new ApplicationException("Assembly version is undefined");
string versionPart = versionString.Split('+')[0];
return Version.Parse(versionPart);
});
public static Version Version => version.Value;

public static Version FileVersion { get; } = new Version(1, 0);

Expand All @@ -41,16 +45,13 @@ public static class AppInfo
= Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>()?.Copyright
?? throw new ApplicationException("Assembly copyright information is undefined");

private static readonly Lazy<bool> isUwp = new Lazy<bool>(() =>
private static readonly Lazy<bool> isUwp = new(() =>
{
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10240))
{
int length = 0;
StringBuilder sb = new StringBuilder(0);
GetCurrentPackageFullName(ref length, sb);
sb = new StringBuilder(length);
int result = GetCurrentPackageFullName(ref length, sb);
return result != APPMODEL_ERROR_NO_PACKAGE;
uint length = 0;
WIN32_ERROR status = Windows.Win32.PInvoke.GetCurrentPackageFullName(ref length, null);
return status != WIN32_ERROR.APPMODEL_ERROR_NO_PACKAGE;
}
else return false;
});
Expand Down Expand Up @@ -122,9 +123,4 @@ public static bool TryGetVocupUwpApp([NotNullWhen(true)] out Version? version)
version = null;
return false;
}

private const int APPMODEL_ERROR_NO_PACKAGE = 15700;

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName);
}
4 changes: 3 additions & 1 deletion src/Vocup/Vocup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="LostTech.App.Settings" Version="0.5.0" />
<PackageReference Include="PInvoke.User32" Version="0.7.124" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Update="Resources\easter_egg.vhf">
Expand Down
8 changes: 4 additions & 4 deletions tests/Vocup.UnitTests/Vocup.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 7165d0f

Please sign in to comment.