问题描述
我正在尝试使用JNA调用kernel32.dll
的OpenEvent
,但失败并显示错误
I am trying to call OpenEvent
of kernel32.dll
using JNA and it fails with the error
我的存根声明如下
public static native Pointer OpenEvent(int access, boolean inheritHandle, String name);
有人可以帮我在这里找到问题吗?
Can someone help me identify the issue here?
-根据用户反馈进行修改后,我现在没有收到错误;但OpenEvent方法始终返回null.这是演示行为的代码
--After making modification based on users feedback I dont get the error now; but OpenEvent method always returns null. This is the code that demonstrates the behavior
/** * 你好,世界! * */
/** * Hello world! * */
导入com.sun.jna.FromNativeContext;导入com.sun.jna.Native;导入com.sun.jna.Pointer;导入com.sun.jna.PointerType;
import com.sun.jna.FromNativeContext;import com.sun.jna.Native;import com.sun.jna.Pointer;import com.sun.jna.PointerType;
公共类应用{ 静态的 { Native.register("kernel32"); }
public class App{ static { Native.register("kernel32"); }
public static native HANDLE OpenEventW(int access, boolean inheritHandle,
String name);
public static native HANDLE CreateEventW(Pointer securityAttributes,
boolean manualReset, boolean initialState, String name);
public static native int GetLastError();
public static void main( String[] args )
{
HANDLE i = CreateEventW(null,false,false,"Global\\testEvent");
System.out.println("After create event:"+GetLastError());
HANDLE j = OpenEventW(100000, false, "Global\\testEvent");
System.out.println("After open event:"+GetLastError());
}
public static class HANDLE extends PointerType {
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}
static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{ super.setPointer(Pointer.createConstant(-1)); }
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
}
推荐答案
不知道什么是JNA或JNA的工作原理,但问题可能是实际导出的函数不是"OpenEvent".是"OpenEventA"还是"OpenEventW",具体取决于您是否要使用ASCII或Unicode变体.我假设Java字符串是Unicode,所以您很可能希望使用"OpenEventW".
No idea what JNA is or how it works, but the problem is likely that the actual exported function is NOT "OpenEvent". It is "OpenEventA" or "OpenEventW" depending on if you want toe ASCII or Unicode variant. I assume Java strings are Unicode, so you most likely want "OpenEventW".
这篇关于通过JNA调用OpenEvent失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!