Tuesday, November 11, 2008

Explorer Your Dll

String path=openFileDialog1.FileName;
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@path);
Type[] arrT = a.GetTypes();
listBox1.Items.Clear();
foreach (Type t in arrT)
{
listBox1.Items.Add("Namespace = " + t.Namespace);
listBox1.Items.Add("Class names = " + t.Name);


System.Reflection.MethodInfo[] arrMI = t.GetMethods();

System.Reflection.FieldInfo[] arrFi = t.GetFields();
System.Reflection.ConstructorInfo[] arrCi = t.GetConstructors();
foreach (System.Reflection.ConstructorInfo ci in arrCi)
{
listBox1.Items.Add("Contructor = " + ci.Name);
System.Reflection.ParameterInfo[] arrPI = ci.GetParameters();
foreach (System.Reflection.ParameterInfo pi in arrPI)
{
listBox1.Items.Add("Parameter = " + pi.Name);
}
}
foreach (System.Reflection.FieldInfo fi in arrFi)
{
listBox1.Items.Add("Field = " + fi.Name);
}
foreach (System.Reflection.MethodInfo mi in arrMI)
{
listBox1.Items.Add("Method = " + mi.Name);
System.Reflection.ParameterInfo[] arrPif = mi.GetParameters();

foreach (System.Reflection.ParameterInfo pif in arrPif)
{
listBox1.Items.Add("Parameter Name = " + pif.Name);
listBox1.Items.Add("Parameter DataType = " + pif.ParameterType.Name); 

}
}

}

Read More......

How to invoke your Dll using relative path

public Form1()
{
InitializeComponent();
Assembly a = Assembly.LoadFile(@"F:\by Ratno\Final Skripsi\UML Studio Project\NeutralNotation\bin\Debug\NeutralNotation.dll");
Module m = a.GetModule("NeutralNotation.dll");
Type typen = null;
foreach (Type t in m.GetTypes())
{
if (t.Name.Trim().Equals("Neutral"))
{
typen = t;
}
}

ConstructorInfo[] ci;
ci = typen.GetConstructors();
d=(AbstractDiagram.IDiagram) ci[0].Invoke(null);
toolStrip1.Items.AddRange(d.GetNotationCollection()); 

}

Read More......

Assembly Explorer

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;


namespace Engine.Asm
{
/// 


/// Class ini digunakan untuk melakukan penjelajahan ke
/// dalam dll
/// 

public class AssemblyExplorer
{

private string path;
private List lstBaseName;
protected Assembly asm;
protected Type[] arrType;

public string getName()
{
return asm.ManifestModule.Name;
}
/// 
/// Buat instant baru dari class AssemblyExplorer
/// 

/// path lokasi dll yang akan diload
public AssemblyExplorer(string path)
{
try
{
this.path = path;
asm = Assembly.LoadFile(path);
////System.Windows.Forms.MessageBox.Show(asm.ManifestModule.Name);
arrType = asm.GetTypes();
lstBaseName = new List();
foreach (Type t in arrType)

if (!lstBaseName.Contains(t.BaseType.Name))
lstBaseName.Add(t.BaseType.Name);
}
}
catch (ReflectionTypeLoadException e)
{
System.Windows.Forms.MessageBox.Show(e.Message,e.GetType().Name);
}
catch (BadImageFormatException e)
{
System.Windows.Forms.MessageBox.Show(e.Message, e.GetType().Name);
}
catch (NullReferenceException e)
{
System.Windows.Forms.MessageBox.Show(e.Message, e.GetType().Name);
}
}

/// 
/// Ambil daftar kelas induk dari semua kelas
/// yang terdapat pada dll ini
/// 

/// daftar kelas induk
public List getListBaseName()
{
return lstBaseName;
}

/// 
/// Cek, apakah suatu dll memiliki kelas 
/// yang memiliki suatu kelas induk
/// 

/// nama kelas induk
/// true bila memiliki dan false bila tidak 
/// memiliki

public bool containtBaseClass(string baseClass)
{
if (lstBaseName == null)
return false;
foreach (string s in lstBaseName)
{
if (s.Trim().Equals(baseClass))
return true;
}
return false;
}

/// 
/// Ambil suatu class berdasar nama
/// 

/// nama kelas yang dicari
/// null bila tidak ditemukan, 
/// atau mengembalikan kelas 
/// bila ditemukan

public Type getClass(string ClassName)
{
if (arrType == null)
return null;
foreach (Type t in arrType)
{
if (t.Name.Equals(ClassName))
return t;
}
return null;
}

~AssemblyExplorer()
{
path = null;
asm = null;
lstBaseName = null;
arrType = null;
}


}
}

Read More......

How to Invode Dll from C# 2005

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
namespace Engine.Asm
{
/// 


/// Class untuk melakukan invokasi atau pemicuan suatu dll
/// 

/// Jenis kembalian
public class AssemblyInvoke : AssemblyExplorer,IInvoke
{
private Type selected;
private ConstructorInfo[] arrCi;
private string name, extension,dir;
/// 
/// Constructor untuk menciptakan instace dari class ini
/// 

/// path dari dll yang akan diinvokasi
public AssemblyInvoke(string path)
: base(path)
{

FileInfo fif = new FileInfo(path);
name = fif.Name;
dir = fif.DirectoryName;
extension = fif.Extension;
}
/// 
/// Invokasi atau picu dll
/// 

/// Nama class yang akan diinvokasi
/// Hasil kembalian yang berupa class relative
public ResultType Invoke(string ClassNameToInvoke)
{
selected = getClass(ClassNameToInvoke);
arrCi = selected.GetConstructors();
return (ResultType)arrCi[0].Invoke(new object[] { dir,name,null});
}
}
}

Read More......

Monday, November 10, 2008

How to Invoke Jar File using Dynamic Path

This post describe how to invoke some jar file using dynamic path.

First Create ZipClassLoader
package Lib;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public  class ZipClassLoader extends ClassLoader {
    private  ZipFile file;



    public ZipClassLoader(String filename) throws IOException {
        this.file = new ZipFile(filename);
    }
    public ZipClassLoader(File File_Zip) throws IOException {
        this.file = new ZipFile(File_Zip);
    }
    public ZipEntry[] getListEntry(){
        Enumeration eZe = file.entries();
        ArrayList lstZe=new ArrayList();
        while (eZe.hasMoreElements()) {
            lstZe.add(eZe.nextElement());
        }
        ZipEntry[] arrZe=new ZipEntry[lstZe.size()];
        lstZe.toArray(arrZe);
        return arrZe;
    }
    @Override
    protected Class findClass(String name) throws ClassNotFoundException {
        ZipEntry entry = this.file.getEntry(name.replace('.', '/') + ".class");
        if (entry == null) {
            throw new ClassNotFoundException(name);
        }
        try {
            byte[] array = new byte[1024];
            InputStream in = this.file.getInputStream(entry);

            ByteArrayOutputStream out = new ByteArrayOutputStream(array.length);
            int length = in.read(array);
            while (length > 0) {
                out.write(array, 0, length);
                length = in.read(array);
            }
            return defineClass(name, out.toByteArray(), 0, out.size());
        }
        catch (IOException exception) {
            throw new ClassNotFoundException(name, exception);
        }
    }
    protected Class findClass(ZipEntry zipEntry) throws IOException {
        ZipEntry entry = zipEntry;
        byte[] array = new byte[1024];
//        ZipFile.
        InputStream in = this.file.getInputStream(entry);
        ByteArrayOutputStream out = new ByteArrayOutputStream(array.length);
        int length = in.read(array);
        while (length > 0) {
            out.write(array, 0, length);
            length = in.read(array);
        }
//        defi
        return defineClass(zipEntry.getName(), out.toByteArray(), 0, out.size());
    }
    
    
    @Override
    protected URL findResource(String name) {
        ZipEntry entry = this.file.getEntry(name);
        if (entry == null) {
            return null;
        }
        try {
            return new URL("jar:file:" + this.file.getName() + "!/" + entry.getName());
        }
        catch (MalformedURLException exception) {
            return null;
        }
    }

    @Override
    protected Enumeration findResources(final String name) {
        return new Enumeration() {
            private URL element = findResource(name);

            public boolean hasMoreElements() {
                return this.element != null;
            }

            public URL nextElement() {
                if (this.element != null) {
                    URL element = this.element;
                    this.element = null;
                    return element;
                }
                throw new NoSuchElementException();
            }
        };
    }
    
}

U can use ZipClassLoader by main class


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jarinvoker;

import Lib.ZipClassLoader;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;

/**
 *
 * @author Ratno Kustiawan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        // TODO code application logic here
        File f=new File("H:/Prepare For Modular System/JarToInvoke/dist/JarToInvoke.jar");
        ZipClassLoader z=new ZipClassLoader(f);
        System.out.println("-------------------------------------");
        System.out.println("List Entry");
        System.out.println("-------------------------------------");
        ZipEntry[] lstEntry=z.getListEntry();
        for (ZipEntry zipEntry : lstEntry) {
            System.out.println(zipEntry.getName());
        }
        System.out.println("-------------------------------------");
        System.out.println("Invoke Method");
        System.out.println("-------------------------------------");
        Class c=z.loadClass("proces.Target");
        c.newInstance();
    }

}


Read More......

How to Read Working Directory in Java

This posting is tutorial to get working directory from java application. Working directory means a directory where our java application running.



package readworkingdirektory;

import java.io.IOException;

/**
 *
 * @author Ratno Kustiawan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        System.out.println(System.getProperty("user.dir"));
        
    }

}

Read More......