I have been given the task to convert a C# Windows Form GUI to a Java Swing GUI. I have to use the same library which is written in C. One of the functions in the library need the native handle of the window to be able to link the C program to the window. In the C# code the use of native handle is fairly simple and an Intptr
is passed to the function. In java it is a different story. I have found a way to get the handle of the window using JNA (Java Native Access) and HWND
a representation of the native handle. The problem is when I give the handle using HWND
an Invalid memory access Exception pops up every time. Here is the code I am using.
Main Class:
public class main {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setSize(5000,5000);
jFrame.setVisible(true);
final HWND ptrRef = new HWND(Native.getComponentPointer(jFrame));
CLibrary cl = CLibrary.INSTANCE;
System.out.println("Handle: "+ptrRef);
String sPath = "C:\\Users\\markm\\IdeaProjects\\JFrameHandle\\Release";
cl.clStart(ptrRef, sPath, true);
}
}
CLibrary Interface:
public interface CLibrary extends Library {
Clibrary INSTANCE = (CLibrary) Native.loadLibrary("C:\\Users\\markm\\IdeaProjects\\JFrameHandle\\Release\\clibrary",CLibrary.class);
void clStart(HWND parent, String libraryPath, boolean hidePanel);
}
Unfortunately I do not have access to the C code since I only have the .dll files but before I can get access to that code I need to know if am doing something wrong on my part. If someone could point me in the right direction on how to use window handles or maybe there os a way to pass a handle through JNI on the other end please do comment I could use all the help I can get.
Thanks in advance.