Results 1 to 9 of 9

Thread: [VB] Number functions: High/Low Word/Byte, Swap Words/Bytes, Bitshifting

Threaded View

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow [VB] Number functions: High/Low Word/Byte, Swap Words/Bytes, Bitshifting

    A DWord in Windows is 32 bytes, which is equivalent to a Long in Visual Basic. The leftmost 16 bits comprise the low word (an Integer in VB), while the the rightmost 16 bits are the high word.

    Most examples use an And operation to get or set the words in a DWord. However, the quickest way to get/set the high and low word of a DWord (Double Word) is to do a direct memory move operation. To do this we need one API declaration:

    VB Code:
    1. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    2.     (ByRef lpvDest As Any, _
    3.      ByRef lpvSrc As Any, _
    4.      ByVal cbLength As Long)

    And four one-line functions, made into properties for easy and logical use.
    VB Code:
    1. Public Property Get LoWord(ByRef DWord As Long) As Integer
    2.     CopyMemory LoWord, ByVal VarPtr(DWord), 2
    3. End Property
    4.  
    5. Public Property Let LoWord(ByRef DWord As Long, ByVal Word As Integer)
    6.     CopyMemory DWord, Word, 2
    7. End Property
    8.  
    9. Public Property Get HiWord(ByRef DWord As Long) As Integer
    10.     CopyMemory HiWord, ByVal VarPtr(DWord) + 2, 2
    11. End Property
    12.  
    13. Public Property Let HiWord(ByRef DWord As Long, ByVal Word As Integer)
    14.     CopyMemory ByVal VarPtr(DWord) + 2, Word, 2
    15. End Property

    Using them is easy, here are some useless examples:
    VB Code:
    1. Dim lDWord As Long
    2. Dim iWord As Integer
    3.  
    4. ' Set the low word
    5. LoWord(lDWord) = 1
    6.  
    7. ' Set the high word
    8. HiWord(lDWord) = 2
    9.  
    10. ' Get the low word
    11. iWord = LoWord(lDWord)
    12.  
    13. ' Get the high word
    14. iWord = HiWord(lDWord)

    Likewise, we can do the same for the high and low bytes of a Word:
    VB Code:
    1. Public Property Get LoByte(ByRef Word As Integer) As Byte
    2.     CopyMemory LoByte, ByVal VarPtr(Word), 1
    3. End Property
    4.  
    5. Public Property Let LoByte(ByRef Word As Integer, ByVal LowByte As Byte)
    6.     CopyMemory Word, LowByte, 1
    7. End Property
    8.  
    9. Public Property Get HiByte(ByRef Word As Integer) As Byte
    10.     CopyMemory HiByte, ByVal VarPtr(Word) + 1, 1
    11. End Property
    12.  
    13. Public Property Let HiByte(ByRef Word As Integer, ByVal HighByte As Byte)
    14.     CopyMemory ByVal VarPtr(Word) + 1, HighByte, 1
    15. End Property

    Have fun
    Last edited by penagate; Jul 7th, 2005 at 06:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width