Writing callback functions

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.