Some WIN32 API functions require that a function pointer be used as its own argument. The Windows API function can then call the argument function (usually when a specific event occurs later). This technique is called a "callback function". Examples of callback functions include the window process and the callbacks we set during the printing process (the address that provides the callback function for the spooler to update the status and abort printing if necessary).
Another example is the API function EnumWindows (), which enumerates all top-level windows in the current system. EnumWindows () requires that you take a function pointer as your own argument, and then search for a list that is maintained internally by Windows. For each window in the list, it invokes the callback function and passes the window handle as an argument to the callback.
to achieve the same goal in Java, you must use the callback class in the Com.ms.dll package. We inherit from the callback and cancel callback (). This method only approximates the int parameter and returns int or void. The method signature and the specific implementation depend on the Windows API function that uses this callback.
Now all we have to do is create an instance of this callback derivative class and pass it as a function pointer to the API function. Later, J/direct will help us automate the rest of the work.
The following example calls the Win32 API function EnumWindows (); the callback () method in the Enumwindowsproc class takes a handle to each top-level window, gets the caption text, and prints it to the console window.
Import com.ms.dll.*;
Import com.ms.win32.*;
Class Enumwindowsproc extends Callback {public
boolean Callback (int hwnd, int lparam) {
StringBuffer text = new S Tringbuffer (m);
User32.getwindowtext (
hwnd, text, text.capacity () +1);
if (text.length ()!= 0)
System.out.println (text);
return true; To continue enumeration.
}
}
public class Showcallback {public
static void Main (String args[])
throws Interruptedexception {
Boolean OK = user32.enumwindows (
new Enumwindowsproc (), 0);
if (!ok)
System.err.println ("EnumWindows failed.");
Thread.CurrentThread (). Sleep (3000);
}
}
A call to sleep () allows the window process to complete before main () exits.