PE-Viewer Project

From no name for this wiki
Revision as of 09:44, 17 July 2008 by Claude (talk | contribs) (Print dependent assemblies)
Jump to: navigation, search

The PE-Viewer project is about a java library to read .net assembly files. Documentation about that library can be found in this article. The project itself is hosted at http://pe-file-reader.dev.java.net. Go there to check out the source code.

Installation

Java Web Start

The user interface can be started via JavaWebStart. Click JNLP File.

Starting from jar

You can download the jar here: http://pe-file-reader.dev.java.net/files/documents/8202/80983/pe-file-reader.jar. The command is java -jar pe-file-reader.jar. The main class to start the gui is: org.dotnetjava.tools.viewer.MainFrame


Getting the source

See CVS in http://pe-file-reader.dev.java.net


Code samples

List Types and Methods

The following code reads the assembly sudokusolver.exe and lists all types and methods within that assembly to the console.

import java.io.IOException;
import java.util.Collection;
import org.dotnetjava.AssemblyFactory;
import org.dotnetjava.AssemblyFile;
import org.dotnetjava.DotNetMethodDef;
import org.dotnetjava.DotNetModule;
import org.dotnetjava.DotNetTypeDef;
import org.dotnetjava.file.InvalidPEFileException;
public class ListTypesSample {    
   /** Creates a new instance of ListTypesSample */
   public ListTypesSample() {
   }    
   public static void main(String[] args) {
       try {
           AssemblyFile assemblyFile = AssemblyFactory.readPeFile("sudokusolver.exe");            
           if(assemblyFile.isAssembly()){
               System.out.println("This is an assembly, not only a module");
           }                        
           DotNetModule module = assemblyFile.getDotNetMainModule();
           Collection<DotNetTypeDef> types = module.getTypeDefs();
           for(DotNetTypeDef type : types){                
               String name = type.getName();
               Collection<DotNetMethodDef> methods = type.getMethods();                
               System.out.println("TypeName: " + name);
               for(DotNetMethodDef method: methods){
                   String methodName = method.getName();
                   System.out.println("  MethodName: " + methodName);
               }                                
           }
           
       } catch (InvalidPEFileException ex) {
           ex.printStackTrace();
       } catch (IOException ex) {
           ex.printStackTrace();
       }
   } 
}

Print dependent assemblies

Assemblies depend on other assemblies. This sample prints the name of dependent assemblies:

      try {
          AssemblyFile assemblyFile = AssemblyFactory.readPeFile("SilverlightApplication1.dll");                                    
          DotNetModule module = assemblyFile.getDotNetMainModule();        
          for(DotNetAssemblyRef ar : module.getAssemblyRefs()){
               String name = ar.getName();               
               System.out.println("name:" + name );
           }
     } catch (InvalidPEFileException ex) {
          ex.printStackTrace();
      } catch (IOException ex) {
          ex.printStackTrace();
      }