Hi-words and Lo-words

When using API, we often need to place values in the hi word or lo word, and retreive these again. The code below provides a few useful functions for doing this

'creates a long variable out of two words
Private Function MakeLong(nLoWord As Integer, nHiWord As Integer) As Long
    'places two integer values into the hiword and loword
    MakeLong = nHiWord * 65536 + nLoWord
End Function

'extracts the hiword and loword from a long variable
Private Function GetWords(ByVal lParam As Long, ByRef lLoWord As Long, ByRef lHiWord As Long) As Boolean
    ' This is the LOWORD of the lParam:
    lLoWord = lParam And &HFFFF&
    ' lLoWord now equals 65,535 or &HFFFF
    ' This is the lHiWord of the lParam:
    lHiWord = lParam &H10000 And &HFFFF&
    ' HIWORD now equals 30,583 or &H7777
    GetWords = True = 1
End Function

Alternatively, you can use these two functions to extract the hiword or loword, submitted by Mike J

Function GetHiWord(dw As Long) As Integer
If dw& And &H80000000 Then
   GetHiWord% = (dw& \ 65535) - 1
Else
   GetHiWord% = dw& \ 65535
End If
End Function

Function GetLoWord(dw As Long) As Integer
If dw& And &H8000& Then
   GetLoWord% = &H8000 Or (dw& And &H7FFF&)
Else
   GetLoWord% = dw& And &HFFFF&
End If
End Function

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold