gpt4 book ai didi

java - C++ 二进制到 Java 得到 "java.lang.Error: Invalid memory access"

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:27 29 4
gpt4 key购买 nike

我有一个 Java 库(通过 JNA)与 native C++ DLL 连接。此 DLL 提供函数来设置在某些硬件事件发生时调用的回调。

所有这些回调都有效,除了一个回调,尽管它与另一个回调的定义几乎相同。

这些回调签名和枚举在 C++ 代码中定义:

typedef enum _KEYSTATE
{
KEYSTATE_NONE = 0,
KEYSTATE_UP,
KEYSTATE_DOWN,
KEYSTATE_HOLD,
KEYSTATE_INVALID,
} KEYSTATETYPE, *P_KEYSTATETYPE;

typedef enum _DKTYPE
{
DK_NONE = 0,
DK_1,
DK_2,
DK_3,
DK_4,
DK_5,
DK_6,
DK_7,
DK_8,
DK_9,
DK_10,
DK_INVALID,
DK_COUNT = 10
} DKTYPE, *P_DKTYPE;

typedef enum _GESTURETYPE
{
GESTURE_NONE = 0x00000000,
GESTURE_PRESS = 0x00000001,
GESTURE_TAP = 0x00000002,
GESTURE_FLICK = 0x00000004,
GESTURE_ZOOM = 0x00000008,
GESTURE_ROTATE = 0x00000010,
GESTURE_MOVE = 0x00000020,
GESTURE_HOLD = 0x00000040,
GESTURE_RELEASE = 0x00000080,
GESTURE_SCROLL = 0x00000100,
GESTURE_ALL = 0xFFFF
} GESTURETYPE, *P_GESTURETYPE;

typedef enum _EVENTTYPE
{
EVENT_NONE = 0,
EVENT_ACTIVATED,
EVENT_DEACTIVATED,
EVENT_CLOSE,
EVENT_EXIT,
EVENT_INVALID,
} EVENTTYPETYPE, *P_EVENTTYPETYPE;

typedef HRESULT (CALLBACK *DynamicKeyCallbackFunctionType)(DKTYPE, KEYSTATETYPE);
typedef HRESULT (CALLBACK *AppEventCallbackType) (EVENTTYPETYPE, DWORD, DWORD);
typedef HRESULT (CALLBACK *TouchpadGestureCallbackFunctionType)(GESTURETYPE, DWORD, WORD, WORD, WORD);
typedef HRESULT (CALLBACK *KeyboardCallbackFunctionType)(UINT uMsg, WPARAM wParam, LPARAM lParam);

Java 中为回调定义了以下接口(interface):

interface DynamicKeyCallbackFunction extends StdCallLibrary.StdCallCallback {
int callback(int rawDynamicKeyType, int rawDynamicKeyState);
}

interface AppEventCallbackFunction extends StdCallLibrary.StdCallCallback {
int callback(int appEventType, WinDef.UINT dwAppMode, WinDef.UINT dwProcessID);
}

interface TouchpadGestureCallbackFunction extends StdCallLibrary.StdCallCallback {
int callback(int gestureType, WinDef.UINT dwParameters,
WinDef.USHORT wXPos, WinDef.USHORT wYPos, WinDef.USHORT wZPos);
}

interface KeyboardCallbackFunction extends StdCallLibrary.StdCallCallback {
int callback(WinDef.UINT uMsg, WinDef.UINT_PTR wParam, WinDef.INT_PTR lParam);
}

在 API/Library 类/接口(interface)中使用这些函数来设置它们:

// These are defined in an RazerAPI.java file as wrappers to simplify usage
// lib is an instance of a RazerLibrary from JNA

public Hresult RzSBAppEventSetCallback(AppEventCallbackFunction callback) {
return Hresult.getFromApiValue(lib.RzSBAppEventSetCallback(callback));
}

public Hresult RzSBDynamicKeySetCallback(DynamicKeyCallbackFunction callback) {
return Hresult.getFromApiValue(lib.RzSBDynamicKeySetCallback(callback));
}

public Hresult RzSBKeyboardCaptureSetCallback(KeyboardCallbackFunction callback) {
return Hresult.getFromApiValue(lib.RzSBKeyboardCaptureSetCallback(callback));
}

