Library code snippets
Hi-words and Lo-words
By James Crowley, published on 14 Jul 2001
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
Related articles
Related discussion
-
how do you hide all in VB6
by CapnJack (1 replies)
-
Problem with Input File
by novavb6 (3 replies)
-
How to produce a txt file with a table??
by novavb6 (1 replies)
-
VB6 compatability from XP to Vista
by bronx (1 replies)
-
Fully justify code
by fresh (0 replies)
I knew there was an explanation to this.. if it was simple or not was not to be said by this but it sure is cause for some reading. You may even have given me a hint to a solution of another problem I had, due to why I scrapped that thing.
I might take it up again with this knowledge.
Thanks.
From MSDN:
For some API functions, such as SendMessage or PostMessage, you may need to package two short Integer values into a Long variable to pass them as a single parameter. This article demonstrates how to package such Integers and how to unpack them if necessary.
MORE INFORMATION
The trick to packing values is bit shifting. Because Visual Basic does not provide bit shift operators to use, you need to do things the old fashioned way; through multiplication. To make an Integer the high word for a Long value, you need to multiply it by &H10000. This has the effect of shifting the bit values 16-bits (2-bytes) to the left, making room for the low word value you want to add.
Before you can add the low word value, however, you need to make an adjustment. Remember that Visual Basic Integer types are signed values, but the low word value needs to be unsigned if you plan to add it to your high word value. To make sure Visual Basic treats the low word as an unsigned integer, you need to perform a bitwise "And" on the value using &HFFFF& as a mask. In effect, this saves the value as a Long integer with the high (signed) bit cleared but keeps the original Integer's bit value preserved.
in Hi-Word and Lo-Word... cool... hmmmm what is Hi and Lo words? *smiles
This thread is for discussions of Hi-words and Lo-words.