This project is read-only.

Load the Assembly

CCI Metadata

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.
LoadUnitFromloads 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. DefaultHost.LoadUnitFrom 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 Hosts for details.

The following example shows how PeToPe loads a PE file.
MetadataReaderHost host = new PeReader.DefaultHost();
var module = host.LoadUnitFrom(args[0]) as IModule;
if (module == null)
  Console.WriteLine(args[0]+" is not a PE file containing a CLR module or assembly.");

LoadUnitFrom 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 IModule 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 PdbReader object.

To load the PDB file:
  • Create a Stream object for the PDB file, and pass the object to the PdbReader constructor.
PeToPe assumes that the PDB file is located in the same folder as the PE file and shares the same name. Module.Location stores the assembly’s location string. In this case, the assembly is a file on the hard drive and Location contains a file path.

The following example shows how PeToPe loads the PDB file.
PdbReader/*?*/ pdbReader = null;
string pdbFile = Path.ChangeExtension(module.Location, "pdb");
if (File.Exists(pdbFile))
  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 Stream object. If the file exists, PeToPe uses the object to initialize a new PdbReader object.

Next: Write the Results to Storage
Return to Beginning

Last edited Jan 12, 2010 at 4:56 PM by Guy_Smith, version 2


No comments yet.