Java JNA读取通过窗口消息发送的代码

时间:2015-02-12 20:47:29

标签: java windows-ce jna

我需要在Windows CE设备中读取通过Windows消息使用Java发送的代码,我使用的是JNA,但我没有成功解决此问题。我已经定义了库调用:



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);

}




然后我按照这个例子Clipboard Monitor,但无论如何我只能读取第一条消息,然后当我打电话(coredll)时,TranslateMessage返回" false&#34 ;和DispatchMessage返回" 0"并且它没有给我的CallbackProc打电话,如果有人可以帮助我,我将非常感激。



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));
    }
}




如果有人能给我一个提示,我会非常感激,谢谢。

0 个答案:

没有答案