Article ID: 189170 - View products that this article applies to.
This article was previously published under Q189170
This article has been archived. It is offered "as is" and will no longer be updated.
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.
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.
With this in mind, here is a Visual Basic function that creates a Long value from two Integers:
Only a few API functions require that you unpack a double word into two short Integers. If you find that you need to perform such a task, you can use conversion routines similar to these:
Both these functions simply mask the portion of the Long integer they want to return using the bitwise "And" operator. The HiWord function shifts this value right 16-bits by dividing it by &H10000. The LoWord function can simply return the value of the "And" operation in most cases. However, because Visual Basic Integers are signed, any low word value that has its high bit set must be converted back into a negative value using the "Or" operator and a mask of &HFFFF0000.
For additional information, please see the following article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/112651/EN-US/ )How To Mimic HIWORD, LOWORD, HIBYTE, LOBYTE C Macros in VB
Article ID: 189170 - Last Review: February 24, 2014 - Revision: 3.1