

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
9 messages in net.java.dev.jna.usersRe: [jna-users] callback at keyboard ...| From | Sent On | Attachments |
|---|---|---|
| Michele Croci | Nov 5, 2007 12:58 am | |
| Timothy Wall | Nov 5, 2007 5:56 am | |
| Michele Croci | Nov 5, 2007 10:17 am | |
| Timothy Wall | Nov 5, 2007 10:41 am | |
| Timothy Wall | Nov 5, 2007 10:45 am | |
| Michele Croci | Nov 6, 2007 2:45 am | |
| Timothy Wall | Nov 6, 2007 6:58 am | |
| Michele Croci | Nov 7, 2007 2:53 am | |
| Timothy Wall | Nov 7, 2007 6:32 am | .c |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] callback at keyboard event | Actions... |
|---|---|---|
| From: | Michele Croci (mcr...@gmail.com) | |
| Date: | Nov 6, 2007 2:45:59 am | |
| List: | net.java.dev.jna.users | |
Hi Timothy, sorry for annoying you again...
You are right, the crashe happen in the GetMessage instruction, sorry! What could be wrong? - MSG definition - GetMessage specification
- for MSG I have tried using int for WParam and LParam without success. I have tried a lot of combinations but no one worked! - for getmessage I have tried with int/boolean as return type...
I post the relevant part of the code again, maybe you have another suggestion...
C:
BOOL GetMessage( LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax );
typedef struct { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG;
pt Ws POINTAPI
typedef struct { int x; int y; } POINT;
JAVA:
public static class LPARAM extends LONG_PTR { public LPARAM() { this(0); } public LPARAM(long value) { super(value); } } public static class WPARAM extends IntegerType { public WPARAM() { this(0); } public WPARAM(long value) { super(Pointer.SIZE, value); } }
int GetMessage( MSG lpMsg, HWND hWnd, int wMsgFilterMin, int wMsgFilterMax );
public static class MSG extends Structure { public HWND hWnd; public int message; public int wParam; public int lParam; public int time; public POINT pt; }
public static class POINT extends Structure { public int x; public int y; }
Thank you very much again. Best regards,
Michele
2007/11/5, Timothy Wall <twal...@dev.java.net>:
You might also try using "int" in place of WPARAM and LPARAM in the MSG structure.
On Nov 5, 2007, at 1:17 PM, Michele Croci wrote:
Thx again for your kind answer and sorry for the missing info. I have deleted the W-suffix and adopted the DEFAULT_OPTIONS for calling a library. Then I have checked the types, I have played a lot without success.
I don't dispatch the message but the callback is called anyway (I see the output on the console). The JVM crashes when it executes the return-statement of the callback function (but callnexthookex terminates correctly).
I copy my user32 class, maybe you see where the problem is. At the and I copy the JVM error log.
Best Regards,
Michele
C code:
HHOOK SetWindowsHookEx( int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId );
LRESULT CallNextHookEx( HHOOK hhk, int nCode, WPARAM wParam, LPARAM lParam );
LRESULT DispatchMessageW( const MSG *lpmsg );
typedef struct { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG; pt Ws POINTAPI
typedef struct { int x; int y; } POINT;
Java - code:
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, DEFAULT_OPTIONS);
LRESULT CallNextHookEx( HANDLE hhk, int nCode, WPARAM wParam, LPARAM lParam );
int GetAsyncKeyState(int input); int GetCursorPos(POINT p);
HANDLE SetWindowsHookEx( int idHook, StdCallCallback lpfn, HINSTANCE hMod, int dwThreadId );
boolean UnhookWindowsHookEx( HANDLE hhk ); int WH_KEYBOARD_LL = 13; int WH_KEYBOARD = 2; int WH_MOUSE = 7; int WH_MOUSE_LL = 14;
boolean GetMessage( MSG lpMsg, HWND hWnd, int wMsgFilterMin, int wMsgFilterMax );
public static class MSG extends Structure { public HWND hWnd; public int message; public WPARAM wParam; //WPARAM? public LPARAM lParam; //LPARAM? public int time; //long? public POINT pt;
}
public static class POINT extends Structure { public int x; public int y; }
public LRESULT DispatchMessage(MSG msg);
JVM error log:
# # An unexpected error has been detected by Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00386401, pid=5988, tid=6020 # # Java VM: Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode) # Problematic frame: # C 0x00386401 # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp #
--------------- T H R E A D ---------------
Current thread (0x00386400): JavaThread "main" [_thread_in_native, id=6020]
siginfo: ExceptionCode=0xc0000005, writing address 0x7e419233
Registers: EAX=0x00000001, EBX=0x7e4191c6, ECX=0x003ef1c8, EDX=0x0af846c4 ESP=0x003ef208, EBP=0x003ef240, ESI=0x00000100, EDI=0x00000000 EIP=0x00386401, EFLAGS=0x00010202
Top of Stack: (sp=0x003ef208) 0x003ef208: 00000100 003ef240 0af829f8 004a005c 0x003ef218: 00000014 003ef240 003ef270 7c90eae3 0x003ef228: 003ef230 00000024 000d0000 00000100 0x003ef238: 0af829f8 7e41f819 00000047 00000022 0x003ef248: 00000000 0243fc13 00000000 7e4191be 0x003ef258: 7e4191f1 0af84790 00000000 00000000 0x003ef268: 00000000 06d39960 003ef288 10007617 0x003ef278: 0af84790 00000000 00000000 00000000
Instructions: (pc=0x00386401) 0x003863f1: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 40 0x00386401: 19 9b 6d 00 00 00 00 00 00 00 00 00 00 00 00 60
Stack: [0x003a0000,0x003f0000), sp=0x003ef208, free space=316k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C 0x00386401
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) j com.sun.jna.Function.invokeInt(I[Ljava/lang/Object;)I+0 j com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;) Ljava/lang/Object;+309 j com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/ Object;Ljava/util/Map;)Ljava/lang/Object;+197 j com.sun.jna.Library$Handler.invoke (Ljava/lang/Object;Ljava/lang/ reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+303 j $Proxy1.GetMessage(Lmain/User32$MSG;Lwin32/W32API$HWND;II)Z+35 j main.Keyboard.main([Ljava/lang/String;)V+49 v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread ) 0x0adcd000 JavaThread "AWT-Windows" daemon [_thread_in_native, id=6116] 0x0adcc400 JavaThread "Java2D Disposer" daemon [_thread_blocked, id=6100] 0x0aa8b800 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=4588] 0x0aa86c00 JavaThread "CompilerThread0" daemon [_thread_blocked, id=6088] 0x0aa85c00 JavaThread "Attach Listener" daemon [_thread_blocked, id=6052] 0x0aa84c00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=6012] 0x0aa77000 JavaThread "Finalizer" daemon [_thread_blocked, id=12508] 0x0aa72c00 JavaThread "Reference Handler" daemon [_thread_blocked, id=6024] =>0x00386400 JavaThread "main" [_thread_in_native, id=6020]
Other Threads: 0x0aa6e000 VMThread [id=2668] 0x0aa8d000 WatcherThread [id=10696]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap def new generation total 960K, used 650K [0x02960000, 0x02a60000, 0x02e40000) eden space 896K, 65% used [0x02960000, 0x029f2af8, 0x02a40000) from space 64K, 100% used [0x02a50000, 0x02a60000, 0x02a60000) to space 64K, 0% used [0x02a40000, 0x02a40000, 0x02a50000) tenured generation total 4096K, used 212K [0x02e40000, 0x03240000, 0x06960000) the space 4096K, 5% used [0x02e40000, 0x02e75240, 0x02e75400, 0x03240000) compacting perm gen total 12288K, used 4173K [0x06960000, 0x07560000, 0x0a960000) the space 12288K, 33% used [0x06960000, 0x06d73448, 0x06d73600, 0x07560000) No shared spaces configured.
Dynamic libraries: 0x00400000 - 0x00423000 C:\Program Files\Java\jre1.6.0_03\bin \javaw.exe 0x7c900000 - 0x7c9b0000 C:\WINDOWS\system32\ntdll.dll 0x7c800000 - 0x7c8f5000 C:\WINDOWS\system32\kernel32.dll 0x77dd0000 - 0x77e6b000 C:\WINDOWS\system32\ADVAPI32.dll 0x77e70000 - 0x77f01000 C:\WINDOWS\system32\RPCRT4.dll 0x7e410000 - 0x7e4a0000 C:\WINDOWS\system32\USER32.dll 0x77f10000 - 0x77f57000 C:\WINDOWS\system32\GDI32.dll 0x76390000 - 0x763ad000 C:\WINDOWS\system32\IMM32.DLL 0x7c340000 - 0x7c396000 C:\Program Files\Java\jre1.6.0_03\bin \msvcr71.dll 0x6d7c0000 - 0x6da0a000 C:\Program Files\Java\jre1.6.0_03\bin \client\jvm.dll 0x76b40000 - 0x76b6d000 C:\WINDOWS\system32\WINMM.dll 0x6bd00000 - 0x6bd0d000 C:\WINDOWS\system32\SYNCOR11.DLL 0x6d310000 - 0x6d318000 C:\Program Files\Java\jre1.6.0_03\bin \hpi.dll 0x76bf0000 - 0x76bfb000 C:\WINDOWS\system32\PSAPI.DLL 0x6d770000 - 0x6d77c000 C:\Program Files\Java\jre1.6.0_03\bin \verify.dll 0x6d3b0000 - 0x6d3cf000 C:\Program Files\Java\jre1.6.0_03\bin \java.dll 0x6d7b0000 - 0x6d7bf000 C:\Program Files\Java\jre1.6.0_03\bin \zip.dll 0x6d000000 - 0x6d1c3000 C:\Program Files\Java\jre1.6.0_03\bin \awt.dll 0x73000000 - 0x73026000 C:\WINDOWS\system32\WINSPOOL.DRV 0x77c10000 - 0x77c68000 C:\WINDOWS\system32\msvcrt.dll 0x774e0000 - 0x7761d000 C:\WINDOWS\system32\ole32.dll 0x5ad70000 - 0x5ada8000 C:\WINDOWS\system32\uxtheme.dll 0x73760000 - 0x737a9000 C:\WINDOWS\system32\ddraw.dll 0x73bc0000 - 0x73bc6000 C:\WINDOWS\system32\DCIMAN32.dll 0x74720000 - 0x7476b000 C:\WINDOWS\system32\MSCTF.dll 0x755c0000 - 0x755ee000 C:\WINDOWS\system32\msctfime.ime 0x6d3e0000 - 0x6d3e6000 C:\Program Files\Java\jre1.6.0_03\bin \jawt.dll 0x10000000 - 0x10015000 C:\Documents and Settings\crocimi\Local Settings\Temp\jna51371.tmp
VM Arguments: java_command: main.Keyboard Launcher Type: SUN_STANDARD
--------------- S Y S T E M ---------------
OS: Windows XP Build 2600 Service Pack 2
CPU:total 1 (1 cores per cpu, 1 threads per core) family 15 model 2 stepping 9, cmov, cx8, fxsr, mmx, sse, sse2
Memory: 4k page, physical 1046320k(215688k free), swap 2520088k (1750060k free)
vm_info: Java HotSpot(TM) Client VM (1.6.0_03-b05) for windows-x86, built on Sep 24 2007 22:24:33 by "java_re" with unknown MS VC++:1310
The JVM crashed on the following statement: return User32.INSTANCE.CallNextHookEx(handle, code,
wParam, lParam);
2007/11/5, Timothy Wall < twal...@dev.java.net>: See comments below.
On Nov 5, 2007, at 3:58 AM, Michele Croci wrote:
Hi all,
i have taken an example found in the mailing-list archive for a callback for a keyboard event. The example doesn't worked, so I have completed it and now works a little better, but as soon as the callback procedure is called the JVM crashes.
Can you provide the stack dump of the crash?
I tried that example as well, and found that the regular keyboard hook didn't work (which is why it isn't yet in the contrib area of the project). I didn't have time to spend playing around with it to find the problem.
public class Keyboard {
Pointer hinst; KeyboardProc kbListener; Pointer handle;
public Keyboard() { hinst = Kernel32.INSTANCE.GetModuleHandleW(null);
kbListener = new KeyboardProc() { public int callback(int code, WPARAM wParam, LPARAM lParam) { System.out.println("keyboard callback code: " +code + " wParam "+ wParam + " lParam "+lParam );
Is this next statement where the crash occurs? If so, then that is the likely candidate for a) improper calling convention, b) improper argument sizes, or c) invalid parameter values.
return User32.INSTANCE.CallNextHookEx(handle, code, wParam, lParam); } }; handle = User32.INSTANCE.SetWindowsHookExW (User32.WH_KEYBOARD_LL, kbListener, hinst, 0); }
protected void finalize() throws Throwable { User32.INSTANCE.UnhookWindowsHookExW(handle); super.finalize(); }
public static void main(String[] args) { System.out.println("Start."); Keyboard instance = new Keyboard(); BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); try { boolean go = true; while(go){ MSG msg = new MSG(); go = User32.INSTANCE.GetMessageW (msg,null,0,0); //fUser32.INSTANCE.DispatchMessageW(msg);
I assume this is part of debugging, since if you don't dispatch the message, your callback will never be called.
} br.readLine(); } catch( IOException ex ) {} System.out.println("Done."); }
public interface KeyboardProc extends StdCallCallback { int callback(int code, WPARAM wParam, LPARAM lParam); } }
// LPARAM and WAPRAM def:
public static class LPARAM extends LONG_PTR { public LPARAM() { this(0); } public LPARAM(long value) { super(value); } } public static class WPARAM extends IntegerType { public WPARAM() { this(0); } public WPARAM(long value) { super(Pointer.SIZE, value); } }
The function callback is called only one, then the JVM crashes. note that if I call SetWindowsHookExW with WH_KEYBOARD no callback function is called.
This may have to do with whether you use javaw.exe or java.exe, since one is a GUI-based invocation and the latter is a console-based invocation. The console-based java.exe may not set things up properly for handling windows messages. Just a guess, though.
The crash is maybe caused by a incorrect type conversion?
That's usually the problem. Double-check your parameters for the proper sizes. The other problem is often using Library where StdCallLibrary is called for.
Note: If you're going to explicitly use the "W"-suffixed methods, you will need to explicitly use WString where LPTCSTR is requested. The DEFAULT_OPTIONS object in the W32API interface sets things up to automatically map functions to the "W"-suffixed methods, and also automatically convert java.lang.String to the appropriate type. In your case, java.lang.String will be converted to a "char*"-based C string, rather than a "wchar_t*"-based wide string, which is what the "W"-suffixed methods expect.








.c