如何在VB6中模拟.NET的Int64?

4

如何在VB6中存储Int64数字以便与Win32函数配合使用?
是否有一种方法可以在.NET中定义像Int64这样的类型?并且可以简单地计算数字。

1个回答

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

现在你可以使用CInt64来创建一个Int64数字。 例如:

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


我有一个可以与COM一起使用的应用程序。实际上我正在返回一个Int64属性,但VB5用户无法使用它。你碰巧有吗? - Leonardo Herrera
哇,我以为我已经删除了那个。我的问题是是否有机会在VB5中将Int64传递给ActiveX方法。 - Leonardo Herrera
我不知道任何VB5,但在VB6中你可以这样做: 虽然有点复杂,但你可以自己创建一个变量类型,如Int64(VT_I8)..., 具体步骤如下: 1)将一个数字设置为变量 2)用RtlMoveMemory之类的方法替换变量类型字节,例如:RtlMoveMemory(variant, {VT_I8=20}, {4或只是1}) 然后你就可以将创建的变量传递给像CallByName这样的后期绑定方法调用器... - Ali Kherad
另一种方法是创建一个与您想要将Int64传递到的目标接口/类完全相似的接口,然后只需将该特定参数类型更改为类似于Int64结构的内容,然后您可以-我称之为-在飞行中进行转换到该类型(由于调用时没有类型检查并且是弱类型,因此在vb上可用), 最后,在转换后的类型上简单地调用您的方法,例如: Dim interface As IMyOwnCreatedInterface \n CopyMemory interface, YouTargetClass, 4 \n interface.Method(Int64Parameter) \n CopyMemory interface, Clng(0), 4 '非常重要\n - Ali Kherad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接