Skip to content

Commit 24c5df6

Browse files
committed
🎹 CtrlCHelper: use P/Invoke source generation
1 parent 90f7627 commit 24c5df6

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

‎YoutubeDl.Wpf/Utils/CtrlCHelper.cs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ namespace YoutubeDl.Wpf.Utils;
88
/// Related issue: https://github.com/dotnet/runtime/issues/14628
99
/// Reference: https://stackoverflow.com/questions/283128/how-do-i-send-ctrlc-to-a-process-in-c
1010
/// </summary>
11-
internal static class CtrlCHelper
11+
internal static partial class CtrlCHelper
1212
{
1313
internal const int CTRL_C_EVENT = 0;
1414

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

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

21-
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
22-
internal static extern bool FreeConsole();
23+
[LibraryImport("kernel32.dll", SetLastError = true)]
24+
[return: MarshalAs(UnmanagedType.Bool)]
25+
internal static partial bool FreeConsole();
2326

24-
[DllImport("kernel32.dll")]
25-
internal static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? HandlerRoutine, bool Add);
27+
[LibraryImport("kernel32.dll")]
28+
[return: MarshalAs(UnmanagedType.Bool)]
29+
internal static partial bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? HandlerRoutine, [MarshalAs(UnmanagedType.Bool)] bool Add);
2630

2731
internal delegate bool ConsoleCtrlDelegate(uint dwCtrlType);
2832
}

‎YoutubeDl.Wpf/YoutubeDl.Wpf.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageId>YoutubeDl.Wpf</PackageId>
2222
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
2323
<PackageReadmeFile>README.md</PackageReadmeFile>
24+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2425
</PropertyGroup>
2526

2627
<ItemGroup>

0 commit comments

Comments
 (0)