Jude Aloysius Jude Aloysius - 12 days ago 4
C++ Question

Duplicate Handle to Read file

I have a file which is exclusively opened by a Process A (a third party application). I obviously couldn't open the file in my application, so I thought of Duplicating the Handle using DuplicateHandle API.

However, I realized that the change in position of the file handle will also be reflected in Process A as well , which could cause undesired behaviour. Is there a way I can duplicate the file handle for reading, without affecting the file Handle (position) in Process A?

Answer

I finally figured it out:

  1. First, you need to create a Duplicate file handle using NtQuerySystemInformation and DuplicateHandle API.

  2. Using the duplicate handle now CreateFileMapping handle and followed by MapViewOfFile . MapViewOfFile maps certain portion of the file to the your process address space and can be used to read the file without disturbing the current file offset of the of the original file handle.

Hope this helps someone.

Comments