Jacob的简单使用

来源:互联网 发布:apache ant 1.7.1下载 编辑:程序博客网 时间:2024/06/02 23:49

                这几天接到了一个项目,就是关于做中控指纹考勤机二次开发的,而中控的考勤机的dll是activex控件,所以使用jacob可以直接读取。在用jacob时,在做new active object的时候,里面传的参数是中控考勤机dll注册后的prog Id(zkemkeeper.ZKEM.1是在注册表里面找到的,在注册里面就搜索zkemsdk.dll就可以找到其progid了),不是classid。

                做这个项目时,要把中控的dll文件和jacob的jacob.dll拷到system32 下面 ,在做该项目时,要在工程中引入jacob.jar包.

下面介绍几个class:

1. ActiveXComponent Class: 可以说将com封装成一个组件。
2. Dispatch Class: 表示MS level dispatch object!相当于在windows上的资料结构。
3. Variant Class: 在java 与com中做互相通信的参数  

4. ComException Class: COM JNI 的错误处理。

幾個主要的函數:

1. call method: Dispatch class的函數!可用來呼叫COM/DLL的函數!它可以回傳結果(Variant variable)!
2. callSub method: 跟call函數類似!不過不回傳值!
3. get method: Dispatch class的函數!用來取得COM的屬性!
4. put method: Dispatch class的函數!用來設定COM的屬性!
5. invoke method: 等同於call函數!不過使用方式較複雜!
6. invokeSub method: 等同於callSub函數!不過使用方式較複雜!
7. getProperty method: ActiveXComponent class的函數!用來取得COM的屬性!
8. setProperty method: ActiveXComponent class的函數!用來設定COM的屬性!


通常要建立一個JACOB程式分成下面幾個步驟:

1. 構建ActiveX元件物件:
ActiveXComponent word = new ActiveXComponent("Word.Application");
其中的ActiveXComponent建構式內的值和你需要調用的ActiveX控制項有關!
一般常用的MS控制項

InternetExplorer: InternetExplorer.Application
Excel: Excel: Application
Word: Word.Application
Powerpoint: Powerpoint.Application
vb/java Script: ScriptControl
windows media Player: WMPlayer.OCX
Outlook: Outlook.Application
Visio: Visio.Application
DAO: DAO.PrivateDBEngine.35
MultiFace: MultiFace.Face

另外可用clsid建立! 如:  ActiveXComponent word = new ActiveXComponent("clsid:000209FF-0000-0000-C000-000000000046");

2. 設定或元件屬性:
如我要將word開啟後不隱藏!
word.setProperty("Visible", new Variant(true));//都是透過Variant來傳參數與COM溝通
Variant vs=Word.getProperty("Visible");

Dispatch class則透過了get與put
如:
Dispatch oDocuments = Word.getProperty("Documents").toDispatch();//透過toDispatch轉型
Variant v= Dispatch.get(oDocuments,"Parent");

3. 執行功能:
可以透過call! 也可以透過callN! 兩者不同點在於callN是以陣列方式傳參數!
Dispatch oDocument = Dispatch.call(oDocuments, "Open",  new Variant("C://atest.doc")).toDispatch();
Dispatch oDocument = Dispatch.callN(oDocuments, "Open", new Variant[]{ new Variant("C://atest.doc")}).toDispatch();

再用一個Dispatch的變數oDocument! 是為了之後能關閉close
Dispatch.call(oDocument, "Close", new Variant(false));
        
4. 關閉與回收資源
若有開啟檔案則要關閉與回收資源
if (Word != null) {
            Word.invoke("Quit", new Variant[] {new Variant(0)});
            Word = null;
            ComThread.Release();
}

原创粉丝点击