Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit b48b2cb

Browse files
authored
Merge pull request #495 from qmfrederik/features/deviceiocontrol-nativeoverlapped
Add NativeOverlapped overloads
2 parents c493509 + 53b9104 commit b48b2cb

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/Kernel32.Tests/storebanned/Kernel32Facts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public unsafe void CancelIoEx_CancelWriteAll()
624624
Assert.Equal(Win32ErrorCode.ERROR_IO_PENDING, lastError);
625625
try
626626
{
627-
var cancelled = CancelIoEx(file, null);
627+
var cancelled = CancelIoEx(file, (NativeOverlapped*)null);
628628

629629
// We can't assert that it's true as if the IO finished already it'll fail with ERROR_NOT_FOUND
630630
if (!cancelled)

src/Kernel32/Kernel32.Helpers.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace PInvoke
66
using System;
77
using System.Runtime.InteropServices;
88
using System.Text;
9+
using System.Threading;
910

1011
/// <content>
1112
/// Methods and nested types that are not strictly P/Invokes but provide
@@ -256,6 +257,54 @@ public static ArraySegment<byte> ReadFile(SafeObjectHandle hFile, int nNumberOfB
256257
return new ArraySegment<byte>(buffer, 0, bytesRead);
257258
}
258259

260+
/// <inheritdoc cref="CancelIoEx(SafeObjectHandle, OVERLAPPED*)" />
261+
[NoFriendlyOverloads]
262+
public static unsafe bool CancelIoEx(
263+
SafeObjectHandle hFile,
264+
NativeOverlapped* lpOverlapped)
265+
{
266+
return CancelIoEx(hFile, (OVERLAPPED*)lpOverlapped);
267+
}
268+
269+
/// <inheritdoc cref="ReadFile(SafeObjectHandle, void*, int, int*, OVERLAPPED*)"/>
270+
[NoFriendlyOverloads]
271+
public static unsafe bool ReadFile(
272+
SafeObjectHandle hFile,
273+
void* lpBuffer,
274+
int nNumberOfBytesToRead,
275+
int* lpNumberOfBytesRead,
276+
NativeOverlapped* lpOverlapped)
277+
{
278+
return ReadFile(hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, (OVERLAPPED*)lpOverlapped);
279+
}
280+
281+
/// <inheritdoc cref="WriteFile(SafeObjectHandle, void*, int, int*, OVERLAPPED*)"/>
282+
[NoFriendlyOverloads]
283+
public static unsafe bool WriteFile(
284+
SafeObjectHandle hFile,
285+
void* lpBuffer,
286+
int nNumberOfBytesToWrite,
287+
int* lpNumberOfBytesWritten,
288+
NativeOverlapped* lpOverlapped)
289+
{
290+
return WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, (OVERLAPPED*)lpOverlapped);
291+
}
292+
293+
/// <inheritdoc cref="DeviceIoControl(SafeObjectHandle, int, void*, int, void*, int, out int, OVERLAPPED*)"/>
294+
[NoFriendlyOverloads]
295+
public static unsafe bool DeviceIoControl(
296+
SafeObjectHandle hDevice,
297+
int dwIoControlCode,
298+
void* inBuffer,
299+
int nInBufferSize,
300+
void* outBuffer,
301+
int nOutBufferSize,
302+
out int pBytesReturned,
303+
NativeOverlapped* lpOverlapped)
304+
{
305+
return DeviceIoControl(hDevice, dwIoControlCode, inBuffer, nInBufferSize, outBuffer, nOutBufferSize, out pBytesReturned, (OVERLAPPED*)lpOverlapped);
306+
}
307+
259308
/// <summary>
260309
/// Tries to get the error message text using the supplied buffer.
261310
/// </summary>

