c++windowswinapintfsdeviceiocontrol

DeviceIoControl GetLastError 87 (ERROR_INVALID_PARAMETER)


This code is perfectly fine when launched on Windows 7:

HANDLE hVol = CreateFile(L"\\\\.\\c:", GENERIC_WRITE | GENERIC_READ, 
                         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
// hVol is always successful (both of Win7 and 10), I double-checked
BYTE pData[sizeof(DWORDLONG) + 0x10000];
DWORD cb;
MFT_ENUM_DATA med;
med.StartFileReferenceNumber = 0;
med.LowUsn = 0;
med.HighUsn = MAXLONGLONG;
DeviceIoControl(hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL);
err = GetLastError();

but I get error 87 (ERROR_INVALID_PARAMETER) when running it on Windows 10. What could be the reason?


Solution

  • MFT_ENUM_DATA this is really typedef which expanded to MFT_ENUM_DATA_V0 or MFT_ENUM_DATA_V1 depend from NTDDI_VERSION :

    #if (NTDDI_VERSION >= NTDDI_WIN8)
    typedef MFT_ENUM_DATA_V1 MFT_ENUM_DATA, *PMFT_ENUM_DATA;
    #else
    typedef MFT_ENUM_DATA_V0 MFT_ENUM_DATA, *PMFT_ENUM_DATA;
    #endif
    

    obviously the in your case NTDDI_VERSION >= NTDDI_WIN8 and you use MFT_ENUM_DATA_V1.

    and you not initialize MaxMajorVersion which must be set to 2 or 3.

    so you need or add line med.MaxMajorVersion = 2; (or 3). or use MFT_ENUM_DATA_V0 med={};