I've wrapped a dll method that has an integer as an out parameter in a web service. In testing I was finding that when I was expecting -1 I was getting 65,535 instead. I realised that the dll was using 16 bit integers and I was specifying the standard .NET 32bit integer when referencing the external dll in my code. this was quickly fixed by specifying a 16 bit integer and all is well.
My question is why did this happen? I could understand an overflow occuring if I was trying to fit a 32 bit integer in a 16 bit integer but I am not so sure why this happens the other way round. Clearly my understanding of this type of casting between types is a little lacking so any guidance will be greatly appreciated.
A 16 bit integer "-1" has all 16 bits set. If you set the bottom 16 bits of a 32 bit integer, the value is 65,535. For an explanation of the internal representation of negative ints, have a look at this article.
This happened because of the type-casting.
You don't actually send 16-bit integers on the call stack -- they're still 32-bit. So what the DLL returned exactly was:
0x0000ffff
If you cast this to e.g. sint16, this is -1, but if this is 32-bits, this is 65535.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With