scottm
scottm

Reputation: 28699

How do I convert a Win32 lParam to a C struct?

Say I have some windows method and a struct:

struct SomeStruct{
int foo;
int bar;
int baz;
int bat;
}

SomeMethod(int a,int wParam, int lParam)
{
   SomeStruct s;

   // get lParam into SomeStruct
}

How to I get the lParam into a SomeStruct variable? I'm thinking that I need something like this (but feel free to point out my ignorance):

SomeMethod(int a, int wParam, int lParam)
{
  SomeStruct *s; //declare struct variable
  s = lParam;    //assign integer value as pointer to struct
  printf("the value of s.foo is %d", s.foo);  //print result
}

Upvotes: 1

Views: 6159

Answers (5)

Still Learn
Still Learn

Reputation: 21

lParam and wParam are usually names for variables of the WPARAM and LPARAM types respectively. WPARAM and LPARAM types of variables are used to declare parameters in SendMessage, PostMessage, WindowProc,DefWindowProc - MS Windows API as well as in the message handlers (frequently but inappropriately called event handlers). Historicaly, in 16-bit versions of windows, WPARAM was defined as WORD (16-bit) value and LPARAM was defined as LONG; that explains names. Both parameters are uses as untyped cookies, that carry some information in a message, either value or the address of the entity containing more information that is processed by a message handler. In 16-bit world, WPARAM was used to carry near address and LPARAM used to carry far address. WPARAM and LPARAM are types now defined as 32-bit values. In SDKs (Software Developer Kit) in WinDef.h. WPARAM is defined as UINT in earlier versions of SDK and now as UINT_PTR. LPARAM stays defined as LONG in earlier versions of SDK and now as LONG_PTR

Upvotes: 2

MSalters
MSalters

Reputation: 179981

Please use the correct types, or your code will fail on Win64.

SomeMethod(int a, WPARAM wParam, LPARAM lParam)
{
  SomeStruct *s = (SomeStruct *) lParam;
  printf("the value of s.foo is %d", s.foo);  //print result
}

Upvotes: 0

dreamlax
dreamlax

Reputation: 95355

Just be careful when you cast integers to pointers. On x64 architectures, int remains a 32-bit integer, but pointers are 64-bit.

Upvotes: 0

daniel
daniel

Reputation: 395

Sorry, but It's a huge mistake to try to convert integers into pointers. To use an undefined type pointer, just use the void pointer. Instead of:



struct SomeStruct {
    int foo;
    int bar;
    int baz;
    int bat;
}

SomeMethod(int a, int wParam, int lParam)
{
    SomeStruct *s; //declare struct variable
    s = lParam;    //assign integer value as pointer to struct
    printf("the value of s.foo is %d", s.foo);  //print result
}

You might use:



struct SomeStruct {
    int foo;
    int bar;
    int baz;
    int bat;
}

SomeMethod(int a, int wParam, void *lParam)
{
    struct SomeStruct *s; //declare struct variable
    s = (struct SomeStruct *)lParam; //assign integer value as pointer to struct
    printf("the value of s.foo is %d", s->foo); //print result
}

Where also a concrete pointer to the structure may work:



SomeMethod(int a, int wParam, struct SomeStruct *lParam)
{
    printf("the value of s.foo is %d", lParam->foo);  //print result
}

Try reading some tip about C, like C-FAQ.

Upvotes: -1

ChrisW
ChrisW

Reputation: 56123

Yes, assuming that lParam 'really' contains a pointer to the struct, then you get at it by 'casting':

  SomeStruct* s; //declare pointer-to-struct variable
  s = (SomeStruct*) lParam;    //assign integer value as pointer to struct
  printf("the value of s->foo is %d", s->foo);  //print result

Upvotes: 7

Related Questions