Setting NTFS “dirty bit” without using fsutil

Back in April, a USENET visitor from outer space asked if the "dirty bit" on his NTFS volumes of choice could be set, without having to shell out to the fsutil process. Ok, a simple API look up right? BZZT. Wrong. There is no documented way to do this.

I disassembled the fsutil tool and discovered it sends an undocumented (until now) control code to the volume driver via DeviceIoControl. I named the control code FSCTL_SET_DIRTY_BIT, which has a hexadecimal value of 9003.

Here's some quickly-generated sample code:

/* Undocumented volume management control code */
#define FSCTL_SET_DIRTY_BIT 0x90030

DWORD bytesReturned = 0x00;
HANDLE hVol = CreateFile(
  _T("\\\\\\.\\I:"),
  GENERIC_WRITE
  FILE_SHARE_READ | FILE_SHARE_WRITE,
  0x00,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  0x00);

BOOL bitSet = DeviceIoControl(
  hVol,
  FSCTL_SET_DIRTY_BIT,
  0x00,
  0x00,
  0x00,
  0x00,
  &bytesReturned,
  0);

if( bitSet )
   printf( _T("dirty bit set.n") );