From a70d82ac2caeb6f8c6cdf5b045b0d273881f447e Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Tue, 1 Mar 2022 19:25:07 -0800 Subject: [PATCH] Address TODO to switch to generated p/invoke --- .../Advapi32/Interop.ChangeServiceConfig2.cs | 6 ------ .../Interop.SERVICE_DELAYED_AUTOSTART_INFO.cs | 17 ----------------- .../Interop.CreateToolhelp32Snapshot.cs | 18 +++++++----------- ...t.Extensions.Hosting.WindowsServices.csproj | 3 +++ ...rocess.ServiceController.TestService.csproj | 2 -- 5 files changed, 10 insertions(+), 36 deletions(-) delete mode 100644 src/libraries/Common/src/Interop/Windows/Advapi32/Interop.SERVICE_DELAYED_AUTOSTART_INFO.cs diff --git a/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ChangeServiceConfig2.cs b/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ChangeServiceConfig2.cs index 4c3dff1685caef..c3a331f0996f0a 100644 --- a/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ChangeServiceConfig2.cs +++ b/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ChangeServiceConfig2.cs @@ -11,11 +11,5 @@ internal static partial class Advapi32 { [GeneratedDllImport(Libraries.Advapi32, EntryPoint = "ChangeServiceConfig2W", SetLastError = true)] public static partial bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DESCRIPTION serviceDesc); - -#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time - // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable types. - [DllImport(Libraries.Advapi32, EntryPoint = "ChangeServiceConfig2W", ExactSpelling = true, SetLastError = true)] - public static extern bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DELAYED_AUTOSTART_INFO serviceDesc); -#pragma warning restore DLLIMPORTGENANALYZER015 } } diff --git a/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.SERVICE_DELAYED_AUTOSTART_INFO.cs b/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.SERVICE_DELAYED_AUTOSTART_INFO.cs deleted file mode 100644 index 6319225c17d136..00000000000000 --- a/src/libraries/Common/src/Interop/Windows/Advapi32/Interop.SERVICE_DELAYED_AUTOSTART_INFO.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.Runtime.InteropServices; - -internal static partial class Interop -{ - internal static partial class Advapi32 - { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public struct SERVICE_DELAYED_AUTOSTART_INFO - { - public bool fDelayedAutostart; - } - } -} diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs index b0dff37964bf08..3d5b3b48061adc 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs @@ -21,8 +21,8 @@ internal enum SnapshotFlags : uint NoHeaps = 0x40000000 } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - internal struct PROCESSENTRY32 + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + internal unsafe struct PROCESSENTRY32 { internal int dwSize; internal int cntUsage; @@ -33,23 +33,19 @@ internal struct PROCESSENTRY32 internal int th32ParentProcessID; internal int pcPriClassBase; internal int dwFlags; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] - internal string szExeFile; + internal fixed char szExeFile[MAX_PATH]; } // https://docs.microsoft.com/windows/desktop/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)] internal static partial IntPtr CreateToolhelp32Snapshot(SnapshotFlags dwFlags, uint th32ProcessID); -#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time - // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable types. // https://docs.microsoft.com/windows/desktop/api/tlhelp32/nf-tlhelp32-process32first - [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - internal static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); + [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "Process32FirstW", SetLastError = true)] + internal static partial bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); // https://docs.microsoft.com/windows/desktop/api/tlhelp32/nf-tlhelp32-process32next - [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - internal static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); -#pragma warning restore DLLIMPORTGENANALYZER015 + [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "Process32NextW", SetLastError = true)] + internal static partial bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); } } diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj index e68a5c2e805338..7db6b60f93327b 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj @@ -12,6 +12,9 @@ + -