0

I am trying to enumerate the MFT to get list of file names.

This code works when used in a Win32 project:

MFT_ENUM_DATA med;
med.StartFileReferenceNumber = 0;
med.LowUsn = 0;
med.HighUsn = ujd.NextUsn;
BYTE pData[sizeof(DWORDLONG) + 0x10000];
DWORD cb;
while (DeviceIoControl(handleToVolume, FSCTL_ENUM_USN_DATA, &med, sizeof(med),
  pData, sizeof(pData), &cb, NULL) != FALSE) {
 //do the processing
}

But when I use the same code in C++/CLI, DeviceIoControl gives false and GetLastError gives ERROR_INVALID_FUNCTION. When the same code is translated into C#, it works.

Any idea what is happening?

max_force
  • 769
  • 6
  • 12
  • You should beware of `GetLastError` in managed code. You're supposed to put the right flag in your DllImport attribute, and use `Marshal::GetLastWin32Error` instead... Also, even though it isn't supposed to have an influence, you should initialize `cb` to zero before the call. – Medinoc Jul 01 '13 at 12:09
  • I am using unmanaged `DeviceIoControl`. I tried `Marshal::GetLastWin32Error` too and it gives the same value 1 as `GetLastError`. Also initialized `cb`, but no good. I am now wondering if this is caused by some project setting or properties... – max_force Jul 02 '13 at 05:13

1 Answers1

0

check if you have this define:

#define FSCTL_ENUM_USN_DATA 
 CTL_CODE(FILE_DEVICE_FILE_SYSTEM,44,METHOD_NEITHER,FILE_READ_DATA)

and replace FILE_READ_DATA with FILE_ANY_ACCESS

Alexan
  • 8,165
  • 14
  • 74
  • 101
ciprian
  • 1
  • 1