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



Question:

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  }  


Solution:1

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  


Solution:2

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


Solution:3

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


Solution:4

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  }  


Solution:5

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »