Home > Enterprise >  What is the working mechanism under the hood for ReadDirectoryChangesW()?
What is the working mechanism under the hood for ReadDirectoryChangesW()?

Time:10-13

I understand ReadDirectoryChangesW can be implemented synchronously or asynchronously. The general consideration being asynchronous is good for slow I/O operations, vice-versa.

Currently, I am trying to do monitoring and get the paths of all new files created on the drive. This would involve watching many subdirectories and I am unsure what exactly are the operations carried out in this function and if asynchronous is good for this use case.

It would be good if there's somewhere to read about its working mechanism. I have read the documentation but it only states how to use it.

Thanks.

CodePudding user response:

ReadDirectoryChanges is implemented by issuing IRP_MJ_DIRECTORY_CONTROL to the appropriate file system driver, with a minor function code of IRP_MN_NOTIFY_CHANGE_DIRECTORY.

At the file system level the implementation will likely always be asynchronous unless change data is already available to return. The general synchronous case is likely instead handled by ReadDirectoryChangesW, waiting for the dispatched I/O request packet to complete on behalf of the user.

Whether you choose to call ReadDirectoryChangesW synchronously or asynchronously should depend on whether you have any useful work you could be doing while waiting for a response. If no then there is no point to dealing with the trouble asynchronous I/O presents. If yes (even if just updating UI) then the asynchronous route will very likely be preferable. It might also be useful to instead spawn a worker thread and have that thread issue the call, very often mixing asynchronous I/O code with UI becomes a tangled mess. A separate worker thread can allow each to deal with just one or the other.

  • Related