Reading and Writing PE and PDB Files

CCI applications typically start by reading a PE file and perhaps the associated PDB file from storage. Rewriting applications write the modified PE file back to storage. CCI provides objects that simplify the reading and writing process for both file types.

For an example of how to read and write PE and PDB files, see the PeToPe Sample Walkthrough.

Reading Files

The PeReader object provides a fast memory-efficient way to read PE files incrementally . This is an ideal way for compilers written in managed code to import metadata, because it involves no unmanaged code and no interop overhead. Applications that load PE files from the file system typically use PeReader indirectly, by calling DefaultHost.LoadUnitFrom, which opens the file, passes the contents to OpenModule or OpenAssembly, and returns the appropriate interface.

To load a PDB file, create a new PdbReader object and pass the constructor a stream representing the file’s contents. PdbReader constructs an object model to represent the PDB file from the contents of the stream. Unlike PeReader, PdbReader does not return an interface to an object model. Instead, the PdbReader object itself represents the PDB file.

Writing Files

The PeWriter object provides a fast and memory-efficient way to write PE files to a stream, typically a file stream. Unlike components such as System.Reflection.Emit, PeWriter can write any object model that implements the CCI interfaces. You do not need a compiler to construct objects. Like PeReader, PeWriter uses only managed code to write PE streams.

To write a PdbReader object model to a stream, create a PdbWriter object and pass it to PdbWriter.WritePeToStream. PeWriter interacts with PdbWriter to produce an output stream. At the moment, PdbWriter is a managed wrapper for an unmanaged COM component, so writing PDB files does use some unmanaged code.

Next: CCI and Code
Return to Beginning

Last edited Jan 21, 2010 at 5:22 PM by Guy_Smith, version 1


No comments yet.