----- Original Message -----
From: "Wayne Meissner" <wmei...@gmail.com>
To: <use...@jna.dev.java.net>
Sent: Wednesday, August 22, 2007 4:53 AM
Subject: Re: [jna-users] Accessing extern variables with JNA
Albert Strasheim wrote:
Hello all
I recently started using JNA and I'm seriously impressed.
I was wondering if JNA has a way to access extern variables in a DLL?
There variables are typically declared as extern __declspec(dllexport)
on Windows and simply as extern on other platforms.
Various C libraries I've encounted make use of these variables as a
way of providing default flags without using a #define (which would
cause the defaults to be compiled into the program linking against the
library, which could lead to problems if the library is upgraded).
At least on linux (and presumably macosx), you can just get the address of
the library variable via NativeLibrary.getFunction:
Pointer p_errno = NativeLibrary.getInstance("c").getFunction("errno")
System.out.println("errno=" + p_errno.getInt(0));
Its not pretty, but it works.
Not sure how or if that would work on windows though.
I also discovered this solution after looking at what ctypes does. Fetching
ints works on 32-bit Windows using jnalib-v3.
ctypes uses dlsym/GetProcAddress for functions and variables, which seems to
be what JNA also does.
JNA could presumably encapsulate that via something like:
interface Libc extends Library {
static Libc INSTANCE = ...;
static GlobalVariable<Integer> errno;
}
That would be very useful. Should I file a feature request?
For reference, here is what the ctypes API looks like:
http://docs.python.org/lib/ctypes-accessing-values-exported-from-dlls.html
Cheers,
Albert