Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Accessing Windows API

Reply
Thread Tools

Accessing Windows API

 
 
Ken Kast
Guest
Posts: n/a
 
      04-11-2004
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.
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      04-11-2004
Ken Kast wrote:

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

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
 
Ken Kast
Guest
Posts: n/a
 
      04-12-2004
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

Christophe Vanfleteren <(E-Mail Removed)> wrote in message news:<fghec.68098$(E-Mail Removed)-ops.be>...
> Ken Kast wrote:
>
> > 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.

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      04-12-2004
On 12 Apr 2004 07:24:57 -0700, Ken Kast wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Jim Cobban
Guest
Posts: n/a
 
      04-13-2004

"Ken Kast" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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()


 
Reply With Quote
 
Ian Shef
Guest
Posts: n/a
 
      04-14-2004
Gordon Beaton <(E-Mail Removed)> wrote in news:407acb3e$(E-Mail Removed):

<SNIP>
>
> 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



--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
 
Reply With Quote
 
Eugene Toporov
Guest
Posts: n/a
 
      04-15-2004
<SNIP>
>
> 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
 
Reply With Quote
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      04-19-2004
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..

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
windows-api-0.2.0/lib/windows/api.rb:211: [BUG] Segmentation fault aidy.lewis@googlemail.com Ruby 2 10-25-2007 04:27 AM
.Net Profiler API in 64 bit windows -FunctionMapper callback API =?Utf-8?B?TGVv?= Windows 64bit 0 09-05-2007 06:10 PM
Profiling API or Membership API John123 ASP .Net 0 10-20-2006 03:18 PM
Calling the C API from Python and Python program from same C API -bidirectional Praveen, Tayal (IE10) Python 0 03-17-2005 06:33 AM
What API replaces the unlock API that existed in gcc 2.9.3? Shlomo Anglister C++ 1 08-02-2004 06:50 PM



Advertisments