0

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

}

vinzy
  • 13
  • 3

2 Answers2

0

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".

Luke
  • 11,211
  • 2
  • 27
  • 38
  • JNA typically handles these alternate mappings. – Michael Brewer-Davis Jan 24 '11 at 17:42
  • I dont see the error now when I change the stub to OpenEventA or OpenEventW. But now the method seems to return null on every call. – vinzy Jan 24 '11 at 18:04
  • Are global events accessible in java or does JVM never see it? – vinzy Jan 24 '11 at 18:37
  • If changing the function name helps, I suspect you aren't using the Windows-specific StdCallLibrary properly. Also, you may want a HANDLE return type rather than a Pointer. There's a Kernel32 library in the JNA contrib section that you may want to look at for reference (or extension). – Michael Brewer-Davis Jan 24 '11 at 19:01
  • I updated my code to use a HANDLE in place of Pointer, but still get the same result(null). I have updated the sample code I use in the issue description. – vinzy Jan 24 '11 at 20:40
0

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.

technomage
  • 9,861
  • 2
  • 26
  • 40