使用windows';使用JNA从java创建CreateEvent()函数

使用windows';使用JNA从java创建CreateEvent()函数,java,winapi,jna,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

我编写了以下类来包装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();
    }
}
我在Windows7机器上使用JNA3.3,当我尝试创建这个类的实例时,我得到以下堆栈跟踪

线程“main”java.lang.UnsatifiedLinkError中出现异常:错误 查找函数“CreateEvent”:无法找到指定的过程 找到了

位于com.sun.jna.Function.(Function.java:179) com.sun.jna.nativellibrary.getFunction(nativellibrary.java:347)位于 com.sun.jna.nativellibrary.getFunction(nativellibrary.java:327)位于 com.sun.jna.Library$Handler.invoke(Library.java:203)位于 $Proxy0.CreateEvent(未知源)位于 Win32Event.(Win32Event.java:23)


我对JNA真的很陌生,不知道我做错了什么。

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


我通过查看kernel32的定义并注意到它有一个静态实例变量,找到了答案。

eventName中的
断言是可疑的。事件可以命名或未命名,但要创建未命名事件,必须向实际的
CreateEvent()
DLL函数传递一个空字符指针。
Kernel32.INSTANCE.CreateEvent()是否解释了这一点?另外,在实际的DLL中,
CreateEvent()
对于Ansi导出为
CreateEventA()
,对于Unicode名称导出为
CreateEventW()
Kernel32.INSTANCE.CreateEvent()
也说明了这一点吗?对于我的用例,我总是希望事件被命名。对于你的其他评论,我只希望他们使用W版本,因为Java的字符串是unicode,Java会自动处理到“ansi”或unicode的映射以及相关的字符串转换;请参阅
w32apoptions.DEFAULT_OPTIONS
,它用于初始化
kernel32
库。