1.首先需要获取ActiveX内部属性方法信息.
参见[
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/]
代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
* 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
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
* 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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class ListOLE
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String progID = "CAMERA.CameraCtrl.1";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Shell shell = new Shell();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
OleFrame frame = new OleFrame(shell, SWT.NONE);
OleControlSite site = null;
OleAutomation auto = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
site = new OleControlSite(frame, SWT.NONE, progID);
auto = new OleAutomation(site);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (SWTException ex)
{
System.out.println("Unable to open type library for " + progID);
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TYPEATTR typeattr = auto.getTypeInfoAttributes();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (typeattr != null)
{
if (typeattr.cFuncs > 0)
System.out.println("Functions for " + progID + " :\n");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < typeattr.cFuncs; i++)
{
OleFunctionDescription data = auto.getFunctionDescription(i);
String argList = "";
int firstOptionalArgIndex =
data.args.length - data.optionalArgCount;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (typeattr.cVars > 0)
System.out.println("\n\nVariables for " + progID + " :\n");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
auto.dispose();
shell.dispose();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static String getTypeName(int type)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static String getDirection(int direction)
{
String dirString = "";
boolean comma = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((direction & OLE.IDLFLAG_FIN) != 0)
{
dirString += "in";
comma = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((direction & OLE.IDLFLAG_FOUT) != 0)
{
if (comma)
dirString += ", ";
dirString += "out";
comma = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((direction & OLE.IDLFLAG_FLCID) != 0)
{
if (comma)
dirString += ", ";
dirString += "lcid";
comma = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((direction & OLE.IDLFLAG_FRETVAL) != 0)
{
if (comma)
dirString += ", ";
dirString += "retval";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return dirString;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static String getInvokeKind(int invKind)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.得到类似下面结果
METHOD (id = 11) :
Signature : boolean TestFile([] String strFileName)
Description : null
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public static Variant getIntByRef( int val )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int ptr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, 4);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
OS.MoveMemory(ptr, new int[]
{val}, 4 );
Variant res = new Variant( ptr, (short)(OLE.VT_I4 | OLE.VT_BYREF) );
return res;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private static Variant getStringByRef( String text )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
char[] data = (text+"\0").toCharArray();
int ptr = COM.SysAllocString(data);
int ptr2 = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, 4);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
COM.MoveMemory(ptr2, new int[]
{ptr}, 4);
Variant v = new Variant( ptr2, (short)(COM.VT_BYREF | COM.VT_BSTR));
return v;
}
4. 现在我们可以调用AcitveX了
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public void createPartControl(Composite parent)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
OleFrame frame = new OleFrame(parent, SWT.NONE);
OleClientSite clientSite = new OleClientSite(frame, SWT.NONE, "Test.TestCtrl.1");
OleAutomation auto = new OleAutomation(clientSite); ![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientSite.doVerb(OLE.OLEIVERB_SHOW);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.out.println(errorMsg);
System.out.println(pVarResult);
System.out.println(auto.getLastError());
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (RuntimeException e)
{
e.printStackTrace();
}
}