JAVA JNA WindowProc实现 [英] JAVA JNA WindowProc implementation

查看:120
本文介绍了JAVA JNA WindowProc实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java编写一个与USB设备通信的简单应用程序。 USB设备由我使用Microchip微控制器制造。通信相当简单,因为USB设备来自HID类,所以在计算机和设备之间交换64字节的数组。
我的程序根据产品ID和供应商ID找到设备,可以写入和读取64个字节,但现在我想检测设备何时连接或断开与计算机的连接。

I'm trying to write a simple application in Java that will communicate with an USB device. The USB device is made by me using a Microchip Microcontroller. The communication is rather simple, since the USB device is from the HID Class, arrays of 64 bytes are exchanged between the computer and the device. My program finds the device based on the product ID and the vendor ID, can write and read 64 bytes, but now I would like to detect when the device is connected or disconnected from the computer.

正如我在Microchip提供的C#程序中看到的那样,WndProc方法被覆盖并处理WM_DEVICECHANGE消息。我的问题是如何使用JNA在Java中完成,如果可以的话,我如何覆盖WindowProc方法并处理消息:),但我希望它是:D

As I've seen in a C# program provided by Microchip as an example application, the WndProc method is overriden and the WM_DEVICECHANGE message is handled. My question is how can this be done in Java using JNA, how can I override the WindowProc Method and handle messages, if this is possible at all :), but I hope it is :D

提前感谢您的回答。

Gabor。

推荐答案

我终于设法解决了问题:)我找到了以下解决方案:

I finally managed to solve the problem :) And I found the following solution:

首先以下列方式扩展User32接口

First extend the User32 interface in the following way

public interface MyUser32 extends User32 {

    public static final MyUser32 MYINSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class, W32APIOptions.UNICODE_OPTIONS);

    /**
     * Sets a new address for the window procedure (value to be set).
     */
    public static final int GWLP_WNDPROC = -4;

    /**
     * Changes an attribute of the specified window
     * @param   hWnd        A handle to the window
     * @param   nIndex      The zero-based offset to the value to be set.
     * @param   callback    The callback function for the value to be set.
     */
    public int SetWindowLong(WinDef.HWND hWnd, int nIndex, Callback callback);
}

然后使用您需要的Windows消息代码扩展WinUser界面这是WM_DEVICECHANGE,因为我想检查我的USB设备是否已连接或与计算机分离。

Then extend the WinUser interface with the Windows Message code that you need, in my case this is the WM_DEVICECHANGE, because I want to check I the USB Device was attached or detached from the computer.

public interface MyWinUser extends WinUser {
    /**
     * Notifies an application of a change to the hardware configuration of a device or the computer.
     */
    public static final int WM_DEVICECHANGE = 0x0219;
}

然后使用回调函数创建一个接口,它实际上是我的WndProc函数。

Then create an interface with the callback function, which will actually be my WndProc function.

//Create the callback interface 
public interface MyListener extends StdCallCallback {

    public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
}

public MyListener listener = new MyListener()
{
    public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam)
    {
        if (uMsg == MyWinUser.WM_DEVICECHANGE)
        {
            // TODO Check If my device was attached or detached
            return new LRESULT(1);
        }
        return new LRESULT(0);
    }
};

然后在初始化事物的JFrame代码中的某处添加窗口过程的新地址使用SetWindowLong函数:

And then somewhere in the code of the JFrame where you initialize things add the new address for the window procedure with the SetWindowLong function:

    // Get Handle to current window
    HWND hWnd = new HWND();
    hWnd.setPointer(Native.getWindowPointer(this));

    MyUser32.MYINSTANCE.SetWindowLong(hWnd, MyUser32.GWLP_WNDPROC, listener);

这段代码很好用,但我对一件事情有些怀疑。我不确定回调函数的返回值是否正确。我在MSDN中读到,在处理WM_DEVICECHANGE消息后,回调函数应该返回true,我不确定我当前返回的值是系统预期的值,所以欢迎任何建议。

This code works nicely, but I have some doubts regarding one thing. I'm not sure if the return value of the callback function is correct. I've read in the MSDN that after handling a WM_DEVICECHANGE message the callback function should return true and I'm not sure that the value i'm currently returning is the one expected by the system, so any suggestions are welcome.

如果有人对我为HID通信编写的整个代码感兴趣,我会非常乐意提供帮助:)

If anyone is interested in the whole code I've written for the HID communication just ask, I would be more than happy to help :)

干杯,
Gabor。

Cheers, Gabor.

这篇关于JAVA JNA WindowProc实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