HINSTANCE cast to int?

678 views
Skip to first unread message

FrenKy

unread,
May 1, 2012, 3:51:46 PM5/1/12
to jna-...@googlegroups.com

I want to do following:
            WinDef.HINSTANCE status = Shell32.INSTANCE.ShellExecute(new WinDef.HWND(), "open", "c:\\test.txt", "", null, 1);
           int SE_ERR_NOASSOC=31;
            if ( (int) staus == SE_ERR_NOASSOC) <<===Problem
            status = Shell32.INSTANCE.ShellExecute(new WinDef.HWND(), "openas", "c:\\test.txt", "", null, 1);

MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx) stays following:

Return value

Type: HINSTANCE

If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.

But for the love of me I can't figure out how to do this comparison...
Anyone has any idea?

Kustaa Nyholm

unread,
May 1, 2012, 4:03:39 PM5/1/12
to jna-...@googlegroups.com
On 5/1/12 22:51, "FrenKy" <kfr...@gmail.com> wrote:

>
>I want to do following:
> WinDef.HINSTANCE status = Shell32.INSTANCE.ShellExecute(new
>WinDef.HWND(), "open", "c:\\test.txt", "", null, 1);
> int SE_ERR_NOASSOC=31;
> if ( (int) staus == SE_ERR_NOASSOC) <<===Problem
> status = Shell32.INSTANCE.ShellExecute(new WinDef.HWND(),
>"openas", "c:\\test.txt", "", null, 1);
>
>MSDN
>(http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.8
>5%29.aspx) stays following:
>Return valueType: HINSTANCE
>If the function succeeds, it returns a value greater than 32. If the
>function fails, it returns an error value that indicates the cause of
>the failure. The return value is cast as an HINSTANCE for backward
>compatibility with 16-bit Windows applications. It is not a true
>HINSTANCE, however. It can be cast only to an int and compared to either
>32 or the following error codes below.
>But for the love of me I can't figure out how to do this comparison...
>Anyone has any idea?
>
>

Wild guess after googling for WinDef.HINSTANCE:

HINSTANCE is a HANDLE is a PointerType so you can
getPointer() on it and pass that to the Pointer class static
nativeValue() method? Maybe that returns the int value of
the HINSTANCE...

br Kusti



FrenKy

unread,
May 1, 2012, 6:42:45 PM5/1/12
to jna-...@googlegroups.com
Hey, thanks for the hint, but I've tried that already without success. Perhaps I should have wrote...
If I do getPointer().getInt/Long/NativeLong JNA crashes (in fact complete JVM crashes)...
Here is crash log from NativeLong: http://pastebin.com/XzFRWGLE

Timothy Wall

unread,
May 1, 2012, 7:17:24 PM5/1/12
to jna-...@googlegroups.com

On May 1, 2012, at 6:42 PM, FrenKy wrote:

> Hey, thanks for the hint, but I've tried that already without success.

What exactly didn't work? PointerType.getPointer() provides you with the pointer value, and Pointer.nativeValue(Pointer) returns the value of the pointer as a Java long.


> Perhaps I should have wrote...
> If I do getPointer().getInt/Long/NativeLong JNA crashes (in fact complete JVM crashes)...
> Here is crash log from NativeLong: http://pastebin.com/XzFRWGLE
>
>

These are obviously wrong choices, since they dereference the pointer (as explained in the JavaDoc for those methods). Since the HINSTANCE is not a valid pointer in the first place, it's not surprising that it should cause a crash.

FrenKy

unread,
May 3, 2012, 3:57:37 PM5/3/12
to jna-...@googlegroups.com
Thanks this works as a charm :)


On Tuesday, May 1, 2012 10:03:39 PM UTC+2, nyholku wrote:

FrenKy

unread,
May 3, 2012, 3:59:53 PM5/3/12
to jna-...@googlegroups.com
This actually worked for me (Pointer.nativeValue()), but I'm quite new with JNA and C/C++ world, so a lot to learn.
Thanks for explanation.
Reply all
Reply to author
Forward
0 new messages