#include #include VOID PrintHex(PBYTE Data, ULONG dwBytes) { for (ULONG i = 0; i < dwBytes; i += 16) { printf("%.8x: ", i); for (ULONG j = 0; j < 16; j++) { if (i + j < dwBytes) { printf("%.2x ", Data[i + j]); } else { printf("?? "); } } for (ULONG j = 0; j < 16; j++) { if (i + j < dwBytes && Data[i + j] >= 0x20 && Data[i + j] <= 0x7e) { printf("%c", Data[i + j]); } else { printf("."); } } printf("\n"); } } DWORD ThreadRoutine(LPVOID lpParameter) { // Wait for the main thread to call ReadDirectoryChanges(). Sleep(500); // Perform a filesystem operation which results in two records (FILE_ACTION_RENAMED_OLD_NAME and FILE_ACTION_RENAMED_NEW_NAME) // being returned at once. MoveFile(L"a\\b", L"a\\c"); return 0; } int main() { // Create a local "a" directory. if (!CreateDirectory(L"a", NULL)) { printf("CreateDirectory failed, %d\n", GetLastError()); return 1; } // Open the newly created directory. HANDLE hDirectory = CreateFile(L"a", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (hDirectory == INVALID_HANDLE_VALUE) { printf("CreateFile(a) failed, %d\n", GetLastError()); RemoveDirectory(L"a"); return 1; } // Create a new file in the "a" directory. HANDLE hNewFile = CreateFile(L"a\\b", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (hNewFile != INVALID_HANDLE_VALUE) { CloseHandle(hNewFile); } // Create a new thread which will modify the directory (rename a file within). CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadRoutine, NULL, 0, NULL); // Wait for changes in the directory and read them into a 1024-byte buffer. BYTE buffer[1024] = { /* zero padding */ }; DWORD BytesRead = 0; if (!ReadDirectoryChangesW(hDirectory, buffer, sizeof(buffer), FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, &BytesRead, NULL, NULL)) { printf("ReadDirectoryChanges failed, %d\n", GetLastError()); DeleteFile(L"a\\c"); RemoveDirectory(L"a"); return 1; } // Dump the output on screen. PrintHex(buffer, BytesRead); // Free resources. DeleteFile(L"a\\c"); RemoveDirectory(L"a"); return 0; }