HRESULT MiniThing::RecordUsn(VOID){
MFT_ENUM_DATA med ={0,0, m_usnInfo.NextUsn };
med.MaxMajorVersion =2;// Used to record usn info, must big enoughchar buffer[0x1000];
DWORD usnDataSize =0;
PUSN_RECORD pUsnRecord;// Find the first USN record// return a USN followed by zero or more change journal records, each in a USN_RECORD structurewhile(FALSE !=DeviceIoControl(m_hVol,
FSCTL_ENUM_USN_DATA,&med,sizeof(med),
buffer,_countof(buffer),&usnDataSize,NULL)){
DWORD dwRetBytes = usnDataSize -sizeof(USN);
pUsnRecord =(PUSN_RECORD)(((PCHAR)buffer)+sizeof(USN));
DWORD cnt =0;while(dwRetBytes >0){// Here FileNameLength may count in bytes, and each wchar_t occupy 2 byteswchar_t* pWchar =newwchar_t[pUsnRecord->FileNameLength /2+1];memcpy(pWchar, pUsnRecord->FileName, pUsnRecord->FileNameLength);
pWchar[pUsnRecord->FileNameLength /2]=0x00;// wcsncpy_s(pWchar, pUsnRecord->FileNameLength / 2, pUsnRecord->FileName, pUsnRecord->FileNameLength / 2);
std::wstring fileNameWstr =WcharToWstring(pWchar);delete pWchar;
UsnInfo usnInfo ={0};
usnInfo.fileNameWstr = fileNameWstr;
usnInfo.pParentRef = pUsnRecord->ParentFileReferenceNumber;
usnInfo.pSelfRef = pUsnRecord->FileReferenceNumber;
usnInfo.timeStamp = pUsnRecord->TimeStamp;
m_usnRecordMap[usnInfo.pSelfRef]= usnInfo;// Get the next USN record
DWORD recordLen = pUsnRecord->RecordLength;
dwRetBytes -= recordLen;
pUsnRecord =(PUSN_RECORD)(((PCHAR)pUsnRecord)+ recordLen);}// Get next page USN record// from MSDN(http://msdn.microsoft.com/en-us/library/aa365736%28v=VS.85%29.aspx ): // The USN returned as the first item in the output buffer is the USN of the next record number to be retrieved. // Use this value to continue reading records from the end boundary forward.
med.StartFileReferenceNumber =*(USN*)&buffer;}return S_OK;}