Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNA C DLL Debug Howto?

Tags:

java

c

dll

jna

I have a DLL that takes an encoded string input and decodes it. The DLL was made on a Win7 64-bit using Codeblocks and mingw64. The C code alone runs fine but when I run it via JNA in a while loop with the same string it is decoded properly for about 70% calls and for the rest its incorrect. I want to debug the situation but I am not sure how to do it after the Java code hands the control over to JNA. Alternatively any ideas on where to look will also be helpful.

The method signature in C and JNA are as follows,

Java: int Decrypt_Blk(byte[] expkey, byte[] in, int size, byte[] out);  
C: __declspec (dllexport) int Decrypt_Blk (unsigned char *expkey, unsigned char *in, unsigned int insize, unsigned char *out);

the correctness is determined by the output, for example, if i take the string "helloworld" encrypt it (I am not going into the encryption details as they are irrelevant) then pass the encrypted output to this DLL method, 7 out of 10 times I get "HelloWorld" other 3 times I would get incorrect and varying characters. And for all 10 times I am in the same thread using the same variable, in the same loop. The 3 incorrect occurrences are also random, the only thing I can notice is that the first run is ALWAYS correct as many times as I have tested but the incorrect run happens at any of the 1-10 runs.

like image 610
iahsan Avatar asked May 03 '12 10:05

iahsan


1 Answers

In Microsoft Visual C++, from the Project Settings for the DLL project, you can select the Debug tab. As the executable for the debug session, provide the full path of the Java.exe. For the arguments, provide the remainder of the command line to run your Java app. Set break points in your C code and start the debug session. Voila!

like image 132
Kevin Welker Avatar answered Nov 13 '22 15:11

Kevin Welker