天下第二博

Tian Xia The Second BO
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

VB 6.0 利用CopyMemory实现 指针功能

Posted on 2009-08-14 11:57  Nuke'Blog  阅读(1170)  评论(1编辑  收藏  举报

工作需要,要用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