Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the underlying file handle of a Stream?

Tags:

c#

.net

stream

seek

I have an application that works with physical memory snapshots (for example, VMware VMEM files). Among other things, it can read processes/modules out of the snapshot by virtual rather than physical address. This involves reconstructing the module 4KB at a time through the page table, which, in turn, means a lot of calls to the Stream's Seek() method.

For reasons I'm not sure of, these calls to Seek() bog things down dramatically. As a result, I'm looking for a way around them -- or at least, a way around the managed Seek() implementation. My best guess is to PInvoke SetFilePointer and work with that directly, but in order to do that I need to get the IntPtr/SafeFileHandle for the Stream. I have a few restrictions:

  1. The API I'm working with is limited to .NET 3.5, so unfortunately MemoryMappedFile isn't an option.

  2. I can't use a FileStream (which already has a private SafeFileHandle field that can be accessed with reflection) or PInvoke CreateFile() to get at the snapshot another way -- the API includes a BinaryReader that has an exclusive lock on the snapshot.

Of course, unlike FileStream, neither BinaryReader nor its underlying Stream have a reference to the file handle. But certainly one must exist? In which case, how do I go about acquiring it?

like image 497
Wayside Avatar asked Dec 27 '11 02:12

Wayside


People also ask

What is a file handle C#?

File Handling involves management of files. Files are a collection of data that is stored in a disk with a name and a directory path. Files contain input as well as output streams that are used for reading and writing data respectively.

How do I use FileStream?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

What is the difference between file and stream?

File descriptors are represented as objects of type int , while streams are represented as FILE * objects. File descriptors provide a primitive, low-level interface to input and output operations.


2 Answers

There's no file handle for Stream because it's an abstract class. A class implementing Stream may or may not use a file handle -- FileStream does, since it reads data from a file, but MemoryStream, for example, does not.

To get the underlying file handle (in this case a SafeFileHandle) of a BinaryReader whose Stream is a FileStream, use reflection to access private SafeFileHandle _handle, like so:

SafeFileHandle sfh = (SafeFileHandle)typeof(FileStream).GetField("_handle", BindingFlags.NonPublic | BindingFlags.Instance).GetValue((FileStream)YOUR_BINARY_READER.BaseStream)

On a side note: neither direct calls to SetFilePointer() nor MemoryMappedFile helped in this case. It seems there's no fast way to handle random disk access at the volume I'm using it (millions of consecutive calls).

like image 89
Wayside Avatar answered Sep 25 '22 11:09

Wayside


Since you have BinaryReader over FileStream you can access the BaseStream of the reader, cast that to FileStream and then use its public SafeFileHandle property to access the handle. Something like this:

FileStream stream = (FileStream)(reader.BaseStream);
//use stream.SafeFileHandle
like image 28
sharptooth Avatar answered Sep 22 '22 11:09

sharptooth