• 博客园Logo
  • 首页
  • 新闻
  • 博问
  • 会员
  • 闪存
  • 班级
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 简洁模式 ... 退出登录
    注册 登录
老杨随笔-重用与重构的艺术
专注于RIA架构设计,无止境的重构,无限制的重用
博客园    首页    新随笔    联系   管理    订阅  订阅

Java SWT调用ActiveX实现

1.首先需要获取ActiveX内部属性方法信息.
参见[http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/]
代码如下:

/**//*
 * 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 ListOLE {

  
public static void main(String[] args) {

    String progID 
= "CAMERA.CameraCtrl.1";

    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;
  }

}


2.得到类似下面结果
METHOD (id = 11) : 
    Signature   : boolean TestFile(
[] String strFileName)
    Description : null

METHOD (id 
= 12) : 
    Signature   : boolean TestApp(
[] String * lpstrErr)
    Description : null
注意,方法TestFile传递的参数是String, 方法TestApp传递的引用型String

3.引用型参数工具类
如果在TestApp传递String, 必将报Type Mismatch错误. 我们需要能转换成引用型的方法
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.internal.ole.win32.COM;

public static Variant getIntByRef( int val )
      
{
        
int ptr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, 4);
        OS.MoveMemory(ptr, 
new int[] {val}, 4 );
        Variant res 
= new Variant( ptr, (short)(OLE.VT_I4 | OLE.VT_BYREF) );
        
return res;
      }


    
private static Variant getStringByRef( String text )
      
{
        
char[] data = (text+"\0").toCharArray();
        
int ptr = COM.SysAllocString(data);
        
int ptr2 = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, 4);
        COM.MoveMemory(ptr2, 
new int[] {ptr}, 4);
        Variant v 
= new Variant( ptr2, (short)(COM.VT_BYREF | COM.VT_BSTR));
        
return v;
      }

4. 现在我们可以调用AcitveX了
public void createPartControl(Composite parent) {
        
try {
            OleFrame frame 
= new OleFrame(parent, SWT.NONE);
            OleClientSite clientSite 
= new OleClientSite(frame, SWT.NONE, "Test.TestCtrl.1");            
            OleAutomation auto 
= new OleAutomation(clientSite); 
            clientSite.doVerb(OLE.OLEIVERB_SHOW);
            
            
int[] rgdispid = auto.getIDsOfNames(new String[]{"TestApp"});
            
int dispIdMember = rgdispid[0];                
        
            Variant[] rgvarg 
= new Variant[1]; 
            rgvarg[
0] = getStringByRef("");
            
            Variant pVarResult 
= auto.invoke(dispIdMember, rgvarg);
            
int errorMsg = rgvarg[0].getByRef();

            System.out.println(errorMsg);
            System.out.println(pVarResult);
            System.out.println(auto.getLastError());
            
        }
 catch (RuntimeException e) {
            e.printStackTrace();
        }

    }
posted @ 2006-12-14 14:28  craboYang  阅读(3200)  评论(2)  编辑  收藏  举报
刷新评论刷新页面返回顶部
Copyright © 2023 craboYang
Powered by .NET 7.0 on Kubernetes