簡單來說,就是用Java呼叫COM元件的API
Jar檔下載位置:http://sourceforge.net/projects/jacob-project/
下方的範例程式:https://github.com/sabaao/sabaao/commit/d84a8df15fcb15e3106de9929aa9291592b986d8
而在使用時注意dll跟jar檔一定要是相同的版本,不然可能會出錯
以下為使用步驟(我使用的版本是1.14.3):
Step1:將jacob-1.14.3-x64.dll及jacob-1.14.3-x86.dll放置於C:\Windows\System32\下
Step2:注冊你要使用的元件(檔案路徑依情況作修改)
REGSVCS
D:\ConnStrManage\ConnStrForJava\ComSecureCnnStr.dll
GACUTIL /i
D:\ConnStrManage\ConnStrForJava\ComSecureCnnStr.dll
Gacutil.exe及Regsvcs.exe可以下列位置取得:
Gacutil.exe=>C:\Program Files (x86)\Microsoft
SDKs\Windows\v7.0A\Bin\x64
Regsvcs.exe=>C:\Windows\Microsoft.NET\Framework\v2.0.50727
Step3:於專案中加入jacob.jar參考
Util如下:
package tw.cbc.jacob;
import com.jacob.com.Dispatch;
public class Jacob {
private String dispatchName;
private String methodName;
private Object[] params;
private Dispatch dispatch;
public Jacob(String dispatchName){
this.dispatchName = dispatchName;
dispatch = new Dispatch(this.dispatchName);
}
public Jacob(String dispatchName,String methodName,Object[] params){
this.dispatchName = dispatchName;
this.methodName = methodName;
this.dispatch = new Dispatch(this.dispatchName);
this.params = params;
}
public String getDispatchName() {
return dispatchName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public Dispatch getDispatch() {
return dispatch;
}
public Object call(){
Object obj = null;
if(params==null){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==0){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==1){
obj = Dispatch.call(this.dispatch,this.methodName,params[0]);
}else if(params.length==2){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1]);
}else if(params.length==3){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2]);
}else if(params.length==4){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3]);
}else if(params.length==5){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4]);
}else if(params.length==6){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5]);
}else if(params.length==7){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6]);
}else if(params.length==8){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6],params[7]);
}
return obj;
}
}
呼叫範例:
Jacob jacob = new Jacob("SecureCnnStr");
jacob.setMethodName("EntSecurCnnStr");
jacob.setParams(new Object[]{"SOD","D:\\CBC\\encode\\","IBPS1455"});
jacob.call();
jacob.setMethodName("GetConnStr");
jacob.setParams(null);
String result = jacob.call().toString();
import com.jacob.com.Dispatch;
public class Jacob {
private String dispatchName;
private String methodName;
private Object[] params;
private Dispatch dispatch;
public Jacob(String dispatchName){
this.dispatchName = dispatchName;
dispatch = new Dispatch(this.dispatchName);
}
public Jacob(String dispatchName,String methodName,Object[] params){
this.dispatchName = dispatchName;
this.methodName = methodName;
this.dispatch = new Dispatch(this.dispatchName);
this.params = params;
}
public String getDispatchName() {
return dispatchName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public Dispatch getDispatch() {
return dispatch;
}
public Object call(){
Object obj = null;
if(params==null){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==0){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==1){
obj = Dispatch.call(this.dispatch,this.methodName,params[0]);
}else if(params.length==2){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1]);
}else if(params.length==3){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2]);
}else if(params.length==4){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3]);
}else if(params.length==5){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4]);
}else if(params.length==6){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5]);
}else if(params.length==7){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6]);
}else if(params.length==8){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6],params[7]);
}
return obj;
}
}
呼叫範例:
Jacob jacob = new Jacob("SecureCnnStr");
jacob.setMethodName("EntSecurCnnStr");
jacob.setParams(new Object[]{"SOD","D:\\CBC\\encode\\","IBPS1455"});
jacob.call();
jacob.setMethodName("GetConnStr");
jacob.setParams(null);
String result = jacob.call().toString();
沒有留言:
張貼留言