java - 使用 JNA 从 java 使用 Windows 的 CreateEvent() 函数

标签 java winapi jna

我编写了以下类来包装 win32 事件对象的创建

import com.sun.jna.*;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.Kernel32;

/**
 * Wraps a (newly-created) native win32 event object and allows you to signal it.
 * 
 * The created event object is manual-reset and is initially un-set.
 */
public final class Win32Event
{
    private static final Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32", Kernel32.class);

    private WinNT.HANDLE m_handle = null;

    public Win32Event(String in_eventName)
    {
        assert null != in_eventName;

        m_handle = INSTANCE.CreateEvent(null, true, false, in_eventName);

        assert !Pointer.NULL.equals(m_handle.getPointer());
    }

    public void signal()
    {
        assert isValid();

        INSTANCE.SetEvent(m_handle);
    }

    /**
     * @return True if the event handle hasn't been freed with free().
     */
    public boolean isValid()
    {
        return null != m_handle;
    }

    /**
     * Frees the wrapped event handle. This must be done to prevent handle leaks.
     */
    public void free()
    {
        if (isValid())
        {
            INSTANCE.CloseHandle(m_handle);

            m_handle = null;
        }
    }

    @Override
    protected void finalize()
    {
        free();
    }
}

我在 Windows 7 计算机上使用 jna 3.3,当我尝试创建此类的实例时,我得到以下堆栈跟踪。

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'CreateEvent': The specified procedure could not be found.

at com.sun.jna.Function.(Function.java:179) at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347) at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327) at com.sun.jna.Library$Handler.invoke(Library.java:203) at $Proxy0.CreateEvent(Unknown Source) at Win32Event.(Win32Event.java:23)

我对 JNA 很陌生,不确定我做错了什么。

最佳答案

我通过将代码从使用我在顶部定义的静态变量执行 INSTANCE.[method] 更改为使用 kernel32.INSTANCE.[method] 来修复此问题.

我通过查看 kernel32 的定义并注意到它有静态实例变量来弄清楚这一点。

关于java - 使用 JNA 从 java 使用 Windows 的 CreateEvent() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34733606/

相关文章:

windows - 对于超过 32 个内核的系统,如何替换 SetProcessAffinityMask()/GetProcessAffinityMask()?

c - 如何判断套接字是否关闭

c - 是否可以创建像 DrawFocusRect() 这样的 XOR 笔?

Java JNA 将输出重定向到记录器

java - 使用 JNA native 等待调用检测线程中断 (Windows)

java - "global"使用 JNA 的 KeyListener

java - 使用 Hibernate 查询 SQL Server 2005 加密列的 Web 应用程序

java.lang.NoClassDefFoundError,Android Studio 2.6.0,内部类,

java - 在不影响 java swing 中的应用程序的情况下检索 mssql 数据的最佳方法

java - Seam:使用外部 SSO 应用程序登录