简体   繁体   English

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

[英]How do I convert a Win32 lParam to a C struct?

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? 如何将lParam转换为SomeStruct变量? 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': 是的,假设lParam“确实”包含指向该结构的指针,那么您可以通过“ 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. 通常,lParam和wParam分别是WPARAM和LPARAM类型的变量的名称。 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). WPARAM和LPARAM类型的变量用于在SendMessage,PostMessage,WindowProc,DefWindowProc-MS Windows API以及消​​息处理程序(经常但不恰当地称为事件处理程序)中声明参数。 Historicaly, in 16-bit versions of windows, WPARAM was defined as WORD (16-bit) value and LPARAM was defined as LONG; 历史上,在16位版本的Windows中,WPARAM被定义为WORD(16位)值,而LPARAM被定义为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. 这两个参数都用作未类型的cookie,它们在消息中携带一些信息,该值可以是包含消息处理程序处理的更多信息的实体的值或地址。 In 16-bit world, WPARAM was used to carry near address and LPARAM used to carry far address. 在16位环境中,WPARAM用于承载近地址,而LPARAM用于承载远地址。 WPARAM and LPARAM are types now defined as 32-bit values. WPARAM和LPARAM是现在定义为32位值的类型。 In SDKs (Software Developer Kit) in WinDef.h. 在WinDef.h的SDK(软件开发工具包)中。 WPARAM is defined as UINT in earlier versions of SDK and now as UINT_PTR. WPARAM在早期版本的SDK中定义为UINT,现在定义为UINT_PTR。 LPARAM stays defined as LONG in earlier versions of SDK and now as LONG_PTR LPARAM在早期版本的SDK中仍然定义为LONG,现在定义为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. 在x64体系结构上, int仍然是32位整数,但指针是64位。

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
}

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 . 要使用未定义的类型指针,只需使用void指针即可 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 . 尝试阅读一些有关C的技巧,例如C-FAQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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