Accessing Windows API

K

Ken Kast

I have the following code in my Main class:

public class Main {
static {
System.loadLibrary("kernel32");
}
native private static short GetSystemDefaultLangID();

and in main I have

ID = GetSystemDefaultLangID(); // Get Language ID for this system

When it executes this line in debug mode the following exception is
thrown:

java.lang.UnsatisfiedLinkError: GetSystemDefaultLangID
at Main.GetSystemDefaultLangID(Native Method)
at Main.main(Main.java:92)
All the right directories are in the PATH variable.

I am teaching myself Java and this app is a porting of a working one
originally written in VB .NET, so I know the underlying code is OK.

Can anyone give me a hint what I'm doing wrong?

Thanks.
 
C

Christophe Vanfleteren

Ken said:
I have the following code in my Main class:

public class Main {
static {
System.loadLibrary("kernel32");
}
native private static short GetSystemDefaultLangID();

and in main I have

ID = GetSystemDefaultLangID(); // Get Language ID for this system

When it executes this line in debug mode the following exception is
thrown:

java.lang.UnsatisfiedLinkError: GetSystemDefaultLangID
at Main.GetSystemDefaultLangID(Native Method)
at Main.main(Main.java:92)
All the right directories are in the PATH variable.

I am teaching myself Java and this app is a porting of a working one
originally written in VB .NET, so I know the underlying code is OK.

Can anyone give me a hint what I'm doing wrong?

Thanks.

Calling native methods doesn't work like that. You have to adapt/wrap the
native library to be JNI (Java Native Interface) compatible.

Look at http://java.sun.com/docs/books/tutorial/native1.1/ for a tutorial on
JNI.
 
K

Ken Kast

I followed the tutorial you cite in devising the code, as well as
looking at posted examples/questions on the web. I saw examples of
the direct loading of Windows libraries. It makes no sense that one
has to wrap a Windows library in order to use it in Java. Why would I
write C code to wrap C?

Ken
 
G

Gordon Beaton

I followed the tutorial you cite in devising the code, as well as
looking at posted examples/questions on the web. I saw examples of
the direct loading of Windows libraries. It makes no sense that one
has to wrap a Windows library in order to use it in Java. Why would
I write C code to wrap C?

Because you can't invoke arbitrary C functions from java. With JNI you
can call methods that:

- are declared in some java class
- follow a very specific naming and calling convention

For example, do the methods you are trying to call expect a JNIEnv* as
the first argument, or an Object reference as the second argument?

You can write a Java class containing native methods that follow the
JNI convention. From those methods you can call any existing functions
you like.

/gordon
 
J

Jim Cobban

Ken Kast said:
I have the following code in my Main class:

public class Main {
static {
System.loadLibrary("kernel32");
}
native private static short GetSystemDefaultLangID();

and in main I have

ID = GetSystemDefaultLangID(); // Get Language ID for this system

I am presuming that you used this simply as an example of the sort of Win32
function you wished to call. That is because if all you want is the default
language ID for your system it is preferable to get that from the default
Locale object. The implementation of Locale obtains that information from
Windows for you.

import java.util.Locale;
....
String langCode;
langCode = Locale.getDefault().getLanguage()
 
I

Ian Shef

You can write a Java class containing native methods that follow the
JNI convention. From those methods you can call any existing functions
you like.

/gordon

.... or you can purchase a commercial library that provides the wrappers for
you, already made, debugged, and tested. One example is xFunction (
http://www.excelsior-usa.com/xfunction.html ), but there are several others.

.... or you can find a free implementation, such as SWIG ( http://www.swig.org
and especially http://www.swig.org/Doc1.3/Java.html#n2 ).

I have not tried either of these myself.

Try an altavista or google search using:
jni AND java AND wrapper AND windows
 
E

Eugene Toporov

I have not tried either of these myself.

Try an altavista or google search using:
jni AND java AND wrapper AND windows


Do not miss JNIWrapper (http://www.jniwrapper.com)
You may even find that Windpws API functions that you need are already
wrapped inside WinPack (a free addition to JNIWrapper)

Good luck

Eugene Toporov
 
M

mromarkhan

peace.
---HelloWorld.java

public class HelloWorld { public native short displayHelloWorld(); static { System.loadLibrary("hello"); }

}
---

javac HelloWorld.java
javah -jni HelloWorld

---HelloWorldImp.c

#include <stdio.h>#include <stdlib.h>#include <jni.h>#include "HelloWorld.h"
#include <windows.h>
JNIEXPORT jshort JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *env, jobject obj)
{ return GetSystemDefaultLangID();}
---

gcc -g -O2 -c -Ic:/j2sdk1.4.2_03/include -Ic:/j2sdk1.4.2_03/include/win32 -g HelloWorldImp.c
dllwrap --output-def hello.def --add-stdcall-alias -o hello.dll -s HelloWorldImp.o

---Main.java

class Main { public static void main(String[] args) { short value = new HelloWorld().displayHelloWorld();
System.out.println("ID: " +value); }}
---

javac Main.java
java -Djava.library.path=. Main

Have a good day..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top