Ali Kherad
Ali Kherad

Reputation: 965

How to emulate .net Int64 in VB6?

How can store an Int64 number in VB6, to work with Win32 functions?
Is there a way to define a type like Int64 in .net? And simply evaluate the number.

Upvotes: 4

Views: 2183

Answers (1)

Ali Kherad
Ali Kherad

Reputation: 965

I think many of VB6 programmers need something like this,
Because some of the Win32 API's use _int64 as their parameters.

I wrote a function to cast a currency into an API compatible structure.
Put these codes in a module file.

Private Declare Sub CopyMemory lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const SIZEOF_INT64 As Long = 8

Public Type Int64 'LowPart must be the first one in LittleEndian systems
    'required part
    LowPart  As Long
    HighPart As Long

    'optional part
    SignBit As Byte 'define this as long if you want to get minimum CPU access time.
End Type
'with the SignBit you can emulate both Int64 and UInt64 without changing the real sign bit in HighPart.
'but if you want to change it you can access it like this  mySign = (myVar.HighPart And &H80000000)
'or turn on the sign bit using  myVar.HighPart = (myVar.HighPart Or &H80000000)

Public Function CInt64(ByVal vCur As Currency) As Int64
    vCur = (CCur(vCur) * 0.0001@)
    Call CopyMemory(CInt64, vCur, SIZEOF_INT64)
End Function


Now you can simply use CInt64 to create an Int64 number.
ex:


myRetVal = Win32APIFunctionWithOneInt64Param(CInt64(10000000))

'----OR

Dim myNum As Int64
myNum = CInt64(10000000)




And for more operations:


Public Sub Op_Ev(Dest As Int64, Src As Int64) 'for setting the value.
    Call CopyMemory(Dest, Src, SIZEOF_INT64)
End Sub
Public Function Op_Eq(V1 As Int64, V2 As Int64) As Boolean 'for equal comparison.
    Op_Eq = (V1.LowPart = V2.LowPart)   : If Not Op_Eq Then Exit Function
    Op_Eq = (V1.HighPart = V2.HighPart)
End Function
Public Function Op_Gr(V1 As Int64, V2 As Int64, Optional ByVal IsUnsignedComparison As Boolean = False) As Boolean 'for grater comparison.
    If IsUnsignedComparison Then
        Dim H1 As Long, H2 As Long 'don't change the location of these definitions to optimize the function to prevent to execute two or more {SUB ESP, 4}
        H1 = (V1.HighPart And &H7FFFFFFF)   : H2 = (V2.HighPart And &H7FFFFFFF)
        Op_Gr = (H1 > H2)                   : If (H1 <> H2) Then Exit Function
        Dim HBS1 As Long, HBS2 As Long 'don't change the type of these two vars to byte to keep alignment for local variables.
        HBS1 = ((V1.HighPart And &H80000000) / &H80000000) 'export the sign bit and shift it to the right.
        HBS2 = ((V2.HighPart And &H80000000) / &H80000000) 'export the sign bit and shift it to the right.
        Op_Gr = (HBS1 > HBS2)               : If (HBS1 <> HBS2) Then Exit Function
    Else
        Op_Gr = (V1.HighPart > V2.HighPart) : If (V1.HighPart <> V2.HighPart) Then Exit Function
    End If
    Op_Gr = (V1.LowPart > V2.LowPart)
End Function
Public Function Op_Ls(V1 As Int64, V2 As Int64, Optional ByVal IsUnsignedComparison As Boolean = False) As Boolean 'for less comparison.
    If IsUnsignedComparison Then
        Dim H1 As Long, H2 As Long 'don't change the location of these definitions to optimize the function to prevent to execute two or more {SUB ESP, 4}
        H1 = (V1.HighPart And &H7FFFFFFF)   : H2 = (V2.HighPart And &H7FFFFFFF)
        Op_Ls = (H1 < H2)                   : If (H1 <> H2) Then Exit Function
        Dim HBS1 As Long, HBS2 As Long 'don't change the type of these two vars to byte to keep alignment for local variables.
        HBS1 = ((V1.HighPart And &H80000000) / &H80000000) 'export the sign bit and shift it to the right.
        HBS2 = ((V2.HighPart And &H80000000) / &H80000000) 'export the sign bit and shift it to the right.
        Op_Ls = (HBS1 < HBS2)               : If (HBS1 <> HBS2) Then Exit Function
    Else
        Op_Ls = (V1.HighPart < V2.HighPart) : If (V1.HighPart <> V2.HighPart) Then Exit Function
    End If
    Op_Ls = (V1.LowPart < V2.LowPart)
End Function
Public Function Op_Cmp(V1 As Int64, V2 As Int64, Optional ByVal IsUnsignedComparison As Boolean = False) As Long 'for comparison.
    If Op_Gr(V1, V2, IsUnsignedComparison) Then
        Op_Cmp = 1
    ElseIf Op_Ls(V1, V2, IsUnsignedComparison) Then
        Op_Cmp = -1
    Else
        Op_Cmp = 0
    End If
End Function

Upvotes: 5

Related Questions