Use GeneratedDllImport in System.IO.Pipes (#53947)
authorElinor Fung <elfung@microsoft.com>
Wed, 9 Jun 2021 19:05:32 +0000 (12:05 -0700)
committerGitHub <noreply@github.com>
Wed, 9 Jun 2021 19:05:32 +0000 (12:05 -0700)
23 files changed:
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Fcntl.Pipe.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetPeerID.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetPeerUserName.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Read.Pipe.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Stat.Pipe.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Write.Pipe.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ImpersonateNamedPipeClient.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.ConnectNamedPipe.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateNamedPipe.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateNamedPipeClient.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreatePipe_SafePipeHandle.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.DisconnectNamedPipe.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.DuplicateHandle_SafePipeHandle.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FlushFileBuffers.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetFileType_SafeHandle.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetNamedPipeHandleState.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetNamedPipeInfo.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.ReadFile_SafeHandle_IntPtr.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.ReadFile_SafeHandle_NativeOverlapped.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetNamedPipeHandleState.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.WaitNamedPipe.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.WriteFile_SafeHandle_IntPtr.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.WriteFile_SafeHandle_NativeOverlapped.cs

index 9e86930..6361f87 100644 (file)
@@ -13,11 +13,11 @@ internal static partial class Interop
         {
             internal static readonly bool CanGetSetPipeSz = (FcntlCanGetSetPipeSz() != 0);
 
-            [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FcntlGetPipeSz", SetLastError=true)]
-            internal static extern int GetPipeSz(SafePipeHandle fd);
+            [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FcntlGetPipeSz", SetLastError=true)]
+            internal static partial int GetPipeSz(SafePipeHandle fd);
 
-            [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FcntlSetPipeSz", SetLastError=true)]
-            internal static extern int SetPipeSz(SafePipeHandle fd, int size);
+            [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FcntlSetPipeSz", SetLastError=true)]
+            internal static partial int SetPipeSz(SafePipeHandle fd, int size);
 
             [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FcntlCanGetSetPipeSz")]
             [SuppressGCTransition]
index 2e9e6f1..23a338a 100644 (file)
@@ -8,7 +8,7 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPeerID", SetLastError = true)]
-        internal static extern int GetPeerID(SafeHandle socket, out uint euid);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPeerID", SetLastError = true)]
+        internal static partial int GetPeerID(SafeHandle socket, out uint euid);
     }
 }
index ccc7d9c..f45a343 100644 (file)
@@ -9,7 +9,7 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPeerUserName", SetLastError = true)]
-        internal static extern string GetPeerUserName(SafeHandle socket);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPeerUserName", SetLastError = true, CharSet = CharSet.Ansi)]
+        internal static partial string GetPeerUserName(SafeHandle socket);
     }
 }
index 09d21bc..c2ecd36 100644 (file)
@@ -18,7 +18,7 @@ internal static partial class Interop
         /// Returns the number of bytes read on success; otherwise, -1 is returned
         /// Note - on fail. the position of the stream may change depending on the platform; consult man 2 read for more info
         /// </returns>
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Read", SetLastError = true)]
-        internal static extern unsafe int Read(SafePipeHandle fd, byte* buffer, int count);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Read", SetLastError = true)]
+        internal static unsafe partial int Read(SafePipeHandle fd, byte* buffer, int count);
     }
 }
index 1852f98..ccf6a41 100644 (file)
@@ -9,7 +9,7 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FStat", SetLastError = true)]
-        internal static extern int FStat(SafePipeHandle fd, out FileStatus output);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FStat", SetLastError = true)]
+        internal static partial int FStat(SafePipeHandle fd, out FileStatus output);
     }
 }
index 2543be8..b71b9c5 100644 (file)
@@ -17,7 +17,7 @@ internal static partial class Interop
         /// <returns>
         /// Returns the number of bytes written on success; otherwise, returns -1 and sets errno
         /// </returns>
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Write", SetLastError = true)]
-        internal static extern unsafe int Write(SafePipeHandle fd, byte* buffer, int bufferSize);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Write", SetLastError = true)]
+        internal static unsafe partial int Write(SafePipeHandle fd, byte* buffer, int bufferSize);
     }
 }
index 2b72ecd..2aef33a 100644 (file)
@@ -9,8 +9,8 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
-        [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
         [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern bool ImpersonateNamedPipeClient(SafePipeHandle hNamedPipe);
+        internal static partial bool ImpersonateNamedPipeClient(SafePipeHandle hNamedPipe);
     }
 }
index 2805d3d..1838d72 100644 (file)
@@ -10,12 +10,12 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
         [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern unsafe bool ConnectNamedPipe(SafePipeHandle handle, NativeOverlapped* overlapped);
+        internal static unsafe partial bool ConnectNamedPipe(SafePipeHandle handle, NativeOverlapped* overlapped);
 
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
         [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern bool ConnectNamedPipe(SafePipeHandle handle, IntPtr overlapped);
+        internal static partial bool ConnectNamedPipe(SafePipeHandle handle, IntPtr overlapped);
     }
 }
index 3d88bf3..9afae1e 100644 (file)
@@ -9,8 +9,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false, EntryPoint = "CreateNamedPipeW")]
-        internal static extern SafePipeHandle CreateNamedPipe(
+        [GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreateNamedPipeW")]
+        internal static partial SafePipeHandle CreateNamedPipe(
             string pipeName,
             int openMode,
             int pipeMode,
index 4c8b9cb..f5cad97 100644 (file)
@@ -10,8 +10,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
-        internal static extern SafePipeHandle CreateNamedPipeClient(
+        [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial SafePipeHandle CreateNamedPipeClient(
             string? lpFileName,
             int dwDesiredAccess,
             System.IO.FileShare dwShareMode,
index 92a3257..0b1e88a 100644 (file)
@@ -9,7 +9,7 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern bool CreatePipe(out SafePipeHandle hReadPipe, out SafePipeHandle hWritePipe, ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static partial bool CreatePipe(out SafePipeHandle hReadPipe, out SafePipeHandle hWritePipe, ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);
     }
 }
index ccef10e..c0d1376 100644 (file)
@@ -9,8 +9,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
         [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern bool DisconnectNamedPipe(SafePipeHandle hNamedPipe);
+        internal static partial bool DisconnectNamedPipe(SafePipeHandle hNamedPipe);
     }
 }
index 96726ab..88f2b8c 100644 (file)
@@ -9,8 +9,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern bool DuplicateHandle(
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static partial bool DuplicateHandle(
             IntPtr hSourceProcessHandle,
             SafeHandle hSourceHandle,
             IntPtr hTargetProcessHandle,
index 094ef61..9b0e857 100644 (file)
@@ -7,8 +7,14 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        [return: MarshalAs(UnmanagedType.Bool)]
+        internal static partial bool FlushFileBuffers(SafeHandle hHandle);
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         [return: MarshalAs(UnmanagedType.Bool)]
         internal static extern bool FlushFileBuffers(SafeHandle hHandle);
+#endif
     }
 }
index 557866d..d5d2062 100644 (file)
@@ -7,7 +7,12 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static partial int GetFileType(SafeHandle hFile);
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         internal static extern int GetFileType(SafeHandle hFile);
+#endif
     }
 }
index f98c2c1..74c60e6 100644 (file)
@@ -8,8 +8,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
-        internal static extern unsafe bool GetNamedPipeHandleStateW(
+        [GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
+        internal static unsafe partial bool GetNamedPipeHandleStateW(
             SafePipeHandle hNamedPipe,
             uint* lpState,
             uint* lpCurInstances,
index 586f661..b370c8e 100644 (file)
@@ -8,8 +8,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern unsafe bool GetNamedPipeInfo(
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial bool GetNamedPipeInfo(
             SafePipeHandle hNamedPipe,
             uint* lpFlags,
             uint* lpOutBufferSize,
index d2ab765..9651a10 100644 (file)
@@ -8,8 +8,13 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial int ReadFile(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         internal static extern unsafe int ReadFile(
+#endif
             SafeHandle handle,
             byte* bytes,
             int numBytesToRead,
index c6fb2c4..ba9b6a9 100644 (file)
@@ -9,16 +9,26 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial int ReadFile(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         internal static extern unsafe int ReadFile(
+#endif
             SafeHandle handle,
             byte* bytes,
             int numBytesToRead,
             IntPtr numBytesRead_mustBeZero,
             NativeOverlapped* overlapped);
 
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial int ReadFile(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         internal static extern unsafe int ReadFile(
+#endif
             SafeHandle handle,
             byte* bytes,
             int numBytesToRead,
index 70f0ab1..2ee9911 100644 (file)
@@ -9,9 +9,9 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
         [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern unsafe bool SetNamedPipeHandleState(
+        internal static unsafe partial bool SetNamedPipeHandleState(
           SafePipeHandle hNamedPipe,
           int* lpMode,
           IntPtr lpMaxCollectionCount,
index cb95a8e..76abe2b 100644 (file)
@@ -7,8 +7,8 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false, EntryPoint = "WaitNamedPipeW")]
+        [GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "WaitNamedPipeW")]
         [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern bool WaitNamedPipe(string? name, int timeout);
+        internal static partial bool WaitNamedPipe(string? name, int timeout);
     }
 }
index 9101041..6354c1a 100644 (file)
@@ -15,7 +15,17 @@ internal static partial class Interop
         // struct in a callback (or an EndWrite method called by that callback),
         // and pass in an address for the numBytesRead parameter.
 
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial int WriteFile(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern unsafe int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
+        internal static extern unsafe int WriteFile(
+#endif
+            SafeHandle handle,
+            byte* bytes,
+            int numBytesToWrite,
+            out int numBytesWritten,
+            IntPtr mustBeZero);
     }
 }
index 3eaff9c..0f66079 100644 (file)
@@ -15,10 +15,30 @@ internal static partial class Interop
         // simultaneously: overlapped IO, free the memory for the overlapped
         // struct in a callback (or an EndWrite method called by that callback),
         // and pass in an address for the numBytesRead parameter.
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial int WriteFile(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern unsafe int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
+        internal static extern unsafe int WriteFile(
+#endif
+            SafeHandle handle,
+            byte* bytes,
+            int numBytesToWrite,
+            IntPtr numBytesWritten_mustBeZero,
+            NativeOverlapped* lpOverlapped);
 
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static unsafe partial int WriteFile(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern unsafe int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, NativeOverlapped* lpOverlapped);
+        internal static extern unsafe int WriteFile(
+#endif
+            SafeHandle handle,
+            byte* bytes,
+            int numBytesToWrite,
+            out int numBytesWritten,
+            NativeOverlapped* lpOverlapped);
     }
 }