简体   繁体   中英

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
}

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

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

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

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
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM