Sign in to follow this  
gretty

Creating my own LPARAM: 32 bits of data

Recommended Posts

gretty    273
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 :P

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:
[code]

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;

[/code]

Share this post


Link to post
Share on other sites
ApochPiQ    23005
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 this post


Link to post
Share on other sites
Trienco    2555
As a quick example for bitfields

[code]
struct MyBitStuffedThingy
{
unsigned int a : 16;
unsigned int b : 8;
unsigned int c : 4;
};
[/code]

Share this post


Link to post
Share on other sites
gretty    273
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:
[code]
MyBitStuffedThingy b;
GetKeyNameText( (LPARAM)b, ...);

[/code]

Share this post


Link to post
Share on other sites
ApochPiQ    23005
The missing piece is unions:

[code]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);
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this