Java Access a DLL file in finger print attendance reader SDK - java

I’m trying to build a fingerprint attendance system. I want to directly query attendance records from finger print attendance device to my java application. To do so I want to deal with the SDK of the FP Attendance device. Manufacture of the device has provided the SDK and the documentation which contains a DLL (Riss.Devices.dll) file that has all the necessary entity classes and utility classes.
I googled whole two days to find a way to interact with this dll (Riss.Devices.dll) file through java and I found many options like JNI, JNA, SWIG etc. But Each and every option is new to me.
If someone can help me to find most suitable way to do this, is very helpful to me because I don’t have much time to hang on.
Documentation

I think JNA is the easiest option. Your code would look something like:
import com.sun.jna.*;
public class Foo {
public interface RissDevices extends Library {
public int BitCheck(int num, int index);
public int SetBit(int num, int index);
// TODO: list the other functions you want
}
public static void main(String[] args) {
RissDevices library = (RissDevices) Native.loadLibrary("c:/path/to/Riss.Devices.dll", RissDevices.class);
System.out.println(library.BitCheck(0, 0));
}

Related

How to call Function from an C# dll in Java?

i have a SAP-DLL to enable communication between a Programming interface and the SAP Programm.
I have following example Code for c# in combination with the dll-File:
var loggerService = LoggerService.GetLoggerService("FileLogger");
var itasProxy = SapProxyFactory.CreateSapProxy(SapSystem.Example, loggerService, "Example_User", StringExtension.CreateSecureString("Example_Password"));
var funcResult = sapProxy.SearchSapAddress(clientNo);
if (funcResult.Successfull)
{
funcResult.ReturnValue = withFormatting
? AddressFormatter.SplitStreetHouseNo(funcResult.ReturnValue)
: funcResult.ReturnValue;
}
Now i want the same functionality to be transferred to java. I have absolutely no clue how to do that. I tried the following with Loggerservice as a starter, but it doesn't work:
public class SAPConnector {
public static void main(String[] args) {
// TODO Auto-generated method stub
connectSAP();
}
public void connectSAP()
{
System.load("C://Temp//SapConnector.dll");
Object loggerService = getLoggerService("FileLogger");
}
public native Object getLoggerService(String lcLogger);
}
i just need some kind of information how to call the Functions from the dll or an example how to transfer the C# Code to working Code in Java.
Greetings,
Kevin
DLL is Microsoft format. Java is cross-platform, thus can't acknowledge anything operating-system specific, such as DLL.
One way around that is to use JNI (Java Native Interface), but that's usually not a good solution, as it makes your program platform-dependent.
Instead, I would look for a JAR from SAP, that provides a similar interface.
Maybe something along SAP JCO.
You can see some actual code examples using JCO here, and some technical information on step-by-step download and configure here.

How can a user written program be send to library?

I tried a code.
Well, the first part is the main method that basically lays down the directory structure. I tried to delete a directory that contains other directories using the rmdirs method I wrote below.
public static void rmdirs(File k)
{
String[] y= k.list();
int i;
File f;
for(i=0;i<y.length;i++)
{
f= new File(k,y[i]);
if(f.isDirectory() && f.list().length>0)
{
rmdirs(f);
}
else
{
f.delete();
}
}
k.delete();
}
The rmdirs method is working and seems to be doing what I expected, but how do I add this program to a library, so that I can repeatedly use it by importing something.
Also, the above program does something like
rmdirs(f2);
to delete a file.
I would like it to be something like
f2.rmdirs();
And I am wondering how I can do it. I tried somehting like
import java.io.*;
public class RFile extends File
{
public RFile(String p)
{
super(p);
}
public RFile(File f1,String p1)
{
super(f1,p1);
}
public void rmdirs()
{
RFile k=this;
String[] y= k.list();
int i;
RFile f;
for(i=0;i<y.length;i++)
{
f= new RFile(k,y[i]);
if(f.isDirectory() && f.list().length>0)
{
f.rmdirs();
}
else
{
f.delete();
}
}
k.delete();
}
}
But then, the tester class or main class becomes one in which I have to use RFile and not File.
This is a problem; Also, like I asked before, how do I add all these to a library so that importing java.io.RFile or something like that will do the job?
You don't extend java.io.File (unless you have a very good reason and this is not such a reason)
One solution is to create a class like "FileUtils" which has a static method "remove" so you can call:
FileUtils.remove(myFile);
It's a general design philosophy that you can find in for example apache libraries (e.g. http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html)
UPDATE
A library is simply a reusable collection of code with a specific purpose.
Apache is a foundation that manages a lot of open source projects (a lot if not all of it java-based). They provide high quality (though in a few cases outdated) software that can be reused. While you're at it you might want to take a peek at "apache maven" which handles the lifecycle of a project and makes library management easy (believe it or not there is a whole repository with more than 600.000 libraries in it for you to use: http://mvnrepository.com/
And this is just one (although the largest) repository...
Design philosophy is an enormous subject with as many opinions as there are coders. However there are some best practices that everyone adheres to.
Apache usually has pretty high quality code so you can check them if not for code, at least for a good way to write libraries. Other than that I can only point you towards books and google to find your way.
Writing maintainable code is more of an art than a science and it takes a lot of reading and practice to master it.

Using Windows API call in Java using "native"

I've tried to solve this issue by referring possible duplicates but none of them seem to be helpful.
Here's a code that I'm using to call Win API methods in Java to get current Windows User Name, and a native Windows MessageBox, but I'm getting UnsatisfiedLinkError that says that my code is unable to locate the native method I'm trying to call.
public class TestNative
{
public static void main(String[] args)
{
long[] buffer= { 128 };
StringBuffer username = new StringBuffer((int)buffer[0]);
GetUserNameA(username,buffer);
System.out.println("Current User : "+username);
MessageBoxA(0,"UserName : "+username,"Box from Java",0);
}
/** #dll.import("ADVAPI32") */
static native void GetUserNameA(StringBuffer username,long[] buffer);
/** #dll.import("USER32") */
private static native int MessageBoxA(int h,String txt,String title,int style);
}
What can be my possible (relatively simple) solution to call native Windows methods in Java. I realize that it will kill the very reason of Java being a cross-platform language, but I need to work on a project for Windows, to be developed in Java.
Thanks.
Update
As David Heffernan suggested, I've tried changing the method signature of MessageBox to MessageBoxA, but still it's not working.
I would guess it's related to the signatures not matching completely.
The GetUserName function takes two parameters: a LPTSTR and a LPDWORD. Java will likely not handle the StringBuffer acting as a TCHAR array for you.
Also, why bother using the Windows API for this? Java can probably get the user's logon name (quick google says: System.getProperty("user.name")), and Swing can make a message box (even one that looks like a Windows one).
Have you tried https://github.com/twall/jna. I have heard good things and its supposed to make jni that bit easier with many conveniences and simplifications.
Do you have a -Djava.library.path VM arg set with the path to your DLL's? Alternatively, you can have it in your system PATH.
The error is because there is no MessageBox. You presumably mean MessageBoxA.

How can I call a Java object/function from Oracle Forms 6i?

I am working on a legacy project that is using Oracle Forms 6i (yes, I know its old) to call C++ functions from a PLL library.
Now we need to use Java instead of C++, therefore we need to call Java (Object/Class/Method) from Oracle Forms.
I know its a challenging subject, but I would be really happy if someone could provide a simple example that does the following:
Invoking a method from the Java class, passing a int variable (within PL/SQL)
Printing the returned value in the Canvas that executed the Function.
A basic example, perhaps a Hello World would be ideal.
I know some PL/SQL, but I am not a Oracle Forms developer; please bear with me.
If this is not possible, could you point me to some other alternatives?
Well, after an intensive lookup through the internet I came across a very good resource (in Spanish though): Elias blog about Oracle Forms and Java
I use:
Oracle Forms 6i
JDK 1.6
With this I managed to create the hello world example:
Configure PATH environment variables:
C:\PATH_TO_JAVA\Java\jdk1.6.0\bin;
C:\PATH_TO_JAVA\Java\jdk1.6.0\jre\bin;
C:\PATH_TO_JAVA\Java\jdk1.6.0\jre\bin\client;
Ex: PATH_TO_JAVA = C:\Program Files
Add to CLASSPATH
FORMS_HOME\TOOLS\common60\JAVA\IMPORTER.JAR (In my case FORMS_HOME was C:\orant)
PATH_TO_YOUR_JAR\NAME_OF_JAR.jar
Create Java Program
Create with your IDE a simple java program, following is mine:
public class HiWorld{
private String hi="Hello World!";
public String getHi(){
return this.hi;
}
public String getMultiply(int a, int b){
return ""+a*b;
}
public static void main(String args[]){
HiWorld hm = new HiWorld();
System.out.println(hm.getHi());
System.out.println(hm.getMultiply(5,10));
}
}
Export it to Jar file (Path has to be the one you put in CLASSPATH environment variable.
Import the classes to Forms
Create a new project in Oracle Forms and also create a Canvas, in the canvas use a Text and a Button. The name of the button: TEXT_HI_WORLD.
Following click on the Menu: Program > Import Java Classes
If everything went Ok then there will be a new window that will show you the package where the Class is, you extend it until there is the HiWorld class. Import it.
In Program Unit now there will be two files:
HIWORLD (Specification)
HIWORLD (Body)
This are files generated automatically and needed to use the class.
Then go back to the canvas, right click on the Button and select the Thrigger WHEN-BUTTON-PRESSED, the programming of this will be:
DECLARE
v_wb ORA_JAVA.JOBJECT;
v_hi VARCHAR2(20);
BEGIN
v_wb := hiworld.new();
v_hi:= hiworld.getHi(v_wb);
:TEXT_HI_WORLD := v_hi
END;
Now execute the program and click on the button! :)
Hope this helps Java programmers with no much of knowledge on Forms to integrate with legacy systems! :D
I've done this before, and with a simple class this should work, but when you try to develop something more complicated, I recommend extend from the VBean class, you can find the library within oracle's forms installation folders (frmall.jar).
// el programa corregido.
public class HolaMundo {
private String hi= "Hey World!!!";
public String GetHi(){
return this.hi;
}
public static void main(String args[]){
HolaMundo hm = new HolaMundo();
System.out.println(hm.GetHi());
}
}

How can it be determined whether a network adapter is wireless?

There are a number of ways to obtain a machine's network adapters & related info such as Sigar & java.net's getNetworkInterfaces(). However, using either of these means, I am unable to determine whether a certain adapter is wireless (unless the name/description explicitly says so).
Are there any ways to determine this through code? (I'd like to be able to do so in both Windows & Linux, but I am willing to deal with system specific solutions).
Edit: removed the part that is not relevant
maybe you could use the JNI (java native interface) to call a C-function which gets this flag... with C, this should be possible (though possible that the code would become unportable)
Edit: For linux, i found the following. Downloaded the source-code of wireless-tools, including iwconfig. They included library, iwlib.c, simply extracts the names of the interface from /proc/net/wireless or /proc/net/dev
You can get the sources from here, this is from Fedora. As the library extracts its data from a path of a standardized file-system, the only thing you need to have is kernel-support for procfs.
Now i can only lead you to the file "iwlib.c", function
void iw_enum_devices(int skfd, iw_enum_handler fn, char * args[], int count)
i don't know about those parameters, but the source code is commented. Maybe you will have to compare the list you get from java with the one you receive through this JNI-hack...
Guess it's a lot of work for a "little task"; hope you find your way through...
regards
If you want to build a platform specific JNI implementation of your functionality, the Windows API function you can use to get a list of wireless network interfaces is WlanEnumInterfaces in Wlanapi.h. You'll need to link Wlanapi.lib/.dll. Also, see the documentation.
I'd recommend that you build a little JNI library with two functions:
getWirlessInterfaceCount();
getWirelessInterfaceAddr(int nIf, char *addr);
Where you actually make sure the 6 bytes for the address are allocated on the Java side and just filled in on the native side. That way you don't need to worry about memory management on the native side. You can wrap those two JNI calls in a Java method like
List<NetworkInterface> getWirelesNetworkInterfaces();
.... which calls the two JNI methods, gets the list of all interfaces via the NetworkInterface API and throws out all interfaces whose address was not returned by the JNI methods.
Although this question is quite old, someone may be looking for this solution, so here it goes a simple way to find out in Windows.
In the current NetworkInterface class the function getName() will return unique names that can be things like "eth0", "wlan0" or "net0", always few letters plus a number.
After gathering the list of NetworkInterfaces and selecting the real ones you can then check if the getName() function returns "ethX" for a wired interface or "wlanX" for a wireless interface (X is a number that depends on how many interfaces you have in your computer.)
The following code is an example for how you can make this identification, although is necessary firstly to find what interfaces are real, there are many solutions for this.
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
public class TesteInterfaces {
public static void main(String[] args) {
Enumeration<NetworkInterface> tempNetInterface = null;
try {
tempNetInterface = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
ex.printStackTrace();
}
for (NetworkInterface iNet : new ArrayList<>(Collections.list(tempNetInterface))) {
try {
if (iNet.getHardwareAddress() != null) { //This verification might not
//be enought to find if a Interface is real.
System.out.println(iNet.getDisplayName());
if (iNet.getName().contains("wlan")) {
System.out.println("It is WIRELESS");
} else {
System.out.println("It is NOT WIRELESS");
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
}
}
}
An output:
Intel(R) Centrino(R) Wireless-N 100
It is WIRELESS
Realtek PCIe GBE Family Controller
It is NOT WIRELESS

Categories

Resources