OLE and ActiveX example snippet : WIN32 « SWT JFace Eclipse « Java






OLE and ActiveX example snippet

/*
 * Copyright (c) 2000, 2003 IBM Corp. All rights reserved. This file is made
 * available under the terms of the Common Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

/*
 * OLE and ActiveX example snippet: browse the typelibinfo for a program id
 * 
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/i.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.ole.win32.TYPEATTR;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleFunctionDescription;
import org.eclipse.swt.ole.win32.OlePropertyDescription;
import org.eclipse.swt.widgets.Shell;

public class Main {

  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("Usage: java Main <program id>");
      return;
    }

    String progID = args[0];

    Shell shell = new Shell();

    OleFrame frame = new OleFrame(shell, SWT.NONE);
    OleControlSite site = null;
    OleAutomation auto = null;
    try {
      site = new OleControlSite(frame, SWT.NONE, progID);
      auto = new OleAutomation(site);
    } catch (SWTException ex) {
      System.out.println("Unable to open type library for " + progID);
      return;
    }

    TYPEATTR typeattr = auto.getTypeInfoAttributes();
    if (typeattr != null) {
      if (typeattr.cFuncs > 0)
        System.out.println("Functions for " + progID + " :\n");
      for (int i = 0; i < typeattr.cFuncs; i++) {
        OleFunctionDescription data = auto.getFunctionDescription(i);
        String argList = "";
        int firstOptionalArgIndex =
          data.args.length - data.optionalArgCount;
        for (int j = 0; j < data.args.length; j++) {
          argList += "[";
          if (j >= firstOptionalArgIndex)
            argList += "optional, ";
          argList += getDirection(data.args[j].flags)
            + "] "
            + getTypeName(data.args[j].type)
            + " "
            + data.args[j].name;
          if (j < data.args.length - 1)
            argList += ", ";
        }
        System.out.println(
          getInvokeKind(data.invokeKind)
            + " (id = "
            + data.id
            + ") : "
            + "\n\tSignature   : "
            + getTypeName(data.returnType)
            + " "
            + data.name
            + "("
            + argList
            + ")"
            + "\n\tDescription : "
            + data.documentation
            + "\n\tHelp File   : "
            + data.helpFile
            + "\n");
      }

      if (typeattr.cVars > 0)
        System.out.println("\n\nVariables for " + progID + " :\n");
      for (int i = 0; i < typeattr.cVars; i++) {
        OlePropertyDescription data = auto.getPropertyDescription(i);
        System.out.println(
          "PROPERTY (id = "
            + data.id
            + ") :"
            + "\n\tName : "
            + data.name
            + "\n\tType : "
            + getTypeName(data.type)
            + "\n");
      }
    }

    auto.dispose();
    shell.dispose();
  }
  
  private static String getTypeName(int type) {
    switch (type) {
      case OLE.VT_BOOL :
        return "boolean";
      case OLE.VT_R4 :
        return "float";
      case OLE.VT_R8 :
        return "double";
      case OLE.VT_I4 :
        return "int";
      case OLE.VT_DISPATCH :
        return "IDispatch";
      case OLE.VT_UNKNOWN :
        return "IUnknown";
      case OLE.VT_I2 :
        return "short";
      case OLE.VT_BSTR :
        return "String";
      case OLE.VT_VARIANT :
        return "Variant";
      case OLE.VT_CY :
        return "Currency";
      case OLE.VT_DATE :
        return "Date";
      case OLE.VT_UI1 :
        return "unsigned char";
      case OLE.VT_UI4 :
        return "unsigned int";
      case OLE.VT_USERDEFINED :
        return "UserDefined";
      case OLE.VT_HRESULT :
        return "int";
      case OLE.VT_VOID :
        return "void";

      case OLE.VT_BYREF | OLE.VT_BOOL :
        return "boolean *";
      case OLE.VT_BYREF | OLE.VT_R4 :
        return "float *";
      case OLE.VT_BYREF | OLE.VT_R8 :
        return "double *";
      case OLE.VT_BYREF | OLE.VT_I4 :
        return "int *";
      case OLE.VT_BYREF | OLE.VT_DISPATCH :
        return "IDispatch *";
      case OLE.VT_BYREF | OLE.VT_UNKNOWN :
        return "IUnknown *";
      case OLE.VT_BYREF | OLE.VT_I2 :
        return "short *";
      case OLE.VT_BYREF | OLE.VT_BSTR :
        return "String *";
      case OLE.VT_BYREF | OLE.VT_VARIANT :
        return "Variant *";
      case OLE.VT_BYREF | OLE.VT_CY :
        return "Currency *";
      case OLE.VT_BYREF | OLE.VT_DATE :
        return "Date *";
      case OLE.VT_BYREF | OLE.VT_UI1 :
        return "unsigned char *";
      case OLE.VT_BYREF | OLE.VT_UI4 :
        return "unsigned int *";
      case OLE.VT_BYREF | OLE.VT_USERDEFINED :
        return "UserDefined *";
    }
    return "unknown " + type;
  }
  
  private static String getDirection(int direction) {
    String dirString = "";
    boolean comma = false;
    if ((direction & OLE.IDLFLAG_FIN) != 0) {
      dirString += "in";
      comma = true;
    }
    if ((direction & OLE.IDLFLAG_FOUT) != 0) {
      if (comma)
        dirString += ", ";
      dirString += "out";
      comma = true;
    }
    if ((direction & OLE.IDLFLAG_FLCID) != 0) {
      if (comma)
        dirString += ", ";
      dirString += "lcid";
      comma = true;
    }
    if ((direction & OLE.IDLFLAG_FRETVAL) != 0) {
      if (comma)
        dirString += ", ";
      dirString += "retval";
    }

    return dirString;
  }
  
  private static String getInvokeKind(int invKind) {
    switch (invKind) {
      case OLE.INVOKE_FUNC :
        return "METHOD";
      case OLE.INVOKE_PROPERTYGET :
        return "PROPERTY GET";
      case OLE.INVOKE_PROPERTYPUT :
        return "PROPERTY PUT";
      case OLE.INVOKE_PROPERTYPUTREF :
        return "PROPERTY PUT BY REF";
    }
    return "unknown " + invKind;
  }
}



           
       








Related examples in the same category

1.Invoke the system text editor on autoexec.bat
2.Place an icon with a popup menu on the system trayPlace an icon with a popup menu on the system tray
3.SWT Ole FrameSWT Ole Frame
4.Word OLEWord OLE
5.Invoke an external batch fileInvoke an external batch file
6.Find the icon of the program that edits .bmp filesFind the icon of the program that edits .bmp files
7.Running a script within IE.Running a script within IE.
8.Reading and writing to a SAFEARRAY
9.Embed Word in an applet
10.OLE and ActiveX example: browse the typelibinfo for a program id
11.OLE and ActiveX example: get events from IE controlOLE and ActiveX example: get events from IE control
12.How to access About, Preferences and Quit menus on carbon