如何在VB6中模拟.net Int64?

Ali*_*rad 4 vb6 casting

如何在 VB6 中存储 Int64 数字以与 Win32 函数一起使用?
有没有办法在.net中定义像Int64这样的类型?并简单评估一下数字。

Ali*_*rad 5

我认为许多 VB6 程序员需要这样的东西,
因为一些 Win32 API 使用 _int64 作为参数。

我编写了一个函数,将货币转换为 API 兼容的结构。
将这些代码放入模块文件中。

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
Run Code Online (Sandbox Code Playgroud)


现在您可以简单地使用 CInt64 创建一个 Int64 数字。
前任:


myRetVal = Win32APIFunctionWithOneInt64Param(CInt64(10000000))

'----OR

Dim myNum As Int64
myNum = CInt64(10000000)
Run Code Online (Sandbox Code Playgroud)




对于更多操作:


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
Run Code Online (Sandbox Code Playgroud)