How using Java JNA get a pointer from a native method and pass it to another native method?
//C code .h
...
extern "C" HN2QCONN __stdcall N2QLibConnCreate(LPCTSTR lpszIniFile, LPCTSTR lpszSection,
N2Q_CALLBACK_PROC Callback);
extern "C" BOOL __stdcall N2QLibConnectQuik(HN2QCONN hConn);
...
//another C code .h
...
DECLARE_HANDLE (HN2QCONN);
...
//test C code .cpp
HN2QCONN hConn = N2QLibConnCreate("n2q.ini", "local", CallbackProc);
if (!N2QLibConnectQuik(hConn))
printf("error connect to server.");
How can I do the same in Java? How do I correctly get the data from the native method and use the managed code to pass it back to the native method?
I'm trying to do this:
//java code
...
public interface N2q_lib extends Library {
N2q_lib INSTANCE = (N2q_lib)Native.load("n2q_lib.dll", N2q_lib.class);
Pointer N2QLibConnCreate(String lpszIniFile, String lpszSection, N2q_callback_proc Callback);
boolean N2QLibConnectQuik(Pointer ptr);
}
...
public class App
{
public static void main( String[] args )
{
Pointer ptr = new Memory(N2q_lib.INSTANCE.N2QLibConnCreate("n2q.ini", "local", null));
if (!N2q_lib.INSTANCE.N2QLibConnectQuik(ptr))
System.out.println("error connect to server.");
}
}
But I always get "false". Am I using pointers incorrectly?
The main problem was that the external library was compiled without UTF-8 support! Therefore, I needed to specify ASCII for JNA. Additionally, there were hard paths in the external native libraries, so it was necessary to place all the dll's in the same directory with the jar
For yourself, an example of working code: