Jude Aloysius
Jude Aloysius

Reputation: 141

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?

Upvotes: 1

Views: 1861

Answers (2)

Jude Aloysius
Jude Aloysius

Reputation: 141

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.

Upvotes: 3

RbMm
RbMm

Reputation: 33706

No way. A HANDLE to a file is really an indirect pointer to a FILE_OBJECT, and if the file is open in synchronous mode (FileObject->Flags & FO_SYNCHRONOUS_IO) then FILE_OBJECT.CurrentByteOffset is used as the file pointer offset. You share the same FILE_OBJECT when you duplicate the handle. In order to have an independent file pointer, you need to open another independent FILE_OBJECT/HANDLE to the file.

Upvotes: 2

Related Questions