Функция DeviceIoControlEx.


Win32 API ????????????? «Ex» ???????? ??????? ReadFile ? WriteFile, ? ?? ????? ??? «Ex» ???????? ??????? DeviceIoControl ?? ????????????. ????????? ???? ?????????? ????? ??????, ??? ??? ??????????????? ??????? Native API ??????????????? ? MSDN: NtDeviceIoControlFile (???? ? ???????? ??? «Deprecated»). ???????? ????? ??????? ????? ????????? ??? ???:

BOOL
WINAPI
DeviceIoControlEx(
    __in HANDLE hDevice,
    __in DWORD dwIoControlCode,
    __in_opt LPVOID lpInBuffer,
    __in DWORD nInBufferSize,
    __out_opt LPVOID lpOutBuffer,
    __in DWORD nOutBufferSize,
    __inout LPOVERLAPPED lpOverlapped,
    __in_opt LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
    );

??????? ?? DeviceIoControl ???: ?????????? ??????????? ???? ???????????? ? ????????? OVERLAPPED ? ???? ??????????? ???????? ????????? ?? completion callback (???????? ?? ??? ???????????).

NtDeviceIoControlFile ????? ?????????? ??? ???:

PIO_STATUS_BLOCK IoStatusBlock = (PIO_STATUS_BLOCK)&lpOverlapped->Internal;
IoStatusBlock->Status = STATUS_PENDING;

NTSTATUS Status =
    NtDeviceIoControlFile(
        hDevice,
        NULL,
        ApcRoutine,
        lpCompletionRoutine,
        IoStatusBlock,
        dwIoControlCode,
        lpInBuffer,
        nInBufferSize,
        lpOutBuffer,
        nOutBufferSize);

if (!NT_SUCCESS(Status))
{
    SetLastError(RtlNtStatusToDosError(Status));
    return FALSE;
}

return TRUE;

????? ????????? OVERLAPPED ???????????? ??? IO_STATUS_BLOCK. ????? ????? ????????? ReadFileEx ? WriteFileEx. NTSTATUS ???? ?????? ????????????? ? Win32 ??????? ???????? RtlNtStatusToDosError, ??????? ????? ??????????????? ? MSDN. ????????? callback-?? Win32 ? Native API ??????????, ??????? ???????????? ??????????????? ??????? ApcRoutine:

VOID
WINAPI
ApcRoutine(
    __in PVOID Context,
    __in PIO_STATUS_BLOCK IoStatusBlock,
    __reserved DWORD Reserved
    )
{
    DWORD BytesTransfered = 0;

    if (NT_SUCCESS(IoStatusBlock->Status))
    {
        BytesTransfered = (DWORD)IoStatusBlock->Information;
    }

    ((LPOVERLAPPED_COMPLETION_ROUTINE)Context)(
        RtlNtStatusToDosError(IoStatusBlock->Status),
        BytesTransfered,
        CONTAINING_RECORD(IoStatusBlock, OVERLAPPED, Internal));
}

? ?????????? ??????? DeviceIoControlEx ???????? ????? ????? ?? ?????????? ??????, ??? ? ReadFileEx, ? WriteFileEx.

Cross-posted from blog.not-a-kernel-guy.com.


Skip to main content