src/Kernel32/PublicAPI.Unshipped.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const PInvoke.Kernel32.PROCESS_MEMORY_EXHAUSTION_INFO.PME_CURRENT_VERSION = 1 ->
203203
const PInvoke.Kernel32.PROCESS_POWER_THROTTLING_STATE.PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1 -> uint
204204
override PInvoke.Kernel32.SafePseudoConsoleHandle.IsInvalid.get -> bool
205205
override PInvoke.Kernel32.SafePseudoConsoleHandle.ReleaseHandle() -> bool
206+
static PInvoke.Kernel32.CancelIoEx(PInvoke.Kernel32.SafeObjectHandle hFile, System.Threading.NativeOverlapped* lpOverlapped) -> bool
206207
static PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES? lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, System.IntPtr lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, ref uint? lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
207208
static PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES? lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, ref uint? lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
208209
static PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, System.IntPtr lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, System.IntPtr lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, System.IntPtr lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
@@ -212,6 +213,7 @@ static PInvoke.Kernel32.CreateRemoteThreadEx(System.IntPtr hProcess, System.IntP
212213
static PInvoke.Kernel32.CreateThread(PInvoke.Kernel32.SECURITY_ATTRIBUTES? lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, System.IntPtr lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, ref uint? lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
213214
static PInvoke.Kernel32.CreateThread(PInvoke.Kernel32.SECURITY_ATTRIBUTES? lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, ref uint? lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
214215
static PInvoke.Kernel32.CreateThread(System.IntPtr lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, System.IntPtr lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, System.IntPtr lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
216+
static PInvoke.Kernel32.DeviceIoControl(PInvoke.Kernel32.SafeObjectHandle hDevice, int dwIoControlCode, void* inBuffer, int nInBufferSize, void* outBuffer, int nOutBufferSize, out int pBytesReturned, System.Threading.NativeOverlapped* lpOverlapped) -> bool
215217
static PInvoke.Kernel32.DeviceIoControlAsync<TInput, TOutput>(PInvoke.Kernel32.SafeObjectHandle hDevice, uint dwIoControlCode, System.Memory<TInput> inBuffer, System.Memory<TOutput> outBuffer, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<uint>
216218
static PInvoke.Kernel32.DosDateTimeToFileTime(ushort wFatDate, ushort wFatTime, System.IntPtr lpFileTime) -> bool
217219
static PInvoke.Kernel32.DosDateTimeToFileTime(ushort wFatDate, ushort wFatTime, out PInvoke.Kernel32.FILETIME lpFileTime) -> bool
@@ -220,14 +222,17 @@ static PInvoke.Kernel32.GetCurrentProcessorNumberEx(System.IntPtr ProcNumber) ->
220222
static PInvoke.Kernel32.GetCurrentProcessorNumberEx(out PInvoke.Kernel32.PROCESSOR_NUMBER ProcNumber) -> void
221223
static PInvoke.Kernel32.GetNativeSystemInfo(System.IntPtr lpSystemInfo) -> void
222224
static PInvoke.Kernel32.GetNativeSystemInfo(out PInvoke.Kernel32.SYSTEM_INFO lpSystemInfo) -> void
225+
static PInvoke.Kernel32.GetOverlappedResult(PInvoke.Kernel32.SafeObjectHandle hFile, System.Threading.NativeOverlapped* lpOverlapped, out int lpNumberOfBytesTransferred, bool bWait) -> bool
223226
static PInvoke.Kernel32.GetProcessInformation(PInvoke.Kernel32.SafeObjectHandle hProcess, PInvoke.Kernel32.PROCESS_INFORMATION_CLASS ProcessInformationClass, System.IntPtr ProcessInformation, uint ProcessInformationSize) -> bool
224227
static PInvoke.Kernel32.GetVolumeInformation(string lpRootPathName, System.IntPtr lpVolumeNameBuffer, int nVolumeNameSize, out uint lpVolumeSerialNumber, out int lpMaximumComponentLength, out PInvoke.Kernel32.FileSystemFlags lpFileSystemFlags, System.IntPtr lpFileSystemNameBuffer, int nFileSystemNameSize) -> bool
225228
static PInvoke.Kernel32.GetVolumeInformation(string lpRootPathName, char[] lpVolumeNameBuffer, int nVolumeNameSize, out uint lpVolumeSerialNumber, out int lpMaximumComponentLength, out PInvoke.Kernel32.FileSystemFlags lpFileSystemFlags, char[] lpFileSystemNameBuffer, int nFileSystemNameSize) -> bool
226229
static PInvoke.Kernel32.OSVERSIONINFO.Create() -> PInvoke.Kernel32.OSVERSIONINFO
230+
static PInvoke.Kernel32.ReadFile(PInvoke.Kernel32.SafeObjectHandle hFile, void* lpBuffer, int nNumberOfBytesToRead, int* lpNumberOfBytesRead, System.Threading.NativeOverlapped* lpOverlapped) -> bool
227231
static PInvoke.Kernel32.ReadProcessMemory(PInvoke.Kernel32.SafeObjectHandle hProcess, System.IntPtr lpBaseAddress, System.IntPtr lpBuffer, System.UIntPtr nSize, out System.UIntPtr lpNumberOfBytesRead) -> bool
228232
static PInvoke.Kernel32.SetFilePointer(PInvoke.Kernel32.SafeObjectHandle hFile, int lDistanceToMove, System.IntPtr lpDistanceToMoveHigh, System.IO.SeekOrigin dwMoveMethod) -> int
229233
static PInvoke.Kernel32.SetFilePointer(PInvoke.Kernel32.SafeObjectHandle hFile, int lDistanceToMove, ref int? lpDistanceToMoveHigh, System.IO.SeekOrigin dwMoveMethod) -> int
230234
static PInvoke.Kernel32.SetProcessInformation(PInvoke.Kernel32.SafeObjectHandle hProcess, PInvoke.Kernel32.PROCESS_INFORMATION_CLASS ProcessInformationClass, System.IntPtr ProcessInformation, uint ProcessInformationSize) -> bool
235+
static PInvoke.Kernel32.WriteFile(PInvoke.Kernel32.SafeObjectHandle hFile, void* lpBuffer, int nNumberOfBytesToWrite, int* lpNumberOfBytesWritten, System.Threading.NativeOverlapped* lpOverlapped) -> bool
231236
static extern PInvoke.Kernel32.CreatePseudoConsole(PInvoke.COORD size, PInvoke.Kernel32.SafeObjectHandle hInput, PInvoke.Kernel32.SafeObjectHandle hOutput, PInvoke.Kernel32.CreatePseudoConsoleFlags dwFlags, out PInvoke.Kernel32.SafePseudoConsoleHandle phPC) -> PInvoke.HResult
232237
static extern PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, uint* lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle
233238
static extern PInvoke.Kernel32.CreateRemoteThreadEx(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, PInvoke.Kernel32.PROC_THREAD_ATTRIBUTE_LIST* lpAttributeList, uint* lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle

src/Kernel32/storebanned/Kernel32.Helpers.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,5 +382,16 @@ public static unsafe ValueTask<uint> DeviceIoControlAsync<TInput, TOutput>(
382382
}
383383
}
384384
}
385+
386+
/// <inheritdoc cref="GetOverlappedResult(SafeObjectHandle, OVERLAPPED*, out int, bool)"/>
387+
[NoFriendlyOverloads]
388+
public static unsafe bool GetOverlappedResult(
389+
SafeObjectHandle hFile,
390+
NativeOverlapped* lpOverlapped,
391+
out int lpNumberOfBytesTransferred,
392+
bool bWait)
393+
{
394+
return GetOverlappedResult(hFile, (OVERLAPPED*)lpOverlapped, out lpNumberOfBytesTransferred, bWait);
395+
}
385396
}
386397
}

0 commit comments

Comments
 (0)