如何在VB6中模拟.net Int64? [英] How to emulate .net Int64 in VB6?

查看:226
本文介绍了如何在VB6中模拟.net Int64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在VB6中存储Int64号码,使用Win32函数?


有没有办法在.net中定义类似Int64的类型?

解决方案

我认为很多VB6程序员都需要这样的代码,

因为一些Win32 API的_int64作为它们的参数。




我写了一个函数来将货币转换为API兼容结构。

将这些代码放在模块文件中。



> 私有声明Sub CopyMemory libkernel32别名RtlMoveMemory(目标为任意,源​​为任何,ByVal长度为长)
Private Const SIZEOF_INT64 As Long = 8

公共类型Int64'LowPart必须是LittleEndian系统中的第一个
'所需零件
低位As Long
HighPart As Long

'可选部分
SignBit As Byte'定义这个长,如果你想获得最小的CPU访问时间。
End使用SignBit键入
',您可以在不更改HighPart中的实数符号位的情况下模拟Int64和UInt64。
',但如果你想改变它,你可以访问它像mySign =(myVar.HighPart And&H80000000)
'或打开符号位使用myVar.HighPart =(myVar.HighPart或&H80000000)

公共函数CInt64(ByVal vCur as Currency)As Int64
vCur =(CCur(vCur)* 0.0001 @)
调用CopyMemory(CInt64,vCur,SIZEOF_INT64)
结束函数



现在您只需使用CInt64创建一个Int64号码。


例如:

  
myRetVal = Win32APIFunctionWithOneInt64Param(CInt64(10000000))

'---- OR

Dim myNum As Int64
myNum = CInt64(10000000)









以及更多操作:

  
Public Sub Op_Ev ,Src As Int64)'设置值。
调用CopyMemory(Dest,Src,SIZEOF_INT64)
End Sub
公共函数Op_Eq(V1 As Int64,V2 As Int64)As Boolean'for equal comparison。
Op_Eq =(V1.LowPart = V2.LowPart):如果不是Op_Eq则退出函数
Op_Eq =(V1.HighPart = V2.HighPart)
结束函数
公共函数Op_Gr (V1 As Int64,V2 As Int64,Optional ByVal IsUnsignedComparison As Boolean = False)As Boolean'for grater comparison。
如果IsUnsignedComparison Then
Dim H1 As Long,H2 As Long'不要更改这些定义的位置以优化函数,以防止执行两个或多个{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'不要将这两个变量的类型更改为字节以保持局部变量的对齐。
HBS1 =((V1.HighPart And& H80000000)/& H80000000)'导出符号位并将其向右移。
HBS2 =((V2.HighPart And& H80000000)/& H80000000)'导出符号位并将其向右移。
Op_Gr =(HBS1> HBS2):If(HBS1> HBS2)then Exit Function
Else
Op_Gr =(V1.HighPart> V2.HighPart):If(V1.HighPart< ;> V2.HighPart)然后退出函数
结束如果
Op_Gr =(V1.LowPart> V2.LowPart)
结束函数
公共函数Op_Ls(V1 As Int64,V2 As Int64,Optional ByVal IsUnsignedComparison As Boolean = False)As Boolean'for less comparison。
如果IsUnsignedComparison Then
Dim H1 As Long,H2 As Long'不要更改这些定义的位置以优化函数,以防止执行两个或多个{SUB ESP,4}
H1 =(V1.HighPart And& H7FFFFFFF):H2 =(V2.HighPart And& H7FFFFFFF)
Op_Ls =(H1 H2)then Exit Function
Dim HBS1 As Long,HBS2 As Long'不要将这两个变量的类型更改为字节以保持局部变量的对齐。
HBS1 =((V1.HighPart And& H80000000)/& H80000000)'导出符号位并将其向右移。
HBS2 =((V2.HighPart And& H80000000)/& H80000000)'导出符号位并将其向右移。
Op_Ls =(HBS1 Else
Op_Ls =(V1.HighPart 结束如果
Op_Ls =(V1.LowPart 结束函数
公共函数Op_Cmp ,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
结束如果
结束函数


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.

解决方案

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

这篇关于如何在VB6中模拟.net Int64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