Some care is required to ensure that the callback is not reclaimed by garbage collection before the native function is finished with it. If the callback is short-term (only callable for the duration of one function call), no special action is required because a callback passed to a DLL function is guaranteed not to be reclaimed by garbage collection while the call is in progress.
If a callback is long-term (used across function calls), you will need to protect the callback from being reclaimed, typically by storing a reference to it in a Java data structure. You can also store references to callbacks within native data structures by using the com.ms.dll.Root class to wrap the callback inside a root handle. The root handle is a 32-bit handle that prevents the callback from being reclaimed until the handle is explicitly freed. For example, a root handle to a WndProc can be stored in the application data area of an HWND structure, and then explicitly freed on the WM_NCDESTROY message.