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/