How to invoke DLL methods in Java

Source: Internet
Author: User

The previous time project needed to use the calling DLL to control the POS peripherals. such as the cash box and small ticket printers, customer display, and so on, I began to invoke the Java method in the DLL to explore the methods, in fact, the online example is still a lot of, some external package examples are many, but each person's actual situation is different, Although it was just a simple call, it took me quite a while to share my experience here.

I was indirectly using an external jar package to get the handle to the DLL file. It is Jacob, this is the Java COM brige shorthand, hehe, this name is very image, I use the version is Jacob 1.9, you can go to its official website to download, Download back of the package will have two files we need to use, one is Jacob.dll, one is Jacob.jar,jacob.dll can be copied to the system system32 directory, and Jacob.jar file, directly add it to the project's library can be. After these two preparations are complete, you can begin to try the call.

Create a new class that introduces two classes in the Jacob.jar,

Import  com.jacob.activeX.ActiveXComponent;  
Import  Com.jacob.com.Dispatch;

Then read the DLL file through the DLL's Controllerid

public class Print {  
    private activexcomponent printcontroller = null;  
    Private Dispatch printobj = null;  
    /** 
     * Default Controllerid method *
    /Public Print () {  
        try{  
            printcontroller = new Activexcomponent (" Poscontroler.controler ");  
            Printobj = (Dispatch) printcontroller.getobject ();  
        } catch (Exception e) {  
            printobj = new Dispatch ();  
            Common.alert (E.getmessage (), "Read Terminal control DLL failed");}}  

So you can get the handle to the DLL file, so how do you call the method inside?

If the method in the method DLL is an empty argument, call it directly, such as

Dispatch.call (Printobj, "Setdefaultfont");

When you call a method that has parameters, you need to pass the arguments in one after the other, noting in order OH:

Dispatch.call (Printobj, "show", Value,type);

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.