Skip to content

Commit

Permalink
🎹 CtrlCHelper: use P/Invoke source generation
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed May 15, 2023
1 parent 90f7627 commit 24c5df6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
22 changes: 13 additions & 9 deletions YoutubeDl.Wpf/Utils/CtrlCHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ namespace YoutubeDl.Wpf.Utils;
/// Related issue: https://github.com/dotnet/runtime/issues/14628
/// Reference: https://stackoverflow.com/questions/283128/how-do-i-send-ctrlc-to-a-process-in-c
/// </summary>
internal static class CtrlCHelper
internal static partial class CtrlCHelper
{
internal const int CTRL_C_EVENT = 0;

[DllImport("kernel32.dll")]
internal static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool AttachConsole(uint dwProcessId);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool AttachConsole(uint dwProcessId);

[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern bool FreeConsole();
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool FreeConsole();

[DllImport("kernel32.dll")]
internal static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? HandlerRoutine, bool Add);
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? HandlerRoutine, [MarshalAs(UnmanagedType.Bool)] bool Add);

internal delegate bool ConsoleCtrlDelegate(uint dwCtrlType);
}
1 change: 1 addition & 0 deletions YoutubeDl.Wpf/YoutubeDl.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageId>YoutubeDl.Wpf</PackageId>
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
<PackageReadmeFile>README.md</PackageReadmeFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 24c5df6

Please sign in to comment.