[Gambas-user] unsigned int datatype for external function

Jussi Lahtinen jussi.lahtinen at gmail.com
Tue Jun 12 01:01:23 CEST 2018


Why not do:
>
> Private Extern TakeAndReturn(value As Integer) As Long In "liblibTest"
>

Not sure, maybe you could in some situation read some trash along the right
answer (32 bits is written to output, but 64 bits are read, what are the
rest?). But most certainly the problem would arise when trying to read
output, which consist of multiple variables (arrays, structures, etc).

This would be my way to do the conversion:

Public Sub UnsignedIntegerToLong(X As Integer) As Long

  If BTst(x, 31) = False Then
    Return x
  Else
    Return CLong(BClr(x, 31)) + 2 ^ 31
  Endif

End

And thus:
UnsignedIntegerToLong(-1) = 4294967295

Note that my earlier example was one bit off (confusing!), but apparently
it's not that easy to crash program with one variable.
However, the main point still remains. Stick with correct bit number!


Jussi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20180612/9a7bb20c/attachment.html>


More information about the User mailing list