Discussion:
Defining Pointer INVALID_HANDLE_VALUE correctly for 32 & 64 bit Windows
k***@public.gmane.org
2011-03-31 08:55:21 UTC
Permalink
Windows headers define

#define INVALID_HANDLE_VALUE -1

I have in my code


INVALID_HANDLE_VALUE = new Pointer(0xFFFFFFFF) and this works in that
if a Windows API functions returns invalid handle value then comparison
using equals against my INVALID_HANDLE_VALUE returns true.

So far so good.

I suppose there is no way to define a INVALID_HANDLE_VALUE Pointer that
could be used with == instead of equals ?

Also when I print the Pointers I see that internally the Pointer is 64
bit because if I define Pointer(0xFFFFFFFF) or Pointer(-1) I they are
different. In my 32 WinXP the Pointer value return by the JNA/WIN API
call is internally 0xFFFFFFFF but I suspect that on 64 bit Windows it
would be different...?

So what would be the correct way to define INVALID_HANDLE_VALUE Pointer
in Java/JNA so that it works both on 32 and 64 bit Windows?
Kustaa Nyholm
2011-04-03 07:23:09 UTC
Permalink
Post by k***@public.gmane.org
Windows headers define
#define INVALID_HANDLE_VALUE -1
I have in my code
INVALID_HANDLE_VALUE = new Pointer(0xFFFFFFFF) and this works in that
if a Windows API functions returns invalid handle value then comparison
using equals against my INVALID_HANDLE_VALUE returns true.
So far so good.
I suppose there is no way to define a INVALID_HANDLE_VALUE Pointer that
could be used with == instead of equals ?
Also when I print the Pointers I see that internally the Pointer is 64
bit because if I define Pointer(0xFFFFFFFF) or Pointer(-1) I they are
different. In my 32 WinXP the Pointer value return by the JNA/WIN API
call is internally 0xFFFFFFFF but I suspect that on 64 bit Windows it
would be different...?
So what would be the correct way to define INVALID_HANDLE_VALUE Pointer
in Java/JNA so that it works both on 32 and 64 bit Windows?
Answering (again) my own mail, in the platform.jar this is defined with
something like:


public static class HANDLE extends PointerType {
private boolean immutable;

public HANDLE() {
}

public HANDLE(Pointer p) {
setPointer(p);
immutable = true;
}

public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}

public void setPointer(Pointer p) {
if (immutable) {
throw new UnsupportedOperationException("immutable");
}

super.setPointer(p);
}
}

public static HANDLE INVALID_HANDLE_VALUE = new
HANDLE(Pointer.createConstant(Pointer.SIZE == 8 ? -1 : 0xFFFFFFFFL));



So indeed the HANDLE values returned from Win API calls can be tested for
'handle==INVALID_HANDLE_VALUE' and you don't need to use
'INVALID_HANDLE_VALUE.equals(handle)' .

Great.

br Kusti

Loading...