public Hresult RzSBGestureSetCallback(TouchpadGestureCallbackFunction callback) {
return Hresult.getFromApiValue(lib.RzSBGestureSetCallback(callback));
}

// These are the methods in the interface RazerLibrary.java file passed to JNA

int RzSBAppEventSetCallback(RazerAPI.AppEventCallbackFunction callback);
int RzSBDynamicKeySetCallback(RazerAPI.DynamicKeyCallbackFunction callback);
int RzSBKeyboardCaptureSetCallback(RazerAPI.KeyboardCallbackFunction callback);
int RzSBGestureSetCallback(RazerAPI.TouchpadGestureCallbackFunction callback);

注册回调时,会使用类似的东西(经过简化,不会让数百行困惑,文章末尾有完整代码文件的链接)。

public class RazerManager implements DynamicKeyCallbackFunction {
private static DynamicKeyCallbackFunction dkCallback;

private RazerManager() {
dkCallback = this;

RazerAPI api = RazerAPI.INSTANCE;

api.RzSBDynamicKeySetCallback(dkCallback);
}

@Override
public int callback(int type, int state) {
System.out.printf("DK Callback: %s %s", type, state);
return 0; // S_OK
}
}

public class Touchpad implements TouchpadGestureCallbackFunction {
private static TouchpadGestureCallbackFunction gestureCallback;

private Touchpad() {
gestureCallback = this;

RazerAPI api = RazerAPI.INSTANCE;

api.RzSBGestureSetCallback(gestureCallback);
}

@Override
public int callback(int gestureType, UINT param, USHORT x, USHORT y, USHORT z) {
System.out.printf("Gesture: %s", gestureType);
}
}

RazerManager 的实例在用于测试的简单 swing 应用程序的主循环中创建,RazerManager 在其构造函数中创建 Touchpad 的实例。 while 循环用于处理 GetMessage 后跟 Translate 和 DispatchMessage(如果有效)的消息,如下所示:

public class Main {
public static void main(String[] args) throws IOException {
// call javax's invokeLater to run app
}

public Main() {
/* JFrame created here and then shown with setVisible(true); */

RazerManager manager = RazerManager.getInstance();

// Yes this is horrible, blocking everything but it's simply
// used to get messages to go through and trigger the callbacks
WinUser.MSG msg = new WinUser.MSG();
while (true) {
int hasMessage = User32.INSTANCE.GetMessage(msg, null, 0, 0);
if (hasMessage != 0) {
User32.INSTANCE.TranslateMessage(msg);
User32.INSTANCE.DispatchMessage(msg);
}
}
}
}

现在,手势事件处理得很好,Touchpad 类中的回调被调用并正确返回。产生动态按键事件时,while循环处理windows消息内部抛出异常:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:383)
at com.sun.jna.Function.invoke(Function.java:315)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at com.sun.proxy.$Proxy11.DispatchMessage(Unknown Source)
at com.sharparam.jblade.tester.Main.<init>(Main.java:113)
at com.sharparam.jblade.tester.Main$1.run(Main.java:25)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这只发生在动态按键事件中,而不是手势或应用事件。我无法理解为什么,因为动态键和手势回调在 C++ 端非常相似。

编辑:完整代码可以在 GitHub repo 中找到, 具体来说 RazerLibrary.java , RazerAPI.java , RazerManager.javaTouchpad.java . Swing 测试可以作为 gist 找到。 .

最佳答案

所以事实证明你不能(或者至少你必须以与我不同的方式来做)让一个类实现多个回调接口(interface)。创建不同回调接口(interface)的显式实现并将它们分配给 RazerManager 中的回调字段解决了这个问题。

这解释了为什么 Touchpad 中的回调有效但 RazerManager 中的回调无效(Touchpad 实现了一个接口(interface)而 RazerManager 实现了三个)。

演示:

public class MyClass {
private static MyCallbackInterface myCallback;

private MyClass() {
myCallback = new CallbackInterface() {
@Override
public int callback(/* parameters */) {
// Do stuff with data here
return 0;
}
}

nativeLib.SetCallback(myCallback);
}

当单个类实现多个回调接口(interface)时, native 库或 JNA 似乎在某处感到困惑,并且不知道调用哪个回调接口(interface)。所以它随机调用一个(或第一个定义的?)。

关于java - C++ 二进制到 Java 得到 "java.lang.Error: Invalid memory access",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390972/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com