Class DynCall



  • public class DynCall
    extends java.lang.Object
    Native bindings to \"dyncall.h\".

    The dyncall library encapsulates architecture-, OS- and compiler-specific function call semantics in a virtual "bind argument parameters from left to right and then call" interface allowing programmers to call C functions in a completely dynamic manner.

    In other words, instead of calling a function directly, the dyncall library provides a mechanism to push the function parameters manually and to issue the call afterwards.

    Since the idea behind this concept is similar to call dispatching mechanisms of virtual machines, the object that can be dynamically loaded with arguments, and then used to actually invoke the call, is called CallVM. It is possible to change the calling convention used by the CallVM at run-time. Due to the fact that nearly every platform comes with one or more distinct calling conventions, the dyncall library project intends to be a portable and open-source approach to the variety of compiler-specific binary interfaces, platform specific subtleties, and so on...

    • Field Detail

      • DC_CALL_C_DEFAULT

        public static final int DC_CALL_C_DEFAULT
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ELLIPSIS

        public static final int DC_CALL_C_ELLIPSIS
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ELLIPSIS_VARARGS

        public static final int DC_CALL_C_ELLIPSIS_VARARGS
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_CDECL

        public static final int DC_CALL_C_X86_CDECL
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_WIN32_STD

        public static final int DC_CALL_C_X86_WIN32_STD
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_WIN32_FAST_MS

        public static final int DC_CALL_C_X86_WIN32_FAST_MS
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_WIN32_FAST_GNU

        public static final int DC_CALL_C_X86_WIN32_FAST_GNU
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_WIN32_THIS_MS

        public static final int DC_CALL_C_X86_WIN32_THIS_MS
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_WIN32_THIS_GNU

        public static final int DC_CALL_C_X86_WIN32_THIS_GNU
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X64_WIN64

        public static final int DC_CALL_C_X64_WIN64
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X64_SYSV

        public static final int DC_CALL_C_X64_SYSV
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_PPC32_DARWIN

        public static final int DC_CALL_C_PPC32_DARWIN
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_PPC32_OSX

        public static final int DC_CALL_C_PPC32_OSX
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ARM_ARM_EABI

        public static final int DC_CALL_C_ARM_ARM_EABI
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ARM_THUMB_EABI

        public static final int DC_CALL_C_ARM_THUMB_EABI
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ARM_ARMHF

        public static final int DC_CALL_C_ARM_ARMHF
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_MIPS32_EABI

        public static final int DC_CALL_C_MIPS32_EABI
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_PPC32_SYSV

        public static final int DC_CALL_C_PPC32_SYSV
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_PPC32_LINUX

        public static final int DC_CALL_C_PPC32_LINUX
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ARM_ARM

        public static final int DC_CALL_C_ARM_ARM
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ARM_THUMB

        public static final int DC_CALL_C_ARM_THUMB
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_MIPS32_O32

        public static final int DC_CALL_C_MIPS32_O32
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_MIPS64_N32

        public static final int DC_CALL_C_MIPS64_N32
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_MIPS64_N64

        public static final int DC_CALL_C_MIPS64_N64
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_X86_PLAN9

        public static final int DC_CALL_C_X86_PLAN9
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_SPARC32

        public static final int DC_CALL_C_SPARC32
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_SPARC64

        public static final int DC_CALL_C_SPARC64
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_ARM64

        public static final int DC_CALL_C_ARM64
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_PPC64

        public static final int DC_CALL_C_PPC64
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_C_PPC64_LINUX

        public static final int DC_CALL_C_PPC64_LINUX
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_SYS_DEFAULT

        public static final int DC_CALL_SYS_DEFAULT
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_SYS_X86_INT80H_LINUX

        public static final int DC_CALL_SYS_X86_INT80H_LINUX
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_SYS_X86_INT80H_BSD

        public static final int DC_CALL_SYS_X86_INT80H_BSD
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_SYS_PPC32

        public static final int DC_CALL_SYS_PPC32
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_CALL_SYS_PPC64

        public static final int DC_CALL_SYS_PPC64
        Supported calling convention modes.
        See Also:
        Constant Field Values
      • DC_ERROR_UNSUPPORTED_MODE

        public static final int DC_ERROR_UNSUPPORTED_MODE
        Error codes.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_UCHAR

        public static final char DC_SIGCHAR_UCHAR
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_SHORT

        public static final char DC_SIGCHAR_SHORT
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_USHORT

        public static final char DC_SIGCHAR_USHORT
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_ULONG

        public static final char DC_SIGCHAR_ULONG
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_LONGLONG

        public static final char DC_SIGCHAR_LONGLONG
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_ULONGLONG

        public static final char DC_SIGCHAR_ULONGLONG
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_FLOAT

        public static final char DC_SIGCHAR_FLOAT
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_DOUBLE

        public static final char DC_SIGCHAR_DOUBLE
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_POINTER

        public static final char DC_SIGCHAR_POINTER
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_STRING

        public static final char DC_SIGCHAR_STRING
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_STRUCT

        public static final char DC_SIGCHAR_STRUCT
        Signatures.
        See Also:
        Constant Field Values
      • DC_SIGCHAR_ENDARG

        public static final char DC_SIGCHAR_ENDARG
        Signatures.
        See Also:
        Constant Field Values
    • Method Detail

      • dcNewCallVM

        public static long dcNewCallVM(long size)
        Creates a new CallVM object.

        Use Free to destroy the CallVM object.

        Parameters:
        size - the max size of the internal stack that will be allocated and used to bind arguments to
      • dcFree

        public static void dcFree(long vm)
        Frees a CallVM object.
        Parameters:
        vm - a CallVM instance
      • dcReset

        public static void dcReset(long vm)
        Resets the internal stack of arguments and prepares it for a new call. This function should be called after setting the call mode (using Mode), but prior to binding arguments to the CallVM. Use it also when reusing a CallVM, as arguments don’t get flushed automatically after a function call invocation.

        Note: you should also call this function after initial creation of the a CallVM object, as NewCallVM doesn’t do this, implicitly.

        Parameters:
        vm - a CallVM instance
      • dcArgBool

        public static void dcArgBool(long vm,
                                     int value)
        Binds a bool argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgChar

        public static void dcArgChar(long vm,
                                     byte value)
        Binds a char argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgShort

        public static void dcArgShort(long vm,
                                      short value)
        Binds a short argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgInt

        public static void dcArgInt(long vm,
                                    int value)
        Binds an int argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgLong

        public static void dcArgLong(long vm,
                                     int value)
        Binds a long argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgLongLong

        public static void dcArgLongLong(long vm,
                                         long value)
        Binds a long long argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgFloat

        public static void dcArgFloat(long vm,
                                      float value)
        Binds a float argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgDouble

        public static void dcArgDouble(long vm,
                                       double value)
        Binds a double argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgPointer

        public static void dcArgPointer(long vm,
                                        long value)
        Binds a pointer argument.
        Parameters:
        vm - a CallVM instance
        value - the argument value
      • dcArgStruct

        public static void dcArgStruct(long vm,
                                       long s,
                                       long value)
        Binds a struct argument.
        Parameters:
        vm - a CallVM instance
        s -
        value - the argument value
      • dcCallVoid

        public static void dcCallVoid(long vm,
                                      long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallBool

        public static int dcCallBool(long vm,
                                     long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallChar

        public static byte dcCallChar(long vm,
                                      long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallShort

        public static short dcCallShort(long vm,
                                        long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallInt

        public static int dcCallInt(long vm,
                                    long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallLong

        public static int dcCallLong(long vm,
                                     long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallLongLong

        public static long dcCallLongLong(long vm,
                                          long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallFloat

        public static float dcCallFloat(long vm,
                                        long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallDouble

        public static double dcCallDouble(long vm,
                                          long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallPointer

        public static long dcCallPointer(long vm,
                                         long funcptr)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
      • dcCallStruct

        public static void dcCallStruct(long vm,
                                        long funcptr,
                                        long s,
                                        long returnValue)
        Calls the function specified by funcptr with the arguments bound to the CallVM and returns.

        After the invocation of the foreign function call, the argument values are still bound and a second call using the same arguments can be issued. If you need to clear the argument bindings, you have to reset the CallVM.

        Parameters:
        vm - a CallVM instance
        funcptr - the function pointer
        s -
        returnValue -
      • dcGetError

        public static int dcGetError(long vm)
        Returns the most recent error state code.
        Parameters:
        vm - a CallVM instance
      • dcNewStruct

        public static long dcNewStruct(long fieldCount,
                                       int alignment)
        Creates a new struct type.
        Parameters:
        fieldCount - the number of fields
        alignment - a custom struct alignment, or 0 to calculate automatically
      • dcStructField

        public static void dcStructField(long s,
                                         int type,
                                         int alignment,
                                         long arrayLength)
        Adds a field to the specified struct.
        Parameters:
        s - the struct
        type - the field type
        alignment - a custom field alignment, or 0 to calculate automatically
        arrayLength - 1 or a higher value if the field is an array
      • dcSubStruct

        public static void dcSubStruct(long s,
                                       long fieldCount,
                                       int alignment,
                                       long arrayLength)
        Adds a nested struct to the specified struct
        Parameters:
        s - the struct
        fieldCount - the number of fields in the nested struct
        alignment - a custom nested struct alignment, or 0 to calculate automatically
        arrayLength - 1 or a higher value if the nested struct is an array
      • dcCloseStruct

        public static void dcCloseStruct(long s)
        Completes the struct definition.
        Parameters:
        s - the struct to close
      • dcStructSize

        public static long dcStructSize(long s)
        Returns the size, in bytes, of the specified struct.
        Parameters:
        s - the struct
      • dcStructAlignment

        public static long dcStructAlignment(long s)
        Returns the alignment, in bytes, of the specified struct.
        Parameters:
        s - the struct
      • dcFreeStruct

        public static void dcFreeStruct(long s)
        Frees the specified struct object.
        Parameters:
        s - the struct to free
      • dcDefineStruct

        public static long dcDefineStruct(java.nio.ByteBuffer signature)
        Creates a new struct type using a signature string.
        Parameters:
        signature - the struct signature