Article ID: 189323
Article Last Modified on 7/15/2004
UnsignedToLong
LongToUnsigned
UnsignedToInteger
IntegerToUnsigned
Declare Function MyAPI Lib "xxx" (Value As Long) As Long Dim uResult As Long uResult = MyAPI(UnsignedToLong(3300000000)) Debug.Print "Return Code: " & LongToUnsigned(uResult)In the above example, the MyAPI API accepts an unsigned Long as a parameter and returns an unsigned Long as a result code. Because VBA only understands signed Longs, the DECLARE statement uses signed Longs. The UnsignedToLong
function converts a number outside the range of signed Long (but within therange of an unsigned Long) into a signed Long for purposes of calling the API. The LongToUnsigned function performs the opposite conversion.
Option Explicit
Private Const OFFSET_4 = 4294967296#
Private Const MAXINT_4 = 2147483647
Private Const OFFSET_2 = 65536
Private Const MAXINT_2 = 32767
Function UnsignedToLong(Value As Double) As Long
If Value < 0 Or Value >= OFFSET_4 Then Error 6 ' Overflow
If Value <= MAXINT_4 Then
UnsignedToLong = Value
Else
UnsignedToLong = Value - OFFSET_4
End If
End Function
Function LongToUnsigned(Value As Long) As Double
If Value < 0 Then
LongToUnsigned = Value + OFFSET_4
Else
LongToUnsigned = Value
End If
End Function
Function UnsignedToInteger(Value As Long) As Integer
If Value < 0 Or Value >= OFFSET_2 Then Error 6 ' Overflow
If Value <= MAXINT_2 Then
UnsignedToInteger = Value
Else
UnsignedToInteger = Value - OFFSET_2
End If
End Function
Function IntegerToUnsigned(Value As Integer) As Long
If Value < 0 Then
IntegerToUnsigned = Value + OFFSET_2
Else
IntegerToUnsigned = Value
End If
End Function
?UnsignedToLong(3300000000)
?LongToUnsigned(-55)
?UnsignedToInteger(45000)
?IntegerToUnsigned(-3000)
-994967296
4294967241
-20536
62536
112673 How To Pass & Return Unsigned Integers to DLLs from VB
Additional query words: kbDSupport kbDSD kbVBp500 kbVBA500 kbVBp400 kbVBp kbAPI kbDll
Keywords: kbhowto KB189323