如何将Win32 lParam转换为C结构?

时间:2021-03-21 07:04:31

Say I have some windows method and a struct:

假设我有一些windows方法和结构:

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):

如何将lParam变为SomeStruct变量?我在想我需要这样的东西(但请随意指出我的无知):

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
}

5 个解决方案

#1


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

是的,假设lParam'确实'包含指向结构的指针,那么你通过'cast'得到它:

  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

#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

lParam和wParam通常分别是WPARAM和LPARAM类型变量的名称。 WPARAM和LPARAM类型的变量用于在SendMessage,PostMessage,WindowProc,DefWindowProc - MS Windows API以及消​​息处理程序(通常但不恰当地称为事件处理程序)中声明参数。历史性地,在16位版本的Windows中,WPARAM定义为WORD(16位)值,LPARAM定义为LONG;这解释了名字。这两个参数都用作无类型cookie,它在消息中携带一些信息,包含值或包含由消息处理程序处理的更多信息的实体的地址。在16位世界中,WPARAM用于携带近地址,而LPARAM用于携带远地址。 WPARAM和LPARAM现在定义为32位值。在WinDef.h中的SDK(软件开发工具包)中。 WPARAM在早期版本的SDK中定义为UINT,现在定义为UINT_PTR。 LPARAM在早期版本的SDK中保持定义为LONG,现在为LONG_PTR

#3


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

将整数转换为指针时要小心。在x64体系结构上,int仍然是32位整数,但指针是64位。

#4


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

请使用正确的类型,否则您的代码将在Win64上失败。

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

#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:

抱歉,尝试将整数转换为指针是一个巨大的错误。要使用未定义的类型指针,只需使用void指针。代替:



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.

尝试阅读关于C的一些提示,例如C-FAQ。

#1


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

是的,假设lParam'确实'包含指向结构的指针,那么你通过'cast'得到它:

  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

#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

lParam和wParam通常分别是WPARAM和LPARAM类型变量的名称。 WPARAM和LPARAM类型的变量用于在SendMessage,PostMessage,WindowProc,DefWindowProc - MS Windows API以及消​​息处理程序(通常但不恰当地称为事件处理程序)中声明参数。历史性地,在16位版本的Windows中,WPARAM定义为WORD(16位)值,LPARAM定义为LONG;这解释了名字。这两个参数都用作无类型cookie,它在消息中携带一些信息,包含值或包含由消息处理程序处理的更多信息的实体的地址。在16位世界中,WPARAM用于携带近地址,而LPARAM用于携带远地址。 WPARAM和LPARAM现在定义为32位值。在WinDef.h中的SDK(软件开发工具包)中。 WPARAM在早期版本的SDK中定义为UINT,现在定义为UINT_PTR。 LPARAM在早期版本的SDK中保持定义为LONG,现在为LONG_PTR

#3


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

将整数转换为指针时要小心。在x64体系结构上,int仍然是32位整数,但指针是64位。

#4


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

请使用正确的类型,否则您的代码将在Win64上失败。

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

#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:

抱歉,尝试将整数转换为指针是一个巨大的错误。要使用未定义的类型指针,只需使用void指针。代替:



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.

尝试阅读关于C的一些提示,例如C-FAQ。