1

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
}
2
  • Is there a specific method you are working with? I think your answer will work for most, but some methods are different.
    – user4891
    Jan 19, 2009 at 16:11
  • Your SomeMethod should probably be: SomeMethod(int a, WPARAM wParam, LPARAM lParam) ... can't recall whether 'int' is always as wide as an LPARAM (aka LONG_PTR), but even if it is, any developer coming after you may wonder the same thing, so better to use LPARAM, etc. Nov 21, 2015 at 20:28

5 Answers 5

7

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
4
  • Just be ware that it's up to you to make sure it is what you say it is.
    – Eclipse
    Jan 19, 2009 at 16:15
  • Yeah, it's supposed to be a pointer to that type struct. thanks
    – scottm
    Jan 19, 2009 at 16:16
  • Integers aren't smaller than pointers because he tagged it "Win32", not "Win64"; in any case, lParam ought to be a LONG, not an INT (perhaps he oversimplified it somehow in his OP).
    – ChrisW
    Jan 21, 2009 at 16:52
  • LParam is in fact an LPARAM, i.e. a LONG_PTR, and thus big enough for a SomeStruct*
    – MSalters
    Jan 23, 2009 at 12:57
1

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

0

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

0

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
}
-1

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.

1
  • 1
    ...but in some situations in Windows programming, you need to use an LPARAM.
    – reuben
    Feb 1, 2009 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.