com.jinvoke
Class Callback

java.lang.Object
  extended by com.jinvoke.Callback

public final class Callback
extends java.lang.Object

Exposes a Java method as a callback function that can be called by native code.

This class exposes a Java method as a native callback function. The Java method may be either a public static method or a public instance method.

Any public Java method of any class can be converted into a Callback object by using one of the Callback class's constructors.

Example: The following code example demonstrates calling the EnumWindows Win32 API that expects a Callback as the first argument. The public static boolean EnumWindowsProc method is converted to a Callback object and passed as an argument to the EnumWindows method. The EnumWindows method then calls back into this Java method for each window object and passes it's hwnd as an argument, which the EnumWindowsProc method uses for getting the window caption.

import com.jinvoke.Callback;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;

public class EnumerateWindows {

        @NativeImport(library = "user32")
        public static native int EnumWindows(Callback callPtr, String lPar);

        @NativeImport(library = "user32")
        public static native int GetWindowText(int hWnd, StringBuffer sb, int nMaxCount);

        public static void main(String[] args) {
                JInvoke.initialize();

                Callback callback = new Callback(EnumerateWindows.class, "EnumWindowsProc");
                EnumWindows(callback, "data");
        }

        public static boolean EnumWindowsProc(int hwnd, String lParam) {
                StringBuffer sb = new StringBuffer(128);
                GetWindowText(hwnd, sb, sb.capacity());
                System.out.println("Window handle = " + hwnd + ", lParam = " + lParam+ ", text=" + sb);
                return true;
        }

}


Field Summary
 long fp
           
 
Constructor Summary
Callback(java.lang.Class clazz, java.lang.String methodname)
          Wraps a static method of the specified class as a native callback function.
Callback(java.lang.Class clazz, java.lang.String methodname, CallingConvention cc)
          Wraps a static method of the specified class as a native callback function.
Callback(java.lang.Object callbackobj, java.lang.String methodname)
          Wraps the specified instance method of the specified object as a native callback function.
Callback(java.lang.Object callbackobj, java.lang.String methodname, CallingConvention cc)
          Wraps the specified instance method of the specified object as a native callback function.
 
Method Summary
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fp

public long fp
Constructor Detail

Callback

public Callback(java.lang.Class clazz,
                java.lang.String methodname,
                CallingConvention cc)
Wraps a static method of the specified class as a native callback function.

When the native code invokes the callback, this Java method is invoked.

Parameters:
clazz - - the class containing the named method methodname
methodname - - a public static method that should be exposed to native code as a callback
cc - - the Calling Convention used by the callback (Default is STDCALL)

Callback

public Callback(java.lang.Class clazz,
                java.lang.String methodname)
Wraps a static method of the specified class as a native callback function.

When the native code invokes the callback, this Java method is invoked. STDCALL calling convention is assumed for callbacks.

Parameters:
clazz - - the class containing the named method methodname
methodname - - a public static method that should be exposed to native code as a callback

Callback

public Callback(java.lang.Object callbackobj,
                java.lang.String methodname,
                CallingConvention cc)
Wraps the specified instance method of the specified object as a native callback function.

When the native code invokes this callback, this Java method is invoked on an instance of the specified object. The callbackobj instance should remain in scope when the callback is being executed. Typically, callbacks are invoked in the lifetime of a single API call. However, this calback can be used for persistent callbacks as long as there is at least one outstanding reference to the callbackobj instance for as long as the native code needs the callback.

Example:

import com.jinvoke.Callback;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;

public class EnumWindowsCallback {

        @NativeImport(library="user32")
        public static native int EnumWindows(Callback callPtr, String lPar); 

        @NativeImport(library="user32")
        public static native int GetWindowText(int hWnd,StringBuffer sb, int nMaxCount);

        public static void main(String[] args) {
                JInvoke.initialize();
                EnumWindowsCallback test2 = new EnumWindowsCallback();
                test2.enumwindows();
        }

        private void enumwindows() {
                EnumWindows(new Callback(this, "EnumWindowsProc") , "data");
        }

        public boolean EnumWindowsProc(int hwnd, String lParam) {
                StringBuffer sb = new StringBuffer(128);
                GetWindowText(hwnd, sb, sb.capacity());
                System.out.println("Window handle = "+ hwnd+ ", lParam = "+lParam+", text="+sb);
                return true;
        }
}

         

Parameters:
callbackobj - - an instance of a class containing the named callback method methodname
methodname - - a public instance method that should be exposed to native code as a callback
cc - - the Calling Convention used by the callback (Default is STDCALL)

Callback

public Callback(java.lang.Object callbackobj,
                java.lang.String methodname)
Wraps the specified instance method of the specified object as a native callback function.

When the native code invokes this callback, this Java method is invoked on an instance of the specified object. The callbackobj instance should remain in scope when the callback is being executed. Typically, callbacks are invoked in the lifetime of a single API call. However, this calback can be used for persistent callbacks as long as there is at least one outstanding reference to the callbackobj instance for as long as the native code needs the callback.

Example:

import com.jinvoke.Callback;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;

public class EnumWindowsCallback {

        @NativeImport(library="user32")
        public static native int EnumWindows(Callback callPtr, String lPar); 

        @NativeImport(library="user32")
        public static native int GetWindowText(int hWnd,StringBuffer sb, int nMaxCount);

        public static void main(String[] args) {
                JInvoke.initialize();
                EnumWindowsCallback test2 = new EnumWindowsCallback();
                test2.enumwindows();
        }

        private void enumwindows() {
                EnumWindows(new Callback(this, "EnumWindowsProc") , "data");
        }

        public boolean EnumWindowsProc(int hwnd, String lParam) {
                StringBuffer sb = new StringBuffer(128);
                GetWindowText(hwnd, sb, sb.capacity());
                System.out.println("Window handle = "+ hwnd+ ", lParam = "+lParam+", text="+sb);
                return true;
        }
}

         

Parameters:
callbackobj - - an instance of a class containing the named callback method methodname
methodname - - a public instance method that should be exposed to native code as a callback