HiWord and LoWord
HiWord and LoWord
HiWord and LoWord
HiWord and LoWord
HiWord and LoWord
HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord
HiWord and LoWord HiWord and LoWord
HiWord and LoWord
Go Back  Xtreme Visual Basic Talk > > > HiWord and LoWord


Reply
 
Thread Tools Display Modes
  #1  
Old 12-27-2016, 10:12 PM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,829
Default HiWord and LoWord


I was cleaning up some code in my app and I came across this:

NotidyCode = wParam \ 65536
ItemID = wParam And 65535

As usual I did not bother to comment my code and I could not remember what this did exactly.

After some (a lot) of thinking I figured

And 65535 was to get loword from long
\ 65536 was to get hiword from long

Searching web to confirm, I found these 2 functions

Could someone comment on the 2 functions below, and what would be wrong with using my simple functions?

I don't understand the additional "layer" in each of the functions.

Code:
Public Function HiWord(wParam As Long) As Integer

   If wParam And &H80000000 Then
      HiWord = (wParam \ 65535) - 1
   Else
      HiWord = wParam \ 65535
   End If

End Function

Public Function LoWord(wParam As Long) As Integer

   If wParam And &H8000& Then
      LoWord = &H8000& Or (wParam And &H7FFF&)
   Else
      LoWord = wParam And &HFFFF&
   End If

End Function
Reply With Quote
  #2  
Old 12-28-2016, 04:06 AM
Flyguy's Avatar
FlyguyHiWord and LoWord Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,137
Default

I thinks it's for dealing with negative numbers
Reply With Quote
  #3  
Old 12-28-2016, 12:03 PM
Cerian Knight's Avatar
Cerian KnightHiWord and LoWord Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,146
Default

Correct... the simple division method does not work for negative values.

If you must handle negative values (let us know), do NOT use the alternative code you presented, since the HiWord and LoWord functions both seem to have issues. Even if those issues were fixed, you may not need the return value as Type Integer (unless for some reason you need to pass the return as a parameter to an API that requires Integer).
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #4  
Old 12-28-2016, 01:09 PM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,829
Default

Thanks guys...

I am using to parse the WM_MENUSELECT message
I don't think Windows will ever send a negative value, but can't say for sure.
Reply With Quote
  #5  
Old 12-28-2016, 01:44 PM
Cerian Knight's Avatar
Cerian KnightHiWord and LoWord Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,146
Default

Currently, the nine possible high-order word returns for WM_MENUSELECT do not have the high-bit set, so you should be fine. For future-proofing, you could mask out the high bit:
NotidyCode = (wParam And &H7FFFFFFF) \ 65536
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #6  
Old 12-29-2016, 06:53 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,829
Default

OK Thanks!

One further question about testing if these flags have been set.

Right now I am using these two lines to determine if a menu item has been checked.

ret = GetMenuState(hPopUpMenu003, 1, MF_BYPOSITION)
If ret = MF_CHECKED Then MsgBox "I am checked"

Since that all I am doing is checking/unchecking an item everything works OK.

If in the future I enable/unable or hilite/unhilite an item then this "test" will fail?

Is the correct way to test for flags like so?

ret = GetMenuState(hPopUpMenu003, 1, MF_BYPOSITION)
If ret Or MF_CHECKED Then MsgBox "I am checked"
Reply With Quote
Reply

Tags
wparam, hiword, functions, function, loword, public, &h8000, integer, code, comment, simple, additional, layer, understand, &h80000000, cleaning, &h7fff, &hffff, lowordwparam, app, hiwordwparam, thinking, assumed, usual, itemid


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
HiWord and LoWord
HiWord and LoWord
HiWord and LoWord HiWord and LoWord
HiWord and LoWord
HiWord and LoWord
HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord HiWord and LoWord
HiWord and LoWord
HiWord and LoWord
 
HiWord and LoWord
HiWord and LoWord
 
-->