通过JNA调用OpenEvent失败

发布于 2024-10-13 08:47:26 字数 1782 浏览 3 评论 0原文

我尝试使用 JNA 调用 kernel32.dllOpenEvent,但失败并出现错误

java.lang.UnsatisfiedLinkError:查找函数“OpenEvent”时出错:找不到指定的过程。

我的存根声明看起来像这样

public static native Pointer OpenEvent(int access, boolean inheritHandle, String name);

有人能帮我找出这里的问题吗?

-- 根据用户反馈进行修改后,我现在没有收到错误;但 OpenEvent 方法总是返回 null。这是演示行为的代码

/** * 你好世界! * */

导入 com.sun.jna.FromNativeContext; 导入com.sun.jna.Native; 导入com.sun.jna.Pointer; 导入com.sun.jna.PointerType;

公开课App { 静止的 { 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");
     }
 };

}

I am trying to call OpenEvent of kernel32.dll using JNA and it fails with the error

java.lang.UnsatisfiedLinkError: Error looking up function 'OpenEvent': The specified procedure could not be found.

My stub declaration looks like this

public static native Pointer OpenEvent(int access, boolean inheritHandle, String name);

Can someone help me identify the issue here?

--
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!
*
*/

import com.sun.jna.FromNativeContext;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;

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");
     }
 };

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

英雄似剑 2024-10-20 08:47:26

不知道 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".

森林散布 2024-10-20 08:47:26

如果您直接映射到 OpenEventW 函数而不使用 JNA 提供的选项,那么您需要使用当前使用 String 的 WString 将 Java String 显式映射到本机 wchar_t* 类型。否则,您将向本机函数传递无效的事件 ID,这可能会导致调用失败。

If you're mapping directly to the OpenEventW function without using the options provided by JNA, then you need to explicitly map the Java String to the native wchar_t* type by using WString where you currently use String. Otherwise you'll be passing invalid event IDs to the native function, which would likely cause the call to fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文