I've written the following class to wrap the creation of a win32 event object
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();
}
}
I'm using jna 3.3 on a windows 7 machine, and when I try to create an instance of this class, I get the following stack trace.
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)
I'm really new to JNA, not sure what I'm doing wrong.
I fixed it by changing the code from doing INSTANCE.[method]
with that static variable I defined at the top to instead use kernel32.INSTANCE.[method]
.
I figured it out by looking at the definition of kernel32 and noticing it had that static INSTANCE variable.