44
55namespace GTerm . Extensions
66{
7- internal class Win32Extensions
7+ internal partial class Win32Extensions
88 {
99 internal const int SW_MINIMIZE = 6 ;
1010 internal const int SW_HIDE = 0 ;
1111
1212 internal delegate bool ConsoleEventDelegate ( int eventType ) ;
1313
14- [ DllImport ( "Kernel32.dll" , CallingConvention = CallingConvention . StdCall , SetLastError = true ) ]
15- internal static extern IntPtr GetConsoleWindow ( ) ;
14+ [ LibraryImport ( "Kernel32.dll" , SetLastError = true ) ]
15+ [ UnmanagedCallConv ( CallConvs = new [ ] { typeof ( System . Runtime . CompilerServices . CallConvStdcall ) } ) ]
16+ internal static partial IntPtr GetConsoleWindow ( ) ;
1617
17- [ DllImport ( "User32.dll" , CallingConvention = CallingConvention . StdCall , SetLastError = true ) ]
18+ [ LibraryImport ( "User32.dll" , SetLastError = true ) ]
19+ [ UnmanagedCallConv ( CallConvs = new [ ] { typeof ( System . Runtime . CompilerServices . CallConvStdcall ) } ) ]
1820 [ return : MarshalAs ( UnmanagedType . Bool ) ]
19- internal static extern bool ShowWindow ( [ In ] IntPtr hWnd , [ In ] int nCmdShow ) ;
21+ internal static partial bool ShowWindow ( IntPtr hWnd , int nCmdShow ) ;
2022
21- [ DllImport ( "User32.dll" , CallingConvention = CallingConvention . StdCall , SetLastError = true ) ]
23+ [ LibraryImport ( "User32.dll" , SetLastError = true ) ]
24+ [ UnmanagedCallConv ( CallConvs = new [ ] { typeof ( System . Runtime . CompilerServices . CallConvStdcall ) } ) ]
2225 [ return : MarshalAs ( UnmanagedType . Bool ) ]
23- internal static extern bool IsWindowVisible ( [ In ] IntPtr hWnd ) ;
26+ internal static partial bool IsWindowVisible ( IntPtr hWnd ) ;
2427
25- [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
26- internal static extern bool SetConsoleCtrlHandler ( ConsoleEventDelegate callback , bool add ) ;
28+ [ LibraryImport ( "kernel32.dll" , SetLastError = true ) ]
29+ [ return : MarshalAs ( UnmanagedType . Bool ) ]
30+ internal static partial bool SetConsoleCtrlHandler ( ConsoleEventDelegate callback , [ MarshalAs ( UnmanagedType . Bool ) ] bool add ) ;
2731
2832 internal enum WinMessages : uint
2933 {
3034 /// <summary>
31- /// An application sends the WM_SETICON message to associate a new large or small icon with a window.
32- /// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption.
35+ /// An application sends the WM_SETICON message to associate a new large or small icon with a window.
36+ /// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption.
3337 /// </summary>
3438 SETICON = 0x0080 ,
3539 }
3640
37- [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
38- private static extern IntPtr SendMessage ( IntPtr hWnd , int Msg , int wParam , IntPtr lParam ) ;
41+ [ LibraryImport ( "user32.dll" , EntryPoint = "SendMessageW" ) ]
42+ private static partial IntPtr SendMessage ( IntPtr hWnd , int Msg , int wParam , IntPtr lParam ) ;
3943
44+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows6.1" ) ]
4045 private static void SetWindowIcon ( System . Drawing . Icon icon )
4146 {
42- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) return ;
43-
44- IntPtr mwHandle = System . Diagnostics . Process . GetCurrentProcess ( ) . MainWindowHandle ;
47+ IntPtr mwHandle = Process . GetCurrentProcess ( ) . MainWindowHandle ;
4548 SendMessage ( mwHandle , ( int ) WinMessages . SETICON , 0 , icon . Handle ) ;
4649 SendMessage ( mwHandle , ( int ) WinMessages . SETICON , 1 , icon . Handle ) ;
4750 }
4851
4952 internal static void SetConsoleIcon ( string iconFilePath )
5053 {
51- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) return ;
52-
54+ if ( ! OperatingSystem . IsWindowsVersionAtLeast ( 6 , 1 ) ) return ;
55+
5356 if ( ! string . IsNullOrEmpty ( iconFilePath ) )
5457 {
5558 System . Drawing . Icon ? icon = System . Drawing . Icon . ExtractAssociatedIcon ( iconFilePath ) ;
@@ -65,7 +68,7 @@ internal static void SetConsoleIcon(string iconFilePath)
6568 /// A utility class to determine a process parent.
6669 /// </summary>
6770 [ StructLayout ( LayoutKind . Sequential ) ]
68- internal struct ParentProcessUtilities
71+ internal partial struct ParentProcessUtilities
6972 {
7073 // These members must match PROCESS_BASIC_INFORMATION
7174 internal IntPtr Reserved1 ;
@@ -75,8 +78,8 @@ internal struct ParentProcessUtilities
7578 internal IntPtr UniqueProcessId ;
7679 internal IntPtr InheritedFromUniqueProcessId ;
7780
78- [ DllImport ( "ntdll.dll" ) ]
79- private static extern int NtQueryInformationProcess ( IntPtr processHandle , int processInformationClass , ref ParentProcessUtilities processInformation , int processInformationLength , out int returnLength ) ;
81+ [ LibraryImport ( "ntdll.dll" ) ]
82+ private static partial int NtQueryInformationProcess ( IntPtr processHandle , int processInformationClass , ref ParentProcessUtilities processInformation , int processInformationLength , out int returnLength ) ;
8083
8184 /// <summary>
8285 /// Gets the parent process of a specified process.
@@ -85,8 +88,8 @@ internal struct ParentProcessUtilities
8588 /// <returns>An instance of the Process class.</returns>
8689 public static Process ? GetParentProcess ( IntPtr handle )
8790 {
88- ParentProcessUtilities pbi = new ParentProcessUtilities ( ) ;
89- int status = NtQueryInformationProcess ( handle , 0 , ref pbi , Marshal . SizeOf ( pbi ) , out int returnLength ) ;
91+ ParentProcessUtilities pbi = new ( ) ;
92+ int status = NtQueryInformationProcess ( handle , 0 , ref pbi , Marshal . SizeOf ( pbi ) , out int _ ) ;
9093 if ( status != 0 )
9194 throw new Win32Exception ( status ) ;
9295
0 commit comments