Load the Assembly
The first step in a rewriting application is usually to load the assembly from storage. PeToPe also loads the associated PDB, if it is present.
Load the Assembly
To load an assembly:
- Call the host’s LoadUnitFrom method and pass it a string that defines the location.
loads the assembly and returns the contents as a CCI Metadata object.
In general, LoadUnitFrom
can load assemblies from any type of storage. Assemblies are usually stored as files, but they could be stored in a database, on a web site, and so on. The location is simply a string that the host can use to load the assembly.
Typically, a location string is a file path, but it could be something like a SQL query string or a URL, depending on how the assembly is stored.
PeToPe operates on PE files stored on the hard drive and uses DefaultHost
as its host object.
loads assemblies or modules from the hard drive, and expects the location string to be a file path. For other types of storage, you must implement a custom host. See
The following example shows how PeToPe loads a PE file.
MetadataReaderHost host = new PeReader.DefaultHost();
var module = host.LoadUnitFrom(args) as IModule;
if (module == null)
Console.WriteLine(args+" is not a PE file containing a CLR module or assembly.");
takes the path of the input PE file which, in this example, is a command-line argument. It returns an immutable CCI Metadata object that represents the assembly in the CCI Metadata environment. Immutable objects are read-only, and are always
represented by an interface such as IModule
. The module
object is represented by its
interface, which can represent both assemblies and modules. For more discussion, see
Mutable and Immutable Representations
Load the PDB File
An assembly is often accompanied by a PDB file, which you can represent in the CCI environment by loading the file into a
To load the PDB file:
- Create a Stream object for the PDB file, and pass the object to the
PeToPe assumes that the PDB file is located in the same folder as the PE file and shares the same name.
stores the assembly’s location string. In this case, the assembly is a file on the hard drive and
contains a file path.
The following example shows how PeToPe loads the PDB file.
PdbReader/*?*/ pdbReader = null;
string pdbFile = Path.ChangeExtension(module.Location, "pdb");
Stream pdbStream = File.OpenRead(pdbFile);
pdbReader = new PdbReader(pdbStream, host);
PeToPe creates a PDB file name from Module.Location
, and uses the File.OpenRead
method to open the file and represent it as a
object. If the file exists, PeToPe uses the object to initialize a new
Next: Write the Results to Storage
Return to Beginning