如何在VB6中存储Int64号码,以使用Win32功能?
有没有办法在.net中定义像Int64这样的类型?并简单地评估数字。
答案 0 :(得分:3)
我认为很多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)
以及更多操作:
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