Java SWT调用ActiveX实现
1.首先需要获取ActiveX内部属性方法信息.
参见[http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/]
代码如下:
2.得到类似下面结果
3.引用型参数工具类
如果在TestApp传递String, 必将报Type Mismatch错误. 我们需要能转换成引用型的方法
4. 现在我们可以调用AcitveX了
参见[http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/]
代码如下:
2.得到类似下面结果
METHOD (id = 11) :
Signature : boolean TestFile([] String strFileName)
Description : null
METHOD (id = 12) :
Signature : boolean TestApp([] String * lpstrErr)
Description : null
注意,方法TestFile传递的参数是String, 方法TestApp传递的引用型StringSignature : boolean TestFile([] String strFileName)
Description : null
METHOD (id = 12) :
Signature : boolean TestApp([] String * lpstrErr)
Description : null
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;
}
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();
}
}
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();
}
}