java调用ActiveX控件

import com.jacob.com.*;
import com.jacob.com.*;
import com.jacob.activeX.*;
public class DispatchTest
{
public static void main(String[] args)
{
ActiveXComponent xl = new ActiveXComponent("Word.Application";
Object xlo = xl.getObject();
try
{
System.out.println("version="+xl.getProperty("Version");
// for (int i=0; i<100; i++)
System.out.println("version="+Dispatch.get(xl, 1));
// System.out.println("version="+Dispatch.get(xlo,"Version");
}

catch (Exception e)
{
e.printStackTrace();
}

finally
{
xl.invoke("Quit", new Variant[] {});
}

}

}

 

 

另一个例子:
 

在Java调用VB编写的Dll文件

import com.jacob.com.*;
import com.jacob.activeX.*;
public class VbdllCall
{
public static String md5CallVbdll(String str){
String res="";
try {
ActiveXComponent pp = new ActiveXComponent("md5.Class1");
Dispatch myCom = (Dispatch) pp.getObject(); //生成一个对象
Variant result = Dispatch.call( myCom, "MD5", str) ;
res=result.toString();
}
catch (Exception e) {
res="";
e.printStackTrace();
}

return res;
}

}

 

在公司里,不同的系统使用不能的语言非常正常,我曾经在一个公司就职,原先的博客系统使用asp编写的,里面有很多dll文件,在asp里,可直接调用dll文件,非常简单,但由于系统升级后,都统一使用了Java,那么原先编写的很多模块(封装在dll里的)就都不能用了,但有一个加密算法还非得在客服系统里用到(因为新旧系统要并行运行一段时间,客服系统必须同时能管理新旧两大系统),那么如何在Java中调用VB编写的Dll文件呢?代码如下

 

这里使用到了Jacob包,有兴趣的读者可以Google一下,深入的了解一下这个Jacob包