Search code examples
javacanvasjna

Passing a Native Window Handle to a DLL in Java


To explain the problem I guess its the best to explain (simplified) what I want to do.

There is a device(camera). The API to this device is provided by a native DLL. One function inside this DLL needs a Native Window Handle to draw the captured image directly in the application. The solution with C# would be to pass a PictureBox.Handle to this function. However in Java the drawing component is an awt.Canvas. The earlier implementation for this problem was to get a peer of this Canvas which is deprecated since JDK 1.1. Also I don't know if this apporach ever has been working. Therefore they are two questions:

  1. Could the deprecated approach really have been working?

  2. Is there any other possible solution to pass a window handle to the DLL?

This is the signature for the function inside the DLL:

int draw_func(..., const void* window,...);

And this how the problem was solved(or maybe not) before:

long lhwnd = ((sun.awt.windows.WComponentPeer)._canvasPictureBox.getPeer()).getHWnd();

Pointer HWndPtr = new Pointer(lhwnd );

Solution

  • JNA provides a method to obtain the component's native peer, Native.getComponentPointer(), which you can use to initialize a HANDLE object from JNA's platform.jar or pass in directly.