Microsoft KB Archive/112651

From BetaArchive Wiki
Knowledge Base


Article ID: 112651

Article Last Modified on 12/9/2003



APPLIES TO

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition



This article was previously published under Q112651

SUMMARY

Visual Basic does not provide any bitwise functions for pulling apart numeric values. In C, there are macros (HIBYTE, LOBYTE, HIWORD, and LOWORD) to separate parts of long integers into two parts, or separate integers into two parts. This article shows by example how to do the same thing in a Visual Basic program.

MORE INFORMATION

The HIBYTE, LOBYTE, HIWORD, and LOWORD macros are defined in C in the WINDOWS.H file. The HIBYTE and LOBYTE macros are used to retrieve the high-order or low-order byte of the integer passed to them. The HIWORD and LOWORD macros retrieve the high-order or low-order word from a long integer value passed to them.

Step-by-Step Example

This example uses Visual Basic to mimic the HIBYTE, LOBYTE, HIWORD, and LOWORD macros

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add two command buttons (Command1 and Command2) to Form1.
  3. Add the following code to the Comamnd1_Click event:

       Sub Command1_Click ()
          Dim wParam As Integer
          Dim LOBYTE As Integer
          Dim HIBYTE As Integer
          ' Set wParam to a value:
          wParam = &H77FF
          ' Make call to function:
          ret = gethilobyte(wParam, LOBYTE, HIBYTE)
          ' Print out return values:
          Print LOBYTE, HIBYTE
       End Sub
                            
  4. Add the following code to the Comamnd2_Click event:

       Sub Command2_Click ()
          Dim lParam As Long
          Dim LOWORD As Long
          Dim HIWORD As Long
          ' Set lParam to a value:
          lParam = &H7777FFFF
          ' Make call to function:
          ret = gethiloword(lParam, LOWORD, HIWORD)
          ' Print out return values:
          Print LOWORD, HIWORD
       End Sub
                            
  5. Add the following code to the general declarations section of Form1:

       ' Enter the following Function statement as one, single line:
       Function gethilobyte(wparam as integer, LOBYTE as integer,
          HIBYTE as integer)
          ' This is the LOBYTE of the wParam:
          LOBYTE = wParam And  &HFF&
          ' This is the HIBYTE of the wParam:
          HIBYTE = wParam  \  &H100  And  &HFF&
          gethilobyte = 1
       End Function
                            
  6. Add the following code to the general declarations section of Form1:

       Function gethiloword(lparam as long, LOWORD as long, HIWORD as long)
          ' This is the LOWORD of the lParam:
          LOWORD = lParam And  &HFFFF&
          ' LOWORD now equals 65,535 or &HFFFF
          ' This is the HIWORD of the lParam:
          HIWORD = lParam  \  &H10000  And  &HFFFF&
          ' HIWORD now equals 30,583 or &H7777
          gethiloword = 1
       End Function
                            
  7. Run the program. Click the Command1 button to pull apart an integer value into its high-order and low-order bytes. Click the Command2 button to pull apart a long integer into its high-order and low-order words.



Additional query words: 2.00 3.00

Keywords: KB112651