我正在尝试使java程序与第三方DLL(我没有该代码)进行交互,并在Windows Mobile 6.5中接收基于窗口的消息。我正在使用JNA来实现这一目标。我可以毫无问题地执行和读取DLL中其他方法的数据。
在C#中我可以通过扩展MessageWindow类并重写WndProc方法来使其工作
internal class MyClass : MessageWindow
{
protected override void WndProc(ref Message m)
{
if (m.get_Msg() == 74)
{
doSomething();
}
base.WndProc(ref m);
}
}
我在Java中编写了相同的函数,但它无法正常工作
//Finds the pointer to the Frame
INT_PTR winHandle = GetWindowHandle.findWindow(null,"MyFrame");
//Gets a pointer
final LONG_PTR prevWndProc = new LONG_PTR(CoreDll.INSTANCE.GetWindowLong(new HWND(winHandle.toPointer()), -4).longValue());
//CallbackProc implements Callback and StdCallback
CallbackProc ptr=new CallbackProc() {
@Override
public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam,LPARAM lParam) {
if (uMsg == 74)
{
doSomething();
}
return CoreDll.INSTANCE.CallWindowProc(prevWndProc, hWnd, uMsg, wParam, lParam);
}
};
CoreDll.INSTANCE.SetWindowLong(winHandle,-4 ,ptr);
我在网上研究了几个小时,但我没有找到任何帮助。如果有人能给我一些提示,我会非常感激。
最好的问候
修改
我将代码编辑为以下内容:
//Finds the pointer to the Frame
INT_PTR winHandle = GetWindowHandle.findWindow(null,"MyFrame");
//Gets a pointer
final LONG_PTR prevWndProc = new LONG_PTR(CoreDll.INSTANCE.GetWindowLong(new HWND(winHandle.toPointer()), -4).longValue());
HWND Hwnd=new HWND(winHandle.toPointer());
//CallbackProc implements Callback and StdCallback
CallbackProc ptr=new CallbackProc() {
@Override
public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam,LPARAM lParam) {
if (uMsg == 74)
{
doSomething();
}
else if (uMsg == 2828)
{
stop=true;
}
return CoreDll.INSTANCE.CallWindowProc(prevWndProc, hWnd, uMsg, wParam, lParam);
}
};
CoreDll.INSTANCE.SetWindowLong(winHandle,-4 ,ptr);
new Thread() {
public void run() {
fireMsgWndEvent(0,"Run!");
while (!stop) {
try { Thread.sleep(10); } catch(Exception e) { }
}
fireMsgWndEvent(0,"Stopped!");
}
}.start();
int result;
MSG msg = new MSG();
while ((result = CoreDll.INSTANCE.GetMessage(msg, null, 0, 0)) != 0) {
if (result == -1) {
System.err.println("error in get message");
break;
}
else {
System.err.println("got message");
CoreDll.INSTANCE.TranslateMessage(msg);
CoreDll.INSTANCE.DispatchMessage(msg);
}
}
但是仍然没有运气,我没有收到任何消息,请帮助我:'(