- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 JNA 中使用以下 Windows API:
UINT WINAPI GetRawInputDeviceList(
_Out_opt_ PRAWINPUTDEVICELIST pRawInputDeviceList,
_Inout_ PUINT puiNumDevices,
_In_ UINT cbSize
);
UINT cbSize 是 RAWPUTDEVICELIST 结构的大小(以字节为单位)。在JNA中如何知道呢?我无意中发现 16 是一个正确的值。
结构如下:
typedef struct tagRAWINPUTDEVICELIST {
HANDLE hDevice;
DWORD dwType;
} RAWINPUTDEVICELIST, *PRAWINPUTDEVICELIST;
pRawInputDeviceList 是 RAWINPUTDEVICELIST 结构的数组,因此在 JNA 中我声明以下签名:
UINT GetRawInputDeviceList(PointerByReference pRawInputDeviceList, IntByReference puiNumDevices, UINT cbSize);
这是我在 JNA 中的结构:
public static class RawInputDeviceList extends Structure {
public HANDLE hDevice;
public DWORD dwType;
public RawInputDeviceList() {
// required for toArray()
}
public RawInputDeviceList(Pointer pointer) {
super(pointer);
read();
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "hDevice", "dwType" });
}
}
当我获取 PointerByReference 值来获取指针时,它为空,这有什么问题?设备数量是正确的,但我无法正确获取 RawInputDeviceList 数组。
这是完整的测试代码:
public class TestRawInput {
public static void main(String[] args) {
PointerByReference lRawInputDevicesReference = new PointerByReference();
IntByReference lNumDevices = new IntByReference();
UINT lRes = WindowsRawInput.INSTANCE.GetRawInputDeviceList(lRawInputDevicesReference, lNumDevices, new UINT(16));
Pointer lRawInputDevicePointer = lRawInputDevicesReference.getValue();
WindowsRawInput.RawInputDeviceList lRawInputDevice = new WindowsRawInput.RawInputDeviceList(lRawInputDevicePointer);
WindowsRawInput.RawInputDeviceList[] lDevices = (WindowsRawInput.RawInputDeviceList[]) lRawInputDevice.toArray(lNumDevices.getValue());
System.out.println("devices deteced=" + lNumDevices.getValue());
for (int i=0;i<lDevices.length;i++) {
System.out.println("device type: " + lDevices[i].dwType);
}
}
public interface WindowsRawInput extends StdCallLibrary {
WindowsRawInput INSTANCE = (WindowsRawInput) Native.loadLibrary("user32", WindowsRawInput.class, W32APIOptions.DEFAULT_OPTIONS);
UINT GetRawInputDeviceList(PointerByReference pRawInputDeviceList,
IntByReference puiNumDevices, UINT cbSize);
public static class RawInputDeviceList extends Structure {
public HANDLE hDevice;
public DWORD dwType;
public RawInputDeviceList() {
// required for toArray()
}
public RawInputDeviceList(Pointer pointer) {
super(pointer);
read();
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "hDevice", "dwType" });
}
}
}
}
最佳答案
Structure.toArray()
创建连续分配结构的数组。struct*
相当于 struct[]
(原生类型),因此使用 RawInputDeviceList
作为参数类型而不是 通过引用指针
。我不确定他们为什么将单个结构称为“列表”,但这就是适合您的窗口。puiNumDevices
中传递数组的大小, native 函数可能会根据实际填充的计数进行更新。Structure
。cbSize
可以通过调用Structure.size()
获得。关于java - 如何在JNA中填充结构体数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25178236/
我正在使用 JNA 加载 native 库: MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary("mylibrary.so", MyLibrary
任何人都可以为我提供一个 JNA 鼠标钩子(Hook)的工作示例,它能够在我的 Java Swing 应用程序之外跟踪鼠标移动/单击吗? 提前致谢 最佳答案 是的,这里是代码... public cl
我在哪里可以获得支持 ARM64 的 JNA 库? com.sun.jna_4.jar 包含适用于 ARM 的 native libjnidispatch.so 库,但不包含适用于 ARM64 的库。
我正在尝试使用 JNA,因为我想使用一个用 c++ 编写的 .dll,而我的其余代码是用 Java 编写的。但是,如果我尝试导入 JNA 类 Eclipse 声称“com.sun.jna.Librar
我使用以下代码从java程序控制Windows服务 public class PostgresService2 { public static void main(String[] args)
我有一个用于 C 函数 mpv_set_option_string 的 JNA Java 接口(interface),定义为: public interface MPV extends StdCall
我正在查看中央 Maven 存储库,并看到一个 net.java.dev.jna 和一个用于 JNA 的 com.sun.jna groupId。 JNA 的 github,使用 com.sun.jn
我已经开始构建一个 java 服务,它结合了 JNA 来加载 native C/C++ 库,我想在 Raspberry PI aka arm 平台上运行 java 服务。我已经成功地建立了一个稳定的服
我要离开 this如何传递 com.sun.jna.Structure 的示例包含 com.sun.jna.StringArray使用 JNA 从 Java 转换为 native C 代码,并且无法在
我正在准备 spring-boot 应用程序。然后我得到以下错误: JNA not found. native methods will be disabled. java.lang.ClassNot
遇到令人抓狂的阻塞错误: Exception in thread "main" java.lang.Error: Structure.getFieldOrder() on class com.luke
我已经下载并复制 Jna.jar 和 Platform.jar 到/usr/cassandra/apache-cassandra-1.0.7/lib 文件夹,但在 Cassandra 启动时仍然看到以
我有一个像这样的 JNA 库 stub : public interface FREngine extends Library { NativeLibrary JNA_NATIVE_LIB =
我想使用 JNAerator 为 JNA 生成一些 C++ API 函数的 Java 包装器。问题是我无法在工作时使用 JNAerator Studio 或从我的 PC 访问 Github。我只有 G
在 JNA 中,如何从 Xlib 映射联合结构,如以下 XEvent typedef union _XEvent { int type; /* must not be changed *
我正在尝试使用 JNA 从 java 运行 ShellExecute 函数。我在非 unicode 文件夹上运行 ShellExecuteA 没有任何问题 import com.sun.jna.*;
在弄清楚如何将 ClientToScreen winapi 函数与 JNA 结合使用时遇到问题。 我仍然得到窗口句柄坐标的 0, 0 输出。我引用了这个,但我确信我做得不对 https://msdn.
我有两个局部变量: Pointer output; int output_len; /* or better `size_t output_len;` */ 我需要将这些变量的指针传递到 JNA 函数
我有一个openvr binding有一段时间以来有一个小问题 基本上,每当我释放某些几何 3D 模型(基站或 Controller )的内存时,我都会时不时地收到错误: "java.lang.Err
我是 JNA 新手,我想将 DsGetDcName 方法转换为 JNA。 DWORD DsGetDcName( __in LPCTSTR ComputerName, __in LPCT
我是一名优秀的程序员,十分优秀!