<div dir="ltr"><div>Good summary and rule of thumb. This way you wont get troubles, even when the actual details are much more complicated. </div><div><br></div><div><br></div><div>Jussi<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 13, 2018 at 4:25 PM, T Lee Davidson <span dir="ltr"><<a href="mailto:t.lee.davidson@gmail.com" target="_blank">t.lee.davidson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Okay so, when passing values to and receiving values from an external library, the bit width of the datatypes should be matched<br>
or else undefined behavior or garbage values will result.<br>
<br>
For example, if a value stored in a 64-bit wide datatype is passed to an external function that expects a 32-bit value, the<br>
behavior is undefined and could lead to application crash. And, if a 32-bit value is returned into a 64-bit datatype, the state<br>
of the higher 32 bits will be undefined tainting the returned value so that it should not be relied upon.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Lee<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
On 06/11/2018 07:01 PM, Jussi Lahtinen wrote:<br>
> <br>
> Why not do:<br>
> <br>
> Private Extern TakeAndReturn(value As Integer) As Long In "liblibTest"<br>
> <br>
> <br>
> Not sure, maybe you could in some situation read some trash along the right answer (32 bits is written to output, but 64 bits<br>
> are read, what are the rest?). But most certainly the problem would arise when trying to read output, which consist of multiple<br>
> variables (arrays, structures, etc).<br>
> <br>
> This would be my way to do the conversion:<br>
> <br>
> Public Sub UnsignedIntegerToLong(X As Integer) As Long<br>
> <br>
> If BTst(x, 31) = False Then<br>
> Return x<br>
> Else<br>
> Return CLong(BClr(x, 31)) + 2 ^ 31<br>
> Endif<br>
> <br>
> End<br>
> <br>
> And thus:<br>
> UnsignedIntegerToLong(-1) = 4294967295<br>
> <br>
> Note that my earlier example was one bit off (confusing!), but apparently it's not that easy to crash program with one variable.<br>
> However, the main point still remains. Stick with correct bit number!<br>
> <br>
> <br>
> Jussi<br>
> <br>
> <br>
> <br>
> <br>
> <br>
</div></div><div class="HOEnZb"><div class="h5">> ----[ Gambas mailing-list is hosted by <a href="https://www.hostsharing.net" rel="noreferrer" target="_blank">https://www.hostsharing.net</a> ]----<br>
> <br>
<br>
----[ Gambas mailing-list is hosted by <a href="https://www.hostsharing.net" rel="noreferrer" target="_blank">https://www.hostsharing.net</a> ]----<br>
</div></div></blockquote></div><br></div>