Java Call DLL(compiled by VB) java调用vb生成的dll
来源:互联网 发布:淘宝拉黑退款率高的人 编辑:程序博客网 时间:2024/06/08 05:26
Java Call DLL(compiled by VB)
VB Side Programing and Compiling
New ActiveX dll Project.
Following code should be added to ClassModule.
Option Base 1Function IStringProcess(name As String) As String '------------@function String Process '------------@param String name '------------@return String IStringCall = "Hello, " + nameEnd FunctionFunction ITextFile(dataFileDir@, resultFileDir@) As Boolean '------------@function read data from txt file and process '------------@param String dataFileDir;String resultFileDir '------------@return Boolean Dim a(10) As Double, b(10) As Double, c(10) As Double, i% Open dataFileDir@ For Input As #1 For i = 1 To 10 Input #1, a(i), b(i) Next i Close #1 For i = 1 To 10 c(i) = a(i) + b(i) Next i Open resultFileDir@ For Output As #2 For i = 1 To 10 Write #2, c(i) Next i Close #2 ITextFileCall = TrueEnd FunctionFunction IArrayAdd(a() As Double, b() As Double) As Double() '------------@function array plus '------------@param Double a();Double b() '------------@return Double Dim c(10) As Double, j% For j = 1 To 10 c(j) = a(j) + b(j) Next j IArrayAdd = cEnd FunctionFunction IDoubleProcess(a#, b#) As Double '------------@function count '------------@param Double a;Double b '------------@return Double IDoubleProcess = a + bEnd Function
Project name must be set and this name will be used in java side programing ,such as “test32”.
Compile dll:File→Generate test.dll (save path “C:\WINDOWS\SYSTEM32” is recommended).
Preparement Before Calling the dll(vb)
Before Java side work ,follwing quotes should be noticed and several preparement works should be done strictly with the guidance.
Although the dll VB compiled has the suffix *.dll,but it do belongs to com component which means it’s not a standard dll (Actually VB can generate standard dll,but it cost too much time VB Standard dll).
Owing to this ,the jni method that we’re familiar with can’t be used here.An alternative method are finnally be found which named jacob(JAVA-COM Bridge),it provide a path to call com component or win32 system library .
Jacob(1.18-M2) can be found in the project (maven), following action should be finished before we use the jacob method.
- unzip the jacob-1.18-m2.zip
- put x86 or x64 dll in the bin folder under your jdk and jre path depend on Computer digit(32/64)
- put jar file to a fix place where you often put jar files or just skip this step when you choose mavem
- put dll file you compiled (such as test32.dll)in “C:\WINDOWS\SYSTEM32” and register it
- cmd→regedit→HKEY_CLASSES_ROOT find the dll you registered (it’s name will be used lately,such as test32.Class1)
Java Side Programing
add jacob jar into build path
program
package cn.edu.hhu.iehh.service;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import cn.edu.hhu.iehh.util.FileUtil;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.SafeArray;import com.jacob.com.Variant;public class JavaCallVB {public static void main(String[] args) { //init JavaCallVB process =new JavaCallVB(); ActiveXComponent pp=new ActiveXComponent("test32.Class1"); //process process.StringProcess(pp); process.TextFileProcess(pp); process.DoubleProcess(pp); process.ArrayAdd(pp);}public void StringProcess(ActiveXComponent pp){ System.out.println(); System.out.println("-------------------------call IStringProcess"); Variant c=Dispatch.call(pp, "IStringProcess",new Object[]{"amos"}); String result=c.getString(); System.out.println(result); System.out.println("-------------------------finish call IStringProcess"); }public void TextFileProcess(ActiveXComponent pp){ System.out.println(); System.out.println("-------------------------call TextFile"); String dataFileDir="C:\\data.txt"; String resultFileDir="C:\\result.txt"; try { FileWriter fos=new FileWriter(dataFileDir); BufferedWriter bw=new BufferedWriter(fos); int [] a=new int[]{111,222,333,444,555,666,777,888,999,100}; for (int i = 0; i < a.length; i++) { bw.write(String.valueOf(a[i])+" "+String.valueOf(a[i])); bw.newLine(); } bw.flush(); bw.close(); fos.close(); } catch (IOException e1) { e1.printStackTrace(); } Variant c=Dispatch.call(pp, "ITextFile",new Object[]{dataFileDir,resultFileDir}); boolean result=c.getBoolean(); if(result){ FileUtil.readTxtFile(resultFileDir); } System.out.println("-------------------------finish call TextFile"); }@Deprecatedpublic void ArrayAdd(ActiveXComponent pp){ System.out.println(); System.out.println("-------------------------call IArrayAdd"); double [] a={111,222,333,444,555,666,777,888,999,100}; SafeArray safea=new SafeArray( Variant.VariantDouble,10); SafeArray safeb=new SafeArray( Variant.VariantDouble,10); for (int i = 0; i <a.length; i++) { safea.setDouble(i, a[i]); safeb.setDouble(i, a[i]); } Variant c=Dispatch.call(pp, "IDoubleProcess",new Object[]{safea,safeb}); //double[] result=c.getVariantArray() System.out.println(c); System.out.println("-------------------------finish call IArrayAdd"); }public void DoubleProcess(ActiveXComponent pp){ System.out.println(); System.out.println("-------------------------call IDoubleProcess"); Variant c=Dispatch.call(pp, "IDoubleProcess",new Object[]{"11.1","22.2"}); double result=c.getDouble(); System.out.println(result); System.out.println("-------------------------finish call IDoubleProcess"); }}
Run result
Conclusion & Task List
dll compiled in 32-bit computer and jdk shoule be 32-bit
[ ] try dll compiled in 64-bit
- Java Call DLL(compiled by VB) java调用vb生成的dll
- vb 生成dll ,vbs 调用dll
- java 用jacob 调用vb写的dll
- java 用jacob 调用vb写的dll
- vb调用vc的dll
- VC 调用 VB 的DLL
- VB调用VC写的DLL导出函数出现“Bad DLL call convention”的解决
- VB调用DLL导出函数出现“Bad DLL call convention”的解决
- VB调用DLL导出函数出现"Bad DLL call convention"的解决
- VB .bas文件生成dll并且调用
- vb.net 下调用生成dll的SqlHelper
- C#生成基于COM的DLL供VB调用
- (转)C#生成基于COM的DLL供VB调用
- VC Call VB Active DLL
- VB如何调用DLL
- VB 调用DLL
- vc DLL vb 调用
- vb调用vc dll
- 反射方法调用
- 【数据结构】[luoguP1886]滑动窗口
- window下基于anaconda安装tensorflow
- Java8 新特性之流式数据处理
- Apache Camel之FTP组件学习
- Java Call DLL(compiled by VB) java调用vb生成的dll
- 7.0fileprovider的使用
- 1061. 判断题(15)
- 在Centos 中将zookeeper设置成开机启动
- 深入理解Hadoop集群和网络
- 【webpack】入门Demo
- 软件测试需要掌握的linux的命令(一)
- Tengine与Nginx特性详解
- 考研最后冲刺100天