Java Call DLL(compiled by VB) java调用vb生成的dll

Java Call DLL(compiled by VB)

VB Side Programing and Compiling

  1. New ActiveX dll Project.

  2. Following code should be added to ClassModule.

    Option Base 1
    Function IStringProcess(name As String) As String
       '------------@function String Process
       '------------@param String name
       '------------@return String
       IStringCall = "Hello, " + name
    End Function
    Function 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 = True
    End Function
    Function 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 = c
    End Function
    Function IDoubleProcess(a#, b#) As Double
       '------------@function count
       '------------@param Double a;Double b
       '------------@return Double
       IDoubleProcess = a + b
    End Function

  3. Project name must be set and this name will be used in java side programing ,such as “test32”.

  4. 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.

  1. unzip the jacob-1.18-m2.zip
  2. put x86 or x64 dll in the bin folder under your jdk and jre path depend on Computer digit(32/64)
  3. put jar file to a fix place where you often put jar files or just skip this step when you choose mavem
  4. put dll file you compiled (such as test32.dll)in “C:\WINDOWS\SYSTEM32” and register it
  5. cmd→regedit→HKEY_CLASSES_ROOT find the dll you registered (it’s name will be used lately,such as test32.Class1)

Java Side Programing

  1. add jacob jar into build path

  2. 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"); 
    }
    @Deprecated
    public 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"); 
    }
    
    }

  3. Run result

Conclusion & Task List

  • dll compiled in 32-bit computer and jdk shoule be 32-bit

  • [ ] try dll compiled in 64-bit

猜你喜欢

转载自blog.csdn.net/u014774952/article/details/77942599
VB
今日推荐