How to call DeviceIOControl code asynchronously?

I am trying to call DeviceIO functions asynchronously using the OVERLAPPED structure as described in MSDN. I use the FSCTL_ENUM_USN_DATA control code to list NTFS MFT disks, but I cannot run it asynchronously. The file descriptor is created using FILE_FLAG_OVERLAPPED, but it makes no difference whether I use an overlapping structure with FILE_FLAG_OVERLAPPED or not. The function does not return immediately. It seems to be synchronous in both cases. The example below shows the listing of the first 100,000 MFT entries on drive C: \. Since I am not very familiar with the use of overlapping structures, I may have done something wrong. My question is: How can I execute DeviceIoControl (hDevice, FSCTL_ENUM_USN_DATA, ...) asynchronously? Thanks for any help.

#include "stdafx.h" #include <Windows.h> typedef struct { DWORDLONG nextusn; USN_RECORD FirstUsnRecord; BYTE Buffer[500]; }TDeviceIoControlOutputBuffer, *PTDeviceIoControlOutputBuffer; int _tmain(int argc, _TCHAR* argv[]) { MFT_ENUM_DATA lInputMftData; lInputMftData.StartFileReferenceNumber = 0; lInputMftData.MinMajorVersion = 2; lInputMftData.MaxMajorVersion = 3; lInputMftData.LowUsn = 0; lInputMftData.HighUsn = 0; TDeviceIoControlOutputBuffer lOutputMftData; DWORD lOutBytesReturned = 0; HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); OVERLAPPED lOverlapped = { 0 }; lOverlapped.hEvent = hEvent; LPCWSTR path = L"\\\\.\\C:"; HANDLE hDevice = CreateFile(path, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (hDevice != INVALID_HANDLE_VALUE) { lOutputMftData.nextusn = 0; while (lOutputMftData.nextusn < 100000) { lInputMftData.StartFileReferenceNumber = lOutputMftData.nextusn; BOOL result = DeviceIoControl(hDevice, FSCTL_ENUM_USN_DATA, &lInputMftData, sizeof(lInputMftData), &lOutputMftData, sizeof(lOutputMftData), &lOutBytesReturned, &lOverlapped); } } } 
+6
source share
1 answer
 HEVENT hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); OVERLAPPED lOverlapped = { 0 }; lOverlapped.hEvent = hEvent; ... BOOL result = DeviceIoControl(hDevice, FSCTL_ENUM_USN_DATA, &lInputMftData, sizeof(lInputMftData), &lOutputMftData, sizeof(lOutputMftData), &lOutBytesReturned, &lOverlapped); // If operation is asynchronous, wait for hEvent here or somewhere else 
0
source

Source: https://habr.com/ru/post/971923/


All Articles