com.sun.jna.Function类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(346)

本文整理了Java中com.sun.jna.Function类的一些代码示例,展示了Function类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Function类的具体详情如下:
包路径:com.sun.jna.Function
类名称:Function

Function介绍

[英]An abstraction for a native function pointer. An instance of Function represents a pointer to some native function. #invoke(Class,Object[],Map) is the primary means to call the function.
Function call behavior may be modified by passing one of the following call flags:

  • Function#C_CONVENTION Use C calling convention (default)
  • Function#ALT_CONVENTION Use alternate calling convention (e.g. stdcall)
  • Function#THROW_LAST_ERROR Throw a LastErrorException if the native function sets the system error to a non-zero value (errno or GetLastError). Setting this flag will cause the system error to be cleared prior to native function invocation.
    [中]本机函数指针的抽象。Function的实例表示指向某个本机函数的指针#调用(类、对象[]、映射)是调用函数的主要方法。
    可以通过传递以下调用标志之一来修改函数调用行为:
    *函数#C#约定使用C调用约定(默认)
    *函数#ALT#U约定使用备用调用约定(例如stdcall)
    *如果本机函数将系统错误设置为非零值(errno或GetLastError),则函数#THROW#u LAST_ERROR抛出LastErrorException。设置此标志将导致在本机函数调用之前清除系统错误。

代码示例

代码示例来源:origin: net.java.dev.jna/jna

/**
 * Obtain a <code>Function</code> representing a native
 * function pointer.  In general, this function should be used by dynamic
 * languages; Java code should allow JNA to bind to a specific Callback
 * interface instead by defining a return type or Structure field type.
 *
 * <p>The allocated instance represents a pointer to the native
 * function pointer.
 *
 * @param   p       Native function pointer
 */
public static Function getFunction(Pointer p) {
  return getFunction(p, 0, null);
}

代码示例来源:origin: net.java.dev.jna/jna

/** Chain invocation to the native function. */
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  if (Library.Handler.OBJECT_TOSTRING.equals(method)) {
    String str = "Proxy interface to " + function;
    Method m = (Method)options.get(Function.OPTION_INVOKING_METHOD);
    Class<?> cls = findCallbackClass(m.getDeclaringClass());
    str += " (" + cls.getName() + ")";
    return str;
  } else if (Library.Handler.OBJECT_HASHCODE.equals(method)) {
    return Integer.valueOf(hashCode());
  } else if (Library.Handler.OBJECT_EQUALS.equals(method)) {
    Object o = args[0];
    if (o != null && Proxy.isProxyClass(o.getClass())) {
      return Function.valueOf(Proxy.getInvocationHandler(o) == this);
    }
    return Boolean.FALSE;
  }
  if (Function.isVarArgs(method)) {
    args = Function.concatenateVarArgs(args);
  }
  return function.invoke(method.getReturnType(), args, options);
}

代码示例来源:origin: net.java.dev.jna/jna

/**
 * Call the native function being represented by this object
 *
 * @param   callFlags calling convention to be used
 * @param    args
 *            Arguments to pass to the native function
 * @param   wide whether the native string uses <code>wchar_t</code>;
 * if false, <code>char</code> is assumed
 * @return    The value returned by the target native function, as a String
 */
private String invokeString(int callFlags, Object[] args, boolean wide) {
  Pointer ptr = invokePointer(callFlags, args);
  String s = null;
  if (ptr != null) {
    if (wide) {
      s = ptr.getWideString(0);
    }
    else {
      s = ptr.getString(0, encoding);
    }
  }
  return s;
}

代码示例来源:origin: net.java.dev.jna/jna

result = null;
} else if (returnType==boolean.class || returnType==Boolean.class) {
  result = valueOf(Native.invokeInt(this, this.peer, callFlags, args) != 0);
} else if (returnType==byte.class || returnType==Byte.class) {
  result = Byte.valueOf((byte)Native.invokeInt(this, this.peer, callFlags, args));
  result = Double.valueOf(Native.invokeDouble(this, this.peer, callFlags, args));
} else if (returnType==String.class) {
  result = invokeString(callFlags, args, false);
} else if (returnType==WString.class) {
  String s = invokeString(callFlags, args, true);
  if (s != null) {
    result = new WString(s);
  return invokePointer(callFlags, args);
} else if (Structure.class.isAssignableFrom(returnType)) {
  if (Structure.ByValue.class.isAssignableFrom(returnType)) {
    result = s;
  } else {
    result = invokePointer(callFlags, args);
    if (result != null) {
      Structure s = Structure.newInstance((Class<? extends Structure>)returnType, (Pointer)result);
  result = invokePointer(callFlags, args);
  if (result != null) {
    result = CallbackReference.getCallback(returnType, (Pointer)result);
  Pointer p = invokePointer(callFlags, args);

代码示例来源:origin: net.java.dev.jna/jna-platform

protected int _invokeNativeInt(int vtableId, Object[] args) {
  Pointer vptr = this.getPointer().getPointer(0);
  // we take the vtable id and multiply with the pointer size (4 bytes on
  // 32bit OS)
  Function func = Function.getFunction(vptr.getPointer(vtableId
      * Native.POINTER_SIZE));
  return func.invokeInt(args);
}

代码示例来源:origin: net.java.dev.jna/jna-platform

protected Object _invokeNativeObject(int vtableId, Object[] args, Class<?> returnType) {
  Pointer vptr = this.getPointer().getPointer(0);
  // we take the vtable id and multiply with the pointer size (4 bytes on
  // 32bit OS)
  Function func = Function.getFunction(vptr.getPointer(vtableId
      * Native.POINTER_SIZE));
  return func.invoke(returnType, args);
}

代码示例来源:origin: net.java.dev.jna/jna-platform

protected void _invokeNativeVoid(int vtableId, Object[] args) {
  Pointer vptr = this.getPointer().getPointer(0);
  // we take the vtable id and multiply with the pointer size (4 bytes on
  // 32bit OS)
  Function func = Function.getFunction(vptr.getPointer(vtableId
      * Native.POINTER_SIZE));
  func.invokeVoid(args);
}

代码示例来源:origin: net.java.dev.jna/jna-platform

@Override
public int AddRef() {
  Function f = Function.getFunction(vTable[1], Function.ALT_CONVENTION);
  return f.invokeInt(new Object[]{interfacePointer});
}

代码示例来源:origin: net.java.dev.jna/jna

/**
 * Call the native function being represented by this object
 *
 * @param    args
 *            Arguments to pass to the native function
 */
public void invoke(Object[] args) {
  invoke(Void.class, args);
}

代码示例来源:origin: org.netbeans.api/org-jruby

public final IRubyObject invoke(Ruby runtime, Function function, Object[] args) {
  Pointer address = function.invokePointer(args);
  if (address == null) {
    return runtime.getNil();
  }
  int len = (int) address.indexOf(0, (byte) 0);
  if (len == 0) {
    return RubyString.newEmptyString(runtime);
  }
  ByteList bl = new ByteList(len);
  bl.length(len);
  address.read(0, bl.unsafeBytes(), bl.begin(), len);
  
  return RubyString.newString(runtime, bl);
}
public static final FunctionInvoker INSTANCE = new StringInvoker();

代码示例来源:origin: com.sun.jna/jna

Object result = null;
if (returnType == null || returnType==void.class || returnType==Void.class) {
  invokeVoid(callingConvention, args);
  result = null;
  result = Boolean.valueOf(invokeInt(callingConvention, args) != 0);
  result = new Character((char)invokeInt(callingConvention, args));
  result = new Integer(invokeInt(callingConvention, args));
  result = new Long(invokeLong(callingConvention, args));
  result = new Float(invokeFloat(callingConvention, args));
  result = new Double(invokeDouble(callingConvention, args));
  if (Structure.ByValue.class.isAssignableFrom(returnType)) {
    Structure s = 
      invokeStructure(callingConvention, args, 
              Structure.newInstance(returnType));
    s.read();
    result = invokePointer(callingConvention, args);
    if (result != null) {
      Structure s = Structure.newInstance(returnType);
  result = invokePointer(callingConvention, args);
  if (result != null) {

代码示例来源:origin: net.java.dev.jna/jna

boolean isVarArgs = args.length > 0 && invokingMethod != null ? isVarArgs(invokingMethod) : false;
int fixedArgs = args.length > 0 && invokingMethod != null ? fixedArgs(invokingMethod) : 0;
for (int i=0; i < args.length; i++) {
  Class<?> paramType = invokingMethod != null
      : paramTypes[i])
    : null;
  args[i] = convertArgument(args, i, invokingMethod, mapper, allowObjects, paramType);
Object result = invoke(args, nativeReturnType, allowObjects, fixedArgs);

代码示例来源:origin: net.java.dev.jna/jna

/**
 * Obtain a <code>Function</code> representing a native
 * function pointer.  In general, this function should be used by dynamic
 * languages; Java code should allow JNA to bind to a specific Callback
 * interface instead by defining a return type or Structure field type.
 *
 * <p>The allocated instance represents a pointer to the native
 * function pointer.
 *
 * @param   p
 *                  Native function pointer
 * @param   callFlags
 *                  Function <a href="#callflags">call flags</a>
 * @param   encoding
 *                  Encoding to use for conversion between Java and native
 *                  strings.
 */
public static Function getFunction(Pointer p, int callFlags, String encoding) {
  return new Function(p, callFlags, encoding);
}

代码示例来源:origin: net.java.dev.jna/jna

if (arg == null || isPrimitiveArray(arg.getClass())) {
  return arg;
    if (!Structure.ByReference[].class.isAssignableFrom(expectedType)) {
      if (byRef) {
        throw new IllegalArgumentException("Function " + getName()
                          + " declared Structure[] at parameter "
                          + index + " but array of "
          throw new IllegalArgumentException("Function " + getName()
                            + " declared Structure[] at parameter "
                            + index + " but element " + i
                    + arg.getClass().getName()
                    + " at parameter " + index
                    + " of function " + getName());

代码示例来源:origin: net.java.dev.jna/jna

/** Looks up the method name by adding a "W" or "A" suffix as appropriate. 
   */
  public String getFunctionName(NativeLibrary library, Method method) {
    String name = method.getName();
    if (!name.endsWith("W") && !name.endsWith("A")) {
      try {
        name = library.getFunction(name + suffix, StdCallLibrary.STDCALL_CONVENTION).getName();
      }
      catch(UnsatisfiedLinkError e) {
        // ignore and let caller use undecorated name
      }
    }
    return name;
  }
}

代码示例来源:origin: org.netbeans.api/org-jruby

public final IRubyObject invoke(Ruby runtime, Function function, Object[] args) {
  return new JNAMemoryPointer(runtime, function.invokePointer(args));
}
public static final FunctionInvoker INSTANCE = new PointerInvoker();

代码示例来源:origin: com.sun.jna/jna

Method invokingMethod = (Method)options.get(OPTION_INVOKING_METHOD);
for (int i=0; i < args.length; i++) {
  args[i] = convertArgument(args, i, invokingMethod, mapper);

代码示例来源:origin: com.sun.jna/jna

if (arg == null || isPrimitiveArray(arg.getClass())) { 
  return arg;

代码示例来源:origin: net.java.dev.jna/platform

/**
   * Release.
   * 
   * @return the ulong
   */
  public int Release() {
    Pointer vptr = this.getPointer().getPointer(0);
    Function func = Function.getFunction(vptr.getPointer(8));

    return func.invokeInt(new Object[] { this.getPointer() });
  }
}

代码示例来源:origin: org.elasticsearch/jna

result = null;
} else if (returnType==boolean.class || returnType==Boolean.class) {
  result = valueOf(Native.invokeInt(this, this.peer, callFlags, args) != 0);
} else if (returnType==byte.class || returnType==Byte.class) {
  result = Byte.valueOf((byte)Native.invokeInt(this, this.peer, callFlags, args));
  result = Double.valueOf(Native.invokeDouble(this, this.peer, callFlags, args));
} else if (returnType==String.class) {
  result = invokeString(callFlags, args, false);
} else if (returnType==WString.class) {
  String s = invokeString(callFlags, args, true);
  if (s != null) {
    result = new WString(s);
  return invokePointer(callFlags, args);
} else if (Structure.class.isAssignableFrom(returnType)) {
  if (Structure.ByValue.class.isAssignableFrom(returnType)) {
    result = s;
  } else {
    result = invokePointer(callFlags, args);
    if (result != null) {
      Structure s = Structure.newInstance(returnType, (Pointer)result);
  result = invokePointer(callFlags, args);
  if (result != null) {
    result = CallbackReference.getCallback(returnType, (Pointer)result);
  Pointer p = invokePointer(callFlags, args);

相关文章