Creating my own LPARAM: 32 bits of data

This topic is 2554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello

I want to create my own LPARAM to pass to the win32 function GetKeyNameText(), (the first parameter takes a LPARAM var).

This may seem like doing things the hard way, but its a work around for not having specific amounts of information & also working at the bit level is REALLY confusing to me which is why I want to familiarise myself with this.

So what I want to put in my LPARAM var is:
- set the 16-23 bits to the keyboard Scan code: I've got the scan code I just dont know how I would combine it into a 32bit variable?
- set the 24th bit to the extended-key flag (I have no idea how to get this yet alone how to combine it into a 32 bit variable)
- set the 25th bit to the dont care bit to I do care - so would I set this bit to 1?

So I understand the way binary & bits work...I think, its amazing I understand higher lvl concepts like polymorphism but not lower lvl computer hardware stuff

So I have a 32 bit(or byte?) variable, does that mean I have 32 0's & 1's: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
OR
I have a variable like this XXXXXX, eg 100011 where the last number is 2^0(so 1), then 2^1(2), then 2^2(4), .... then finally 2^5(32).

So to create my LPARAM would I do this:
 DWORD scanCode = 0x??; // a DWORD is a 32bit var, but the scan code is only 7 bits long? bit extFlag = 1; // now is there a bit variable? How can I find out the extended-key flag also? bit careBit = 1; //Now to combine it all would I do this? DWORD myLParam = scanCode & extFlag & careBit; // OR LPARAM myLParam = scanCode & extFlag & careBit; 

Share on other sites
You can either do this with bitfields in a dummy structure (more clear) or binary masking (slightly less clear but just as effective). I'd recommend bitfields to start off with as your grasp of binary operations is a bit shaky (e.g. the & operator doesn't do what you think it does).

Share on other sites
As a quick example for bitfields

 struct MyBitStuffedThingy { unsigned int a : 16; unsigned int b : 8; unsigned int c : 4; }; 

Share on other sites
Thanks for the code example

Would that structure be able to be converted to a LPARAM? I dont think its possible but you never know.

I cant do something like this can I:
 MyBitStuffedThingy b; GetKeyNameText( (LPARAM)b, ...); 

Share on other sites
The missing piece is unions:

union MyBitFields { struct Bits { unsigned Alpha : 16; unsigned Beta : 8; unsigned Gamma : 4; unsigned IForgetMyGreek : 4; }; unsigned ThirtyTwoBits; }; MyBitFields value; value.Alpha = 0xffff; value.Beta = 0xaa; value.Gamma = 1; value.IForgetMyGreek = 0; SomeAPI(value.ThirtyTwoBits); 

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×