Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
214 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...