Question

In java i am using JNA to load VC++ dll file and calling functions within it,in one function i need to send 6 parameters,In VC++ function definition i am getting correct values for first 3 parameters but last 3 are having value '0',
2nd parameter is byte array,when i send 1024 bytes, i am getting 5th bool parameter as false but when i pass 10 bytes it is taken as true
my function prototype :

int deviceupload(Pointer p, _byte[] data_, long startaddress, long datalength,_Boolean rt_,Pointer xyz);

So will mapping changes depending upon the size of parameters?
or JNA stack is so small that it cant hold 6 paramters?but according to JNA documentation MAX_NARGS of Function class value is 256 so i think 6 number of parameters is not an issue,
even though 3rd and 4th parameters have same data type,in VC++ function definition startaddress is correctly received but datalength received value is 0

so any idea why it is behaving so weird ?

Was it helpful?

Solution 2

Arguments are placed in memory "slots" on the stack. A small argument might take one slot, or share a slot with another argument, while a large argument might take two slots. You can see how if you mistake a small argument for a large one, it will shift all the subsequent arguments into incorrect positions.

void my_func(int32 p1, int64 p2)

|               |
+---------------+
| P2 (64-bit)   |
+---------------+
| P2            | (P2 takes up two slots)
+---------------+
+ P1 (32-bit)   |
+---------------+ <-- Top of stack

Now if you mistakenly use a 64-bit value for P1, you get this instead:

|               |
+---------------+
| P2 (64-bit)   |
+---------------+
| P2            | 
+---------------+
+ P1 (64-bit)   |
+---------------+ 
+ P1            |
+---------------+ <-- Top of stack

The callee (the function you called) has no idea that this shift has taken place, and therefore attempts to read the arguments from their expected positions. For small arguments it might get a piece of a larger one, and for larger arguments it might get pieces of two or more other arguments.

What the callee actually sees is this:

|               |
+---------------+
| (P2)          | 
+---------------+
+ (P1)          |
+---------------+ 
+ (P1)          |
+---------------+ <-- Top of stack

You can see that the value read for P1 is actually only half of the larger value, while the value read for P2 comprises the other half of P1 and half of the value passed in as P2.

(This explanation is somewhat simplified, but generally indicates how the stack works)

OTHER TIPS

Actually my problem got solved,VC++ function was expecting long which is 4 byte,but in java i was passing long which is 8 bytes,but according to JNA documentation VC++ long equivalent is NativeLong in java ,but we can't subtract two NativeLong variables which i am supposed to do it.so i was passing Long.

but later i passed int(4bytes) arguments in java for long(4bytes) parameters in VC++ then it worked properly

but i dint understood why other parameters were affected because of previous parameter data type miss match

in my que because of startaddress variable datatype miss match datalength variable data was getting effected ,so still my question is unanswered ,can any one help me understand it?,unfortunately only my error was cleared

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top