summaryrefslogtreecommitdiff
path: root/src/mscorlib/corefx/Interop/Windows/kernel32
diff options
context:
space:
mode:
Diffstat (limited to 'src/mscorlib/corefx/Interop/Windows/kernel32')
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CancelIoEx.cs16
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CloseHandle.cs16
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CreateFile.cs40
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileOperations.cs12
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileTypes.cs16
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FlushFileBuffers.cs17
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FormatMessage.cs112
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileInformationByHandleEx.cs26
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileType_SafeHandle.cs15
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFullPathNameW.cs18
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetLongPathNameW.cs18
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempFileNameW.cs16
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempPathW.cs16
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.LockFile.cs20
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_IntPtr.cs21
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_NativeOverlapped.cs22
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SECURITY_ATTRIBUTES.cs21
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SafeCreateFile.cs45
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SecurityOptions.cs18
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetEndOfFile.cs15
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetErrorMode.cs14
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFileInformationByHandle.cs39
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFilePointerEx.cs15
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.UnsafeCreateFile.cs25
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WideCharToMultiByte.cs22
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_IntPtr.cs24
-rw-r--r--src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_NativeOverlapped.cs22
27 files changed, 661 insertions, 0 deletions
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CancelIoEx.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CancelIoEx.cs
new file mode 100644
index 0000000000..fc99e3052f
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CancelIoEx.cs
@@ -0,0 +1,16 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern unsafe bool CancelIoEx(SafeHandle handle, NativeOverlapped* lpOverlapped);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CloseHandle.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CloseHandle.cs
new file mode 100644
index 0000000000..96ed922a84
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CloseHandle.cs
@@ -0,0 +1,16 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool CloseHandle(IntPtr handle);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CreateFile.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CreateFile.cs
new file mode 100644
index 0000000000..5f6f6115ab
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.CreateFile.cs
@@ -0,0 +1,40 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ /// <summary>
+ /// WARNING: This method does not implicitly handle long paths. Use CreateFile.
+ /// </summary>
+ [DllImport(Libraries.Kernel32, EntryPoint = "CreateFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
+ private static extern SafeFileHandle CreateFilePrivate(
+ string lpFileName,
+ int dwDesiredAccess,
+ System.IO.FileShare dwShareMode,
+ [In] ref SECURITY_ATTRIBUTES securityAttrs,
+ System.IO.FileMode dwCreationDisposition,
+ int dwFlagsAndAttributes,
+ IntPtr hTemplateFile);
+
+ internal static SafeFileHandle CreateFile(
+ string lpFileName,
+ int dwDesiredAccess,
+ System.IO.FileShare dwShareMode,
+ [In] ref SECURITY_ATTRIBUTES securityAttrs,
+ System.IO.FileMode dwCreationDisposition,
+ int dwFlagsAndAttributes,
+ IntPtr hTemplateFile)
+ {
+ lpFileName = PathInternal.EnsureExtendedPrefixOverMaxPath(lpFileName);
+ return CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileOperations.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileOperations.cs
new file mode 100644
index 0000000000..6e3ebb9ae9
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileOperations.cs
@@ -0,0 +1,12 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+
+ internal const uint SEM_FAILCRITICALERRORS = 1;
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileTypes.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileTypes.cs
new file mode 100644
index 0000000000..1d306665b1
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FileTypes.cs
@@ -0,0 +1,16 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ internal partial class FileTypes
+ {
+ internal const int FILE_TYPE_DISK = 0x0001;
+ internal const int FILE_TYPE_CHAR = 0x0002;
+ internal const int FILE_TYPE_PIPE = 0x0003;
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FlushFileBuffers.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FlushFileBuffers.cs
new file mode 100644
index 0000000000..e10a2279cf
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FlushFileBuffers.cs
@@ -0,0 +1,17 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool FlushFileBuffers(SafeHandle hHandle);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FormatMessage.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FormatMessage.cs
new file mode 100644
index 0000000000..94722b6c8b
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.FormatMessage.cs
@@ -0,0 +1,112 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Text;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
+ private const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
+ private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
+ private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
+
+
+ private const int ERROR_INSUFFICIENT_BUFFER = 0x7A;
+
+ [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", SetLastError = true, BestFitMapping = true)]
+ private static extern int FormatMessage(
+ int dwFlags,
+ IntPtr lpSource,
+ uint dwMessageId,
+ int dwLanguageId,
+ [Out] StringBuilder lpBuffer,
+ int nSize,
+ IntPtr[] arguments);
+
+ /// <summary>
+ /// Returns a string message for the specified Win32 error code.
+ /// </summary>
+ internal static string GetMessage(int errorCode)
+ {
+ return GetMessage(IntPtr.Zero, errorCode);
+ }
+
+ internal static string GetMessage(IntPtr moduleHandle, int errorCode)
+ {
+ var sb = new StringBuilder(InitialBufferSize);
+ do
+ {
+ string errorMsg;
+ if (TryGetErrorMessage(moduleHandle, errorCode, sb, out errorMsg))
+ {
+ return errorMsg;
+ }
+ else
+ {
+ // increase the capacity of the StringBuilder.
+ sb.Capacity *= BufferSizeIncreaseFactor;
+ }
+ }
+ while (sb.Capacity < MaxAllowedBufferSize);
+
+ // If you come here then a size as large as 65K is also not sufficient and so we give the generic errorMsg.
+ return string.Format("Unknown error (0x{0:x})", errorCode);
+ }
+
+ private static bool TryGetErrorMessage(IntPtr moduleHandle, int errorCode, StringBuilder sb, out string errorMsg)
+ {
+ errorMsg = "";
+
+ int flags = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY;
+ if (moduleHandle != IntPtr.Zero)
+ {
+ flags |= FORMAT_MESSAGE_FROM_HMODULE;
+ }
+
+ int result = FormatMessage(flags, moduleHandle, (uint)errorCode, 0, sb, sb.Capacity, null);
+ if (result != 0)
+ {
+ int i = sb.Length;
+ while (i > 0)
+ {
+ char ch = sb[i - 1];
+ if (ch > 32 && ch != '.') break;
+ i--;
+ }
+ errorMsg = sb.ToString(0, i);
+ }
+ else if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
+ {
+ return false;
+ }
+ else
+ {
+ errorMsg = string.Format("Unknown error (0x{0:x})", errorCode);
+ }
+
+ return true;
+ }
+
+ // Windows API FormatMessage lets you format a message string given an errorcode.
+ // Unlike other APIs this API does not support a way to query it for the total message size.
+ //
+ // So the API can only be used in one of these two ways.
+ // a. You pass a buffer of appropriate size and get the resource.
+ // b. Windows creates a buffer and passes the address back and the onus of releasing the buffer lies on the caller.
+ //
+ // Since the error code is coming from the user, it is not possible to know the size in advance.
+ // Unfortunately we can't use option b. since the buffer can only be freed using LocalFree and it is a private API on onecore.
+ // Also, using option b is ugly for the managed code and could cause memory leak in situations where freeing is unsuccessful.
+ //
+ // As a result we use the following approach.
+ // We initially call the API with a buffer size of 256 and then gradually increase the size in case of failure until we reach the maximum allowed limit of 65K.
+ private const int InitialBufferSize = 256;
+ private const int BufferSizeIncreaseFactor = 4;
+ private const int MaxAllowedBufferSize = 65 * 1024;
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileInformationByHandleEx.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileInformationByHandleEx.cs
new file mode 100644
index 0000000000..146c4638ee
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileInformationByHandleEx.cs
@@ -0,0 +1,26 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern bool GetFileInformationByHandleEx(SafeFileHandle hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, out FILE_STANDARD_INFO lpFileInformation, uint dwBufferSize);
+
+ internal partial struct FILE_STANDARD_INFO
+ {
+ internal long AllocationSize;
+ internal long EndOfFile;
+ internal uint NumberOfLinks;
+ internal BOOL DeletePending;
+ internal BOOL Directory;
+ }
+
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileType_SafeHandle.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileType_SafeHandle.cs
new file mode 100644
index 0000000000..c07a1683a5
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFileType_SafeHandle.cs
@@ -0,0 +1,15 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal extern static int GetFileType(SafeHandle hFile);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFullPathNameW.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFullPathNameW.cs
new file mode 100644
index 0000000000..15dd581113
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetFullPathNameW.cs
@@ -0,0 +1,18 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ /// <summary>
+ /// WARNING: This method does not implicitly handle long paths. Use GetFullPathName or PathHelper.
+ /// </summary>
+ [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
+ unsafe internal static extern uint GetFullPathNameW(char* path, uint numBufferChars, char[] buffer, IntPtr mustBeZero);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetLongPathNameW.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetLongPathNameW.cs
new file mode 100644
index 0000000000..a58d1013ca
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetLongPathNameW.cs
@@ -0,0 +1,18 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+partial class Interop
+{
+ partial class Kernel32
+ {
+ /// <summary>
+ /// WARNING: This method does not implicitly handle long paths. Use GetFullPath/PathHelper.
+ /// </summary>
+ [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
+ internal static extern uint GetLongPathNameW(char[] lpszShortPath, char[] lpszLongPath, uint cchBuffer);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempFileNameW.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempFileNameW.cs
new file mode 100644
index 0000000000..d157a29c92
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempFileNameW.cs
@@ -0,0 +1,16 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Text;
+using System.Runtime.InteropServices;
+
+partial class Interop
+{
+ partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
+ internal static extern uint GetTempFileNameW(string tmpPath, string prefix, uint uniqueIdOrZero, [Out]StringBuilder tmpFileName);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempPathW.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempPathW.cs
new file mode 100644
index 0000000000..25ffcd55b0
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.GetTempPathW.cs
@@ -0,0 +1,16 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.IO;
+using System.Text;
+using System.Runtime.InteropServices;
+
+partial class Interop
+{
+ partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, BestFitMapping = false)]
+ internal static extern uint GetTempPathW(int bufferLen, [Out]StringBuilder buffer);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.LockFile.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.LockFile.cs
new file mode 100644
index 0000000000..a21d00f4f6
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.LockFile.cs
@@ -0,0 +1,20 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern bool LockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
+
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern bool UnlockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_IntPtr.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_IntPtr.cs
new file mode 100644
index 0000000000..076f7f136f
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_IntPtr.cs
@@ -0,0 +1,21 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern unsafe int ReadFile(
+ SafeHandle handle,
+ byte* bytes,
+ int numBytesToRead,
+ out int numBytesRead,
+ IntPtr mustBeZero);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_NativeOverlapped.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_NativeOverlapped.cs
new file mode 100644
index 0000000000..3ae65a8806
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.ReadFile_SafeHandle_NativeOverlapped.cs
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern unsafe int ReadFile(
+ SafeHandle handle,
+ byte* bytes,
+ int numBytesToRead,
+ IntPtr numBytesRead_mustBeZero,
+ NativeOverlapped* overlapped);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SECURITY_ATTRIBUTES.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SECURITY_ATTRIBUTES.cs
new file mode 100644
index 0000000000..8d31f8622f
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SECURITY_ATTRIBUTES.cs
@@ -0,0 +1,21 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [StructLayout(LayoutKind.Sequential)]
+ internal struct SECURITY_ATTRIBUTES
+ {
+ internal uint nLength;
+ internal IntPtr lpSecurityDescriptor;
+ internal BOOL bInheritHandle;
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SafeCreateFile.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SafeCreateFile.cs
new file mode 100644
index 0000000000..4192f569e0
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SafeCreateFile.cs
@@ -0,0 +1,45 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+using Microsoft.Win32.SafeHandles;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); // WinBase.h
+
+ /// <summary>
+ /// Does not allow access to non-file devices. This disallows DOS devices like "con:", "com1:",
+ /// "lpt1:", etc. Use this to avoid security problems, like allowing a web client asking a server
+ /// for "http://server/com1.aspx" and then causing a worker process to hang.
+ /// </summary>
+ internal static SafeFileHandle SafeCreateFile(
+ String lpFileName,
+ int dwDesiredAccess,
+ System.IO.FileShare dwShareMode,
+ ref Interop.Kernel32.SECURITY_ATTRIBUTES securityAttrs,
+ FileMode dwCreationDisposition,
+ int dwFlagsAndAttributes,
+ IntPtr hTemplateFile)
+ {
+ SafeFileHandle handle = UnsafeCreateFile(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+
+ if (!handle.IsInvalid)
+ {
+ int fileType = Interop.Kernel32.GetFileType(handle);
+ if (fileType != Interop.Kernel32.FileTypes.FILE_TYPE_DISK)
+ {
+ handle.Dispose();
+ throw new NotSupportedException(SR.NotSupported_FileStreamOnNonFiles);
+ }
+ }
+
+ return handle;
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SecurityOptions.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SecurityOptions.cs
new file mode 100644
index 0000000000..4a4402484f
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SecurityOptions.cs
@@ -0,0 +1,18 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ internal partial class SecurityOptions
+ {
+ internal const int SECURITY_SQOS_PRESENT = 0x00100000;
+ internal const int SECURITY_ANONYMOUS = 0 << 16;
+ internal const int SECURITY_IDENTIFICATION = 1 << 16;
+ internal const int SECURITY_IMPERSONATION = 2 << 16;
+ internal const int SECURITY_DELEGATION = 3 << 16;
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetEndOfFile.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetEndOfFile.cs
new file mode 100644
index 0000000000..e5d60041a8
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetEndOfFile.cs
@@ -0,0 +1,15 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern bool SetEndOfFile(SafeFileHandle hFile);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetErrorMode.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetErrorMode.cs
new file mode 100644
index 0000000000..caa2ce5bfa
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetErrorMode.cs
@@ -0,0 +1,14 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = false, EntryPoint = "SetErrorMode", ExactSpelling = true)]
+ internal static extern uint SetErrorMode(uint newMode);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFileInformationByHandle.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFileInformationByHandle.cs
new file mode 100644
index 0000000000..e31a453ba9
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFileInformationByHandle.cs
@@ -0,0 +1,39 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ internal enum FILE_INFO_BY_HANDLE_CLASS : uint
+ {
+ FileBasicInfo = 0x0u,
+ FileStandardInfo = 0x1u,
+ FileNameInfo = 0x2u,
+ FileRenameInfo = 0x3u,
+ FileDispositionInfo = 0x4u,
+ FileAllocationInfo = 0x5u,
+ FileEndOfFileInfo = 0x6u,
+ FileStreamInfo = 0x7u,
+ FileCompressionInfo = 0x8u,
+ FileAttributeTagInfo = 0x9u,
+ FileIdBothDirectoryInfo = 0xAu,
+ FileIdBothDirectoryRestartInfo = 0xBu,
+ FileIoPriorityHintInfo = 0xCu,
+ FileRemoteProtocolInfo = 0xDu,
+ FileFullDirectoryInfo = 0xEu,
+ FileFullDirectoryRestartInfo = 0xFu,
+ FileStorageInfo = 0x10u,
+ FileAlignmentInfo = 0x11u,
+ FileIdInfo = 0x12u,
+ FileIdExtdDirectoryInfo = 0x13u,
+ FileIdExtdDirectoryRestartInfo = 0x14u,
+ MaximumFileInfoByHandleClass = 0x15u,
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFilePointerEx.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFilePointerEx.cs
new file mode 100644
index 0000000000..c0e5247a52
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.SetFilePointerEx.cs
@@ -0,0 +1,15 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern bool SetFilePointerEx(SafeFileHandle hFile, long liDistanceToMove, out long lpNewFilePointer, uint dwMoveMethod);
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.UnsafeCreateFile.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.UnsafeCreateFile.cs
new file mode 100644
index 0000000000..9a5cd2834d
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.UnsafeCreateFile.cs
@@ -0,0 +1,25 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.IO;
+using Microsoft.Win32.SafeHandles;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ internal static SafeFileHandle UnsafeCreateFile(
+ string lpFileName,
+ int dwDesiredAccess,
+ FileShare dwShareMode,
+ ref Interop.Kernel32.SECURITY_ATTRIBUTES securityAttrs,
+ FileMode dwCreationDisposition,
+ int dwFlagsAndAttributes,
+ IntPtr hTemplateFile)
+ {
+ return CreateFile(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+ }
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WideCharToMultiByte.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WideCharToMultiByte.cs
new file mode 100644
index 0000000000..07271cae33
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WideCharToMultiByte.cs
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ [DllImport(Libraries.Kernel32)]
+ internal static extern unsafe int WideCharToMultiByte(
+ uint CodePage, uint dwFlags,
+ char* lpWideCharStr, int cchWideChar,
+ byte* lpMultiByteStr, int cbMultiByte,
+ IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar);
+
+ internal const uint CP_ACP = 0;
+ internal const uint WC_NO_BEST_FIT_CHARS = 0x00000400;
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_IntPtr.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_IntPtr.cs
new file mode 100644
index 0000000000..e7e868e142
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_IntPtr.cs
@@ -0,0 +1,24 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ // Note there are two different WriteFile prototypes - this is to use
+ // the type system to force you to not trip across a "feature" in
+ // Win32's async IO support. You can't do the following three things
+ // 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.
+
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern unsafe int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
+
+ }
+}
diff --git a/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_NativeOverlapped.cs b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_NativeOverlapped.cs
new file mode 100644
index 0000000000..dc1e97555b
--- /dev/null
+++ b/src/mscorlib/corefx/Interop/Windows/kernel32/Interop.WriteFile_SafeHandle_NativeOverlapped.cs
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+internal partial class Interop
+{
+ internal partial class Kernel32
+ {
+ // Note there are two different WriteFile prototypes - this is to use
+ // the type system to force you to not trip across a "feature" in
+ // Win32's async IO support. You can't do the following three things
+ // 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.
+ [DllImport(Libraries.Kernel32, SetLastError = true)]
+ internal static extern unsafe int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
+ }
+}