继《轻松架起Java连接COM对象的桥梁》文章发表后,收到很多网友的来信,探讨关于jacob开
发的问题,这篇续文是为了解决jacob传送数组到com以及从com的提取com对修改后的数组参数。关
于jacob的使用这里不详述,请见《轻松架起Java连接COM对象的桥梁》,具体实现如下:
import com.jacob.com.*;
import com.jacob.activeX.*;
public class Exam_6
{
public static void main(String[] args)
{
ActiveXComponent pp = new ActiveXComponent("PrjCOM.MyCOM");
try {
SafeArray sa = new SafeArray(Variant.VariantString,2);
Variant var = new Variant();
sa.fromStringArray(new String[]{"H","H"});
var.putSafeArrayRef(sa);//传递数组引用地址
//如果只是传一个数组进去,而不需要取得改动后内容的话,可以用putSafeArray方法
Dispatch.call(pp, "Test",var);
String[] sack= sa.toStringArray();
System.out.println(sack[0]+"\t"+sack[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
VB实现的Com对象如下
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "MyCOM"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Public Sub Test(ByRef param() As String)
ReDim param(2)
param(0) = "Hello"
param(1) = "World"
End Sub
编译执行Exam_6.java
打印 Hello World
有疑问联系zlyperson@163.net