现在的位置: 首页 > 综合 > 正文

VB 6.0 利用CopyMemory实现 指针功能

2011年02月10日 ⁄ 综合 ⁄ 共 719字 ⁄ 字号 评论关闭

工作需要,要用VB写一个接口程序,其中要把浮点型转成Byte数组,用到了一个API,先记录下来,以后C#中可能会用到同样的功能。

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As LongByVal Source As LongByVal Length As Long)

浮点转换Byte函数

Private Function FloatToBytes(value As SingleAs Byte()
    
Dim returnByte(4As Byte
    
Dim sPtr As Long, bPtr As Long
    sPtr 
= VarPtr(value)
    bPtr 
= VarPtr(returnByte(1))
    CopyMemory bPtr, sPtr, 
4
    FloatToBytes 
= returnByte
End Function

Byte转换浮点函数

Private Function BytesToFloat(bytes() As ByteAs Single
    
Dim returnValue As Single
    
Dim sPtr As Long, bPtr As Long
    sPtr 
= VarPtr(returnValue)
    bPtr 
= VarPtr(bytes(1))
    CopyMemory sPtr, 
ByVal bPtr, 4
    BytesToFloat 
= returnValue
End Function

抱歉!评论已关闭.