Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //@(#) $Id: 1156 2004-03-11 10:06:26Z S1462 $
  package org.eclipse.swt.ole.win32;
  import  org.eclipse.swt.internal.ole.win32.COM;
  import  org.eclipse.swt.internal.ole.win32.GUID;
  import  org.eclipse.swt.internal.ole.win32.IDispatch;
  import  org.eclipse.swt.internal.ole.win32.IUnknown;

AutomationHandler provides a mechanism for accessing functionality that is specific to a COM Object .

The COM Object must support the IDispatch interface in order to provide OleAutomation support.

Here is a sample IDL fragment:

  interface IBerechtigungInteractive : IDispatch
    [id(DISPID_VALUE), propget, helpstring("property UserName")]
       HRESULT UserName([out, retval] BSTR *pVal);

    [id(1), helpstring("method Create")]
       HRESULT Create([in] BSTR Profile, [in, defaultvalue(0)] long Wnd, [in, defaultvalue(0)] int Flags);

An example of how to interact with AutomationHandler is shown below:

  AutomationHandler alfaBer = new AutomationHandler("SD_BER.BerechtigungInteractive");

 // Look up the ID of the UserName property
   int[] rgdispid = alfaBer.getAutomationObject().getIDsOfNames(new String[]{"UserName"});
   int dispIdMember = rgdispid[0];
  // Get the  value of the UserName parameter:
   Variant pVarResult = alfaBer.getAutomationObject().getProperty(dispIdMember);

  if (pVarResult != null && pVarResult.getType() == OLE.VT_BSTR) {
    System.out.println("UserName is " + pVarResult.getString());

  // Invoke the Create method
  // Look up the IDs of the Create method and its parameter
   int[] rgdispid = alfaBer.getAutomationObject().getIDsOfNames(new String[]{"Create", "Profile"});
   int dispIdMember = rgdispid[0];

   // Convert arguments to Variant objects
       Variant[] rgvarg = new Variant[1];
       rgvarg[0] = new Variant("KRE");
       int[] rgdispidNamedArgs = new int[1];
       rgdispidNamedArgs[0] = rgdispid[1]; // identifier of argument
 // Call the method
      Variant pVarResult = alfaBer.getAutomationObject().invoke(dispIdMember, rgvarg, rgdispidNamedArgs);

 // Check the return value
  if (pVarResult == null ){
    System.out.println("Failed to call method ");
 // Dispose the automation object

 public class AutomationHandler {
       //the OleAutomation object for this  handler
       private OleAutomation  oleAutomation;
       // Server relevant context
       private final int CLSCTX_SERVER= (COM.CLSCTX_INPROC_SERVER |
                                         COM.CLSCTX_LOCAL_SERVER  |

Creates an OleAutomation object with a given ProgID.

progID the Prog-ID for the COM Object whose functionality you need to access
       public AutomationHandler(String progID){
         GUID appClsid = getClassID(progID);
         int[] ppv = new int[1];
         int result = COM.CoCreateInstance(appClsid, 0,  , COM.IIDIUnknown, ppv);
         if (result != COM.S_OK){
               OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);
        IUnknown  objIUnknown = new IUnknown(ppv[0]);
        int[] ppvObject = new int[1];
        result = objIUnknown.QueryInterface(COM.IIDIDispatch, ppvObject);
        objIUnknown.Release(); // no more needed
        if (result != COM.S_OK){
           OLE.error(OLE.ERROR_INTERFACE_NOT_FOUND, result);
       IDispatch  objIDispatch = new IDispatch(ppvObject[0]);
        = new OleAutomation(objIDispatch);
       objIDispatch.Release(); // no more needed


    public AutomationHandler(IDispatch objIDispatch){
         = new OleAutomation(objIDispatch);

retrieves the CLSID of a COM object with a given ProgID.

clientName the Prog-ID for the COM Object
       private GUID getClassID(String clientName) {
         // create a GUID struct to hold the result
         GUID guid = new GUID();
         // create a null terminated array of char
         char[] buffer = null;
         if (clientName != null) {
           int count = clientName.length();
           buffer = new char[count + 1];
           clientName.getChars(0, countbuffer, 0);
         if (COM.CLSIDFromProgID(bufferguid) != COM.S_OK){
           int result = COM.CLSIDFromString(bufferguid);
           if (result != COM.S_OK)
             OLE.error(OLE.ERROR_INVALID_CLASSID, result);
         return guid;

Disposes the Automation object. This method releases the IDispatch interface on the Automation object.
    public void dispose() {
      if ( != null.dispose();
       = null;

returns the Automation object.
     public OleAutomation getAutomationObject(){
        return ;

returns the adress of Automation object.
    public int getAddress(){
        return .getAddress();
New to GrepCode? Check out our FAQ X