怎么使用jacob,以及常见问题总结
1个回答
2015-01-13 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464973
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
jacob 是java用来与用其它语言写的dll通信的桥.
1. 使用时我们必须先把jacob.dll 加载到系统system32 目录下 ,有些系统可以还需要用resvr32 ??.dll 注册。
还有就是要把jacob.jar 放到 classpath下,或者 当前项目的lib 库下。注意其的跟项目有关的.dll 文件也一般放在system32 目 录下面。
2. First load the library:
//Loading the library:
ActiveXComponent comp=new ActiveXComponent("Com.Calculation");
System.out.println("The Library been loaded, and an activeX component been created");
这可能会出现一个异常:ComFailException: Can't get object clsid from progid 这是因为你没有注册.dll 文件或者没有正确注册;
3.
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
System.out.println("Summation= "+ summation); int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
System.out.println("Subtraction= "+ subtraction);
int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
System.out.println("Multiplication= "+ multiplication);
double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
System.out.println("Division= "+ division);
使用dispatch class ,第一个参数是dispatch组件 第二个是 函数名 ,第三,四个是参数
4. 如果想得到一个属性的值:
Dispatch. get(object, “Property Name”);
package jacobtest;
import com.jacob.activeX.*;
import com.jacob.com.*;
/**
* This class uses the the Jacob tech
* to use and interact with a Com cmponent
* in a java application
*/
public class ReadDLL {
public static void main(String[] args){
//Loading the library:
ActiveXComponent comp=new ActiveXComponent("Com.Calculation");
System.out.println("The Library been loaded, and an activeX component been created");
int arg1=100;
int arg2=50;
//using the functions from the library:
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
System.out.println("Summation= "+ summation);
int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
System.out.println("Subtraction= "+ subtraction);
int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
System.out.println("Multiplication= "+ multiplication);
double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
System.out.println("Division= "+ division);
/**The following code is abstract of using the get,
* when the library contains a function that return
* some kind of a struct, and then get its properties and values
**/
// Dispatch disp=Dispatch.call(comp,"sum",100,10).toDispatch();
// DataType Var=Dispatch.get(disp,"Property Name");
}
}
输出结果:
Summation= 150
Subtraction= 50
Multiplication= 5000
Division= 2.0
1. 使用时我们必须先把jacob.dll 加载到系统system32 目录下 ,有些系统可以还需要用resvr32 ??.dll 注册。
还有就是要把jacob.jar 放到 classpath下,或者 当前项目的lib 库下。注意其的跟项目有关的.dll 文件也一般放在system32 目 录下面。
2. First load the library:
//Loading the library:
ActiveXComponent comp=new ActiveXComponent("Com.Calculation");
System.out.println("The Library been loaded, and an activeX component been created");
这可能会出现一个异常:ComFailException: Can't get object clsid from progid 这是因为你没有注册.dll 文件或者没有正确注册;
3.
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
System.out.println("Summation= "+ summation); int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
System.out.println("Subtraction= "+ subtraction);
int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
System.out.println("Multiplication= "+ multiplication);
double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
System.out.println("Division= "+ division);
使用dispatch class ,第一个参数是dispatch组件 第二个是 函数名 ,第三,四个是参数
4. 如果想得到一个属性的值:
Dispatch. get(object, “Property Name”);
package jacobtest;
import com.jacob.activeX.*;
import com.jacob.com.*;
/**
* This class uses the the Jacob tech
* to use and interact with a Com cmponent
* in a java application
*/
public class ReadDLL {
public static void main(String[] args){
//Loading the library:
ActiveXComponent comp=new ActiveXComponent("Com.Calculation");
System.out.println("The Library been loaded, and an activeX component been created");
int arg1=100;
int arg2=50;
//using the functions from the library:
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
System.out.println("Summation= "+ summation);
int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
System.out.println("Subtraction= "+ subtraction);
int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
System.out.println("Multiplication= "+ multiplication);
double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
System.out.println("Division= "+ division);
/**The following code is abstract of using the get,
* when the library contains a function that return
* some kind of a struct, and then get its properties and values
**/
// Dispatch disp=Dispatch.call(comp,"sum",100,10).toDispatch();
// DataType Var=Dispatch.get(disp,"Property Name");
}
}
输出结果:
Summation= 150
Subtraction= 50
Multiplication= 5000
Division= 2.0
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询