Java SWT调用ActiveX实现

1.首先需要获取ActiveX内部属性方法信息.
参见[ http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/]
代码如下:
ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif * Copyright (c) 2000, 2003 IBM Corp. All rights reserved. This file is made
InBlock.gif * available under the terms of the Common Public License v1.0 which
InBlock.gif * accompanies this distribution, and is available at
InBlock.gif * 
http://www.eclipse.org/legal/cpl-v10.html
ExpandedBlockEnd.gif 
*/

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif * OLE and ActiveX example snippet: browse the typelibinfo for a program id
InBlock.gif * 
InBlock.gif * For a list of all SWT example snippets see
InBlock.gif * 
http://dev.eclipse.org/viewcvs/i.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
ExpandedBlockEnd.gif 
*/

None.gif
import org.eclipse.swt.SWT;
None.gif
import org.eclipse.swt.SWTException;
None.gif
import org.eclipse.swt.internal.ole.win32.TYPEATTR;
None.gif
import org.eclipse.swt.ole.win32.OLE;
None.gif
import org.eclipse.swt.ole.win32.OleAutomation;
None.gif
import org.eclipse.swt.ole.win32.OleControlSite;
None.gif
import org.eclipse.swt.ole.win32.OleFrame;
None.gif
import org.eclipse.swt.ole.win32.OleFunctionDescription;
None.gif
import org.eclipse.swt.ole.win32.OlePropertyDescription;
None.gif
import org.eclipse.swt.widgets.Shell;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class ListOLE dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public static void main(String[] args) dot.gif{
InBlock.gif
InBlock.gif    String progID 
= "CAMERA.CameraCtrl.1";
InBlock.gif
InBlock.gif    Shell shell 
= new Shell();
InBlock.gif
InBlock.gif    OleFrame frame 
= new OleFrame(shell, SWT.NONE);
InBlock.gif    OleControlSite site 
= null;
InBlock.gif    OleAutomation auto 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif      site 
= new OleControlSite(frame, SWT.NONE, progID);
InBlock.gif      auto 
= new OleAutomation(site);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (SWTException ex) dot.gif{
InBlock.gif      System.out.println(
"Unable to open type library for " + progID);
InBlock.gif      
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    TYPEATTR typeattr 
= auto.getTypeInfoAttributes();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (typeattr != nulldot.gif{
InBlock.gif      
if (typeattr.cFuncs > 0)
InBlock.gif        System.out.println(
"Functions for " + progID + " :\n");
ExpandedSubBlockStart.gifContractedSubBlock.gif      
for (int i = 0; i < typeattr.cFuncs; i++dot.gif{
InBlock.gif        OleFunctionDescription data 
= auto.getFunctionDescription(i);
InBlock.gif        String argList 
= "";
InBlock.gif        
int firstOptionalArgIndex =
InBlock.gif          data.args.length 
- data.optionalArgCount;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int j = 0; j < data.args.length; j++dot.gif{
InBlock.gif          argList 
+= "[";
InBlock.gif          
if (j >= firstOptionalArgIndex)
InBlock.gif            argList 
+= "optional, ";
InBlock.gif          argList 
+= getDirection(data.args[j].flags)
InBlock.gif            
+ ""
InBlock.gif            
+ getTypeName(data.args[j].type)
InBlock.gif            
+ " "
InBlock.gif            
+ data.args[j].name;
InBlock.gif          
if (j < data.args.length - 1)
InBlock.gif            argList 
+= "";
ExpandedSubBlockEnd.gif        }

InBlock.gif        System.out.println(
InBlock.gif          getInvokeKind(data.invokeKind)
InBlock.gif            
+ " (id = "
InBlock.gif            
+ data.id
InBlock.gif            
+ ") : "
InBlock.gif            
+ "\n\tSignature   : "
InBlock.gif            
+ getTypeName(data.returnType)
InBlock.gif            
+ " "
InBlock.gif            
+ data.name
InBlock.gif            
+ "("
InBlock.gif            
+ argList
InBlock.gif            
+ ")"
InBlock.gif            
+ "\n\tDescription : "
InBlock.gif            
+ data.documentation
InBlock.gif            
+ "\n\tHelp File   : "
InBlock.gif            
+ data.helpFile
InBlock.gif            
+ "\n");
ExpandedSubBlockEnd.gif      }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    auto.dispose();
InBlock.gif    shell.dispose();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
private static String getTypeName(int type) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
switch (type) dot.gif{
InBlock.gif      
case OLE.VT_BOOL :
InBlock.gif        
return "boolean";
InBlock.gif      
case OLE.VT_R4 :
InBlock.gif        
return "float";
InBlock.gif      
case OLE.VT_R8 :
InBlock.gif        
return "double";
InBlock.gif      
case OLE.VT_I4 :
InBlock.gif        
return "int";
InBlock.gif      
case OLE.VT_DISPATCH :
InBlock.gif        
return "IDispatch";
InBlock.gif      
case OLE.VT_UNKNOWN :
InBlock.gif        
return "IUnknown";
InBlock.gif      
case OLE.VT_I2 :
InBlock.gif        
return "short";
InBlock.gif      
case OLE.VT_BSTR :
InBlock.gif        
return "String";
InBlock.gif      
case OLE.VT_VARIANT :
InBlock.gif        
return "Variant";
InBlock.gif      
case OLE.VT_CY :
InBlock.gif        
return "Currency";
InBlock.gif      
case OLE.VT_DATE :
InBlock.gif        
return "Date";
InBlock.gif      
case OLE.VT_UI1 :
InBlock.gif        
return "unsigned char";
InBlock.gif      
case OLE.VT_UI4 :
InBlock.gif        
return "unsigned int";
InBlock.gif      
case OLE.VT_USERDEFINED :
InBlock.gif        
return "UserDefined";
InBlock.gif      
case OLE.VT_HRESULT :
InBlock.gif        
return "int";
InBlock.gif      
case OLE.VT_VOID :
InBlock.gif        
return "void";
InBlock.gif
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_BOOL :
InBlock.gif        
return "boolean *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_R4 :
InBlock.gif        
return "float *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_R8 :
InBlock.gif        
return "double *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_I4 :
InBlock.gif        
return "int *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_DISPATCH :
InBlock.gif        
return "IDispatch *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_UNKNOWN :
InBlock.gif        
return "IUnknown *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_I2 :
InBlock.gif        
return "short *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_BSTR :
InBlock.gif        
return "String *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_VARIANT :
InBlock.gif        
return "Variant *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_CY :
InBlock.gif        
return "Currency *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_DATE :
InBlock.gif        
return "Date *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_UI1 :
InBlock.gif        
return "unsigned char *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_UI4 :
InBlock.gif        
return "unsigned int *";
InBlock.gif      
case OLE.VT_BYREF | OLE.VT_USERDEFINED :
InBlock.gif        
return "UserDefined *";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return "unknown " + type;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
private static String getDirection(int direction) dot.gif{
InBlock.gif    String dirString 
= "";
InBlock.gif    
boolean comma = false;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if ((direction & OLE.IDLFLAG_FIN) != 0dot.gif{
InBlock.gif      dirString 
+= "in";
InBlock.gif      comma 
= true;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
if ((direction & OLE.IDLFLAG_FOUT) != 0dot.gif{
InBlock.gif      
if (comma)
InBlock.gif        dirString 
+= "";
InBlock.gif      dirString 
+= "out";
InBlock.gif      comma 
= true;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
if ((direction & OLE.IDLFLAG_FLCID) != 0dot.gif{
InBlock.gif      
if (comma)
InBlock.gif        dirString 
+= "";
InBlock.gif      dirString 
+= "lcid";
InBlock.gif      comma 
= true;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
if ((direction & OLE.IDLFLAG_FRETVAL) != 0dot.gif{
InBlock.gif      
if (comma)
InBlock.gif        dirString 
+= "";
InBlock.gif      dirString 
+= "retval";
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return dirString;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
private static String getInvokeKind(int invKind) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
switch (invKind) dot.gif{
InBlock.gif      
case OLE.INVOKE_FUNC :
InBlock.gif        
return "METHOD";
InBlock.gif      
case OLE.INVOKE_PROPERTYGET :
InBlock.gif        
return "PROPERTY GET";
InBlock.gif      
case OLE.INVOKE_PROPERTYPUT :
InBlock.gif        
return "PROPERTY PUT";
InBlock.gif      
case OLE.INVOKE_PROPERTYPUTREF :
InBlock.gif        
return "PROPERTY PUT BY REF";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return "unknown " + invKind;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

2.得到类似下面结果
None.gif METHOD (id  =   11 ) : 
None.gif    Signature   : boolean TestFile(
[]  String strFileName)
None.gif    Description : null
None.gif
None.gifMETHOD (id 
=   12 ) : 
None.gif    Signature   : boolean TestApp(
[]  String * lpstrErr)
None.gif    Description : null
注意,方法TestFile传递的参数是String, 方法TestApp传递的引用型String

3.引用型参数工具类
如果在TestApp传递String, 必将报Type Mismatch错误. 我们需要能转换成引用型的方法
None.gif import  org.eclipse.swt.internal.win32.OS;
None.gif
import  org.eclipse.swt.internal.ole.win32.COM;
None.gif
None.gif
public   static  Variant getIntByRef(  int  val )
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif        
int ptr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, 4);
ExpandedSubBlockStart.gifContractedSubBlock.gif        OS.MoveMemory(ptr, 
new int[] dot.gif{val}4 );
InBlock.gif        Variant res 
= new Variant( ptr, (short)(OLE.VT_I4 | OLE.VT_BYREF) );
InBlock.gif        
return res;
ExpandedBlockEnd.gif      }

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

4. 现在我们可以调用AcitveX了
ExpandedBlockStart.gif ContractedBlock.gif public   void  createPartControl(Composite parent)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            OleFrame frame 
= new OleFrame(parent, SWT.NONE);
InBlock.gif            OleClientSite clientSite 
= new OleClientSite(frame, SWT.NONE, "Test.TestCtrl.1");            
InBlock.gif            OleAutomation auto 
= new OleAutomation(clientSite); InBlock.gif
InBlock.gif            clientSite.doVerb(OLE.OLEIVERB_SHOW);
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] rgdispid = auto.getIDsOfNames(new String[]dot.gif{"TestApp"});
InBlock.gif            
int dispIdMember = rgdispid[0];                
InBlock.gif        
InBlock.gif            Variant[] rgvarg 
= new Variant[1]; 
InBlock.gif            rgvarg[
0= getStringByRef("");
InBlock.gif            
InBlock.gif            Variant pVarResult 
= auto.invoke(dispIdMember, rgvarg);
InBlock.gif            
int errorMsg = rgvarg[0].getByRef();
InBlock.gif
InBlock.gif            System.out.println(errorMsg);
InBlock.gif            System.out.println(pVarResult);
InBlock.gif            System.out.println(auto.getLastError());
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (RuntimeException e) dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/crabo/archive/2006/12/14/592102.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值