tags:

views:

1299

answers:

4

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
}
+4  A: 

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
ChrisW
Just be ware that it's up to you to make sure it is what you say it is.
Eclipse
Yeah, it's supposed to be a pointer to that type struct. thanks
scottm
That won't work if integers are smaller than pointers.
dreamlax
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
LParam is in fact an LPARAM, i.e. a LONG_PTR, and thus big enough for a SomeStruct*
MSalters
A: 

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.

daniel
...but in some situations in Windows programming, you *need* to use an LPARAM.
Reuben
A: 

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

dreamlax
A: 

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