Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

java - Calling OpenEvent fails through JNA

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...