import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
import com.ms.com.*;
//import danim.*;

/**
 * This class can take a variable number of parameters on the command
 * line. Program execution begins with the main() method. The class
 * constructor is not invoked unless an object of type 'Form1' is
 * created in the main() method.
 */
public class Form1 extends Form
{

/*
This method refreshes the listbox display of the running 
object table.
*/
	
	private void RefreshList ( ) 
	{
/*
Clear list box.
*/

		listBox1.removeAll( ) ;

/*
Get the current running object table from the 
operating system.
*/
		
		IRunningObjectTable rot = GetRunningObjectTable (0) ;
		
/*
Call the method IRunningObjectTable::EnumRunning to get an
IEnumMoniker interface that we will use to enumerate the names
of all the entries in it.

Since this interface is returned by an output pointer, we need
to pass a single-element array to receive it.
*/
		IEnumMoniker enum [] = new IEnumMoniker [1] ;
		rot.EnumRunning(enum) ;

		int fetched [] = new int [1] ;
		IMoniker moniker [] = new IMoniker [1] ;
		
		while (true)
		{
/*
Get next moniker in the table.
If we've gotten them all, break out of the loop.
*/
			
			enum [0].RemoteNext (1, moniker, fetched) ;
	
			if (fetched[0] != 1)
			{
				break ;
			}
			
/*
Create a bind context object for the moniker to use while
composing its human-readable display name.
*/
			String str [] = new String [1] ;
			IBindCtx ctx = CreateBindCtx (0) ;
			moniker[0].GetDisplayName (ctx, null, str) ;
		
/*
Add the name to the list box.
*/
			
			listBox1.addItem (str[0]) ;
		}
	}

	public Form1()
	{
		// Required for Visual J++ Form Designer support
		initForm();		

		RefreshList ( ) ;
	}

	/**
	 * Form1 overrides dispose so it can clean up the
	 * component list.
	 */
	public void dispose()
	{
		super.dispose();
		components.dispose();
	}

	private void button1_click(Object source, Event e)
	{
			RefreshList ( ) ;
	}

	/**
	 * NOTE: The following code is required by the Visual J++ form
	 * designer.  It can be modified using the form editor.  Do not
	 * modify it using the code editor.
	 */
	Container components = new Container();
	ListBox listBox1 = new ListBox();
	Button button1 = new Button();

	private void initForm()
	{
		this.setText("Essence of COM Java Running Object Table Viewer");
		this.setAutoScaleBaseSize(new Point(5, 13));
		this.setClientSize(new Point(379, 278));

		listBox1.setLocation(new Point(32, 24));
		listBox1.setSize(new Point(312, 173));
		listBox1.setTabIndex(0);
		listBox1.setText("listBox1");
		listBox1.setUseTabStops(true);

		button1.setLocation(new Point(32, 224));
		button1.setSize(new Point(120, 32));
		button1.setTabIndex(1);
		button1.setText("Refresh");
		button1.addOnClick(new EventHandler(this.button1_click));

		this.setNewControls(new Control[] {
							button1, 
							listBox1});
	}

	/**
	 * The main entry point for the application. 
	 *
	 * @param args Array of parameters passed to the application
	 * via the command line.
	 */
	public static void main(String args[])
	{
		Application.run(new Form1());
	}

	/**
	 * @dll.import("OLE32", ole) 
	 */
	public static native IBindCtx CreateBindCtx (int i) ;

	/**
	 * @dll.import("OLE32", ole) 
	 */
	public static native IRunningObjectTable GetRunningObjectTable (int i) ;

}
