Search code examples
javac++jna

Define HGlobal in JNA


I have to translate app from C++ to Java, but it uses dll, so I have to use JNA. Application should scan something from device. Here is app in C++:

//HEADERS
ABC_Shell = (int (*)(char* cmd, int len, char* rsp, int buflen))GetProcAddress(g_hLib, "ABC_Shell");
ABC_ImageProcessFromRaw = (int (*)(HGLOBAL hFront, HGLOBAL hRear))GetProcAddress(g_hLib, "ABC_ImageProcessFromRaw");
//END    

HGLOBAL hImage[3];
memset(hImage, 0, sizeof(hImage));

nRet = ABC_Shell("CP12", strlen(szShell), (char*)hImage, sizeof(hImage));
ABC_ImageProcessFromRaw(hImage[0], hImage[1]);  //FACE, BACK RGB IMAGE
ABC_ImageProcessFromRaw(hImage[2], NULL);       //FACE IR IMAGE
SaveImage(0, hImage[0], hImage[1], hImage[2]);

And here is in Java:

//HEADERS
int ABC_Shell(String command, int len, byte[] response, int buflen);
int ABC_ImageProcessFromRaw(byte[] hFront, byte[] hRear);
//END

byte[] response = new byte[64];
api.ABC_Shell("CP12", 8, response, 1024);  // it works, but response is strange
api.ABC_ImageProcessFromRaw(response, null);

Of course device is scanning, but I don't know what kind of variable use to take a response and after that use it in next function. In C++ is HGlobal[3], there is no something like that in JNA. I was looking here - https://jna.java.net/javadoc/platform/com/sun/jna/platform/win32/W32API.html.

Do you have any ideas, how to handle this global memory block in three parts?


Solution

  • ...
    int ABC_Shell(String command, int len, Pointer[] response, int buflen);
    int ABC_ImageProcessFromRaw(Pointer hFront, Pointer hRear);
    ...
    
    Pointer[] response = new Pointer[3];
    // You should probably be more programmatic about the command buffer and its length
    api.ABC_Shell("CP12", 8, response, response.length * Pointer.SIZE);
    api.ABC_ImageProcessFromRaw(response[0], null);