问题
I need to read codes sent via Windows Messages with Java in a Windows CE device, I'm using JNA but I'm not succeeding with this issue. I've defined the library call:
public interface CoreDll extends StdCallLibrary {
//loads the coredll with unicode options
CoreDll INSTANCE = (CoreDll)Native.loadLibrary("coredll", CoreDll.class,
W32APIOptions.UNICODE_OPTIONS);
//native calls
INT_PTR CreateWindowEx(int dwExStyle, String lpClassName, String lpWindowName,
int dwStyle, int x, int y, int width, int height, INT_PTR hWndParent,
int hMenu, INT_PTR hInstance, String lpParam)throws LastErrorException;
int DefWindowProc(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam);
long SetWindowLong(INT_PTR hWnd,int nIndex,Callback dwNewLong)throws LastErrorException;
INT_PTR CreateEvent(SECURITY_ATTRIBUTES lpEventAttributes,boolean bManualReset, boolean bInitialState, String lpName);
int MsgWaitForMultipleObjectsEx(int nCount, INT_PTR[] lpHandles, boolean fWaitAll, int dwMilliseconds,int dwWakeMask);
int DestroyWindow(INT_PTR hwnd);
boolean PeekMessage(MSG lpMsg, INT_PTR hWnd, int wMsgFilterMin,int wMsgFilterMax, int wRemoveMsg);
boolean TranslateMessage(MSG lpMsg)throws LastErrorException;
int DispatchMessage(MSG lpMsg)throws LastErrorException;
void SendMessage(INT_PTR hWnd, int message, WPARAM wParam, LPARAM lParam);
}
And then I've followed this example Clipboard Monitor, but anyway I'm able to read only the first message, then when I call (coredll)TranslateMessage returns "false" and DispatchMessage returns "0" and it's not calling my CallbackProc, please if anyone can help me I will really appreciate it.
public interface CallbackProc extends Callback, StdCall {
int callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
}
//Create a new native window
INT_PTR m_hwnd=CoreDll.INSTANCE.CreateWindowEx(0, "STATIC", "", 0, 0, 0, 0, 0, null, 0, new INT_PTR(), new String());
//defines the method to replace WndProc
final CallbackProc ptr=new CallbackProc() {
@Override
public int callback(HWND hWnd, int uMsg, WPARAM wParam,LPARAM lParam) {
doStuff();
return CoreDll.INSTANCE.DefWindowProc(hWnd, uMsg, wParam, lParam);
}
};
//Sets the new WndProc to the new window
int num=(int)CoreDll.INSTANCE.SetWindowLong(m_hwnd,-4 ,ptr);
//Sets the handler to the library to send messages to that window
num = (int) theDLL.INSTANCE.RfidSetHwnd(m_hwnd);
//Starts to send messages
num = (int) theDLL.INSTANCE.triggerProcedure();
MSG msg = new MSG();
INT_PTR intermediate_result=CoreDll.INSTANCE.CreateEvent(null, false,false, null);
final INT_PTR handles[] = { intermediate_result };
while (true)
{
int result = CoreDll.INSTANCE.MsgWaitForMultipleObjectsEx(handles.length, handles, false,INFINITE ,QS_ALLINPUT);
if (result == WAIT_OBJECT_0) {
CoreDll.INSTANCE.DestroyWindow(m_hwnd);
break;
}
if (result != WAIT_OBJECT_0 + handles.length) {
// Serious problem, end the thread's run() method!
break;
}
while (CoreDll.INSTANCE.PeekMessage(msg, null, 0, 0, PM_REMOVE)) {
//This always prints 2634 or 2635 I don't know why
System.out.println(msg.message);
CoreDll.INSTANCE.TranslateMessage(msg);
CoreDll.INSTANCE.DispatchMessage(msg);
//Also try this but it doesn't work
//CoreDll.INSTANCE.SendMessage(m_hwnd, msg.message, new WPARAM(msg.wParam), new LPARAM(msg.lParam));
}
}
If someone can give me a hint i will really appreciate it, thanks.
来源:https://stackoverflow.com/questions/28487457/java-jna-read-codes-sent-via-window-messages