关于WPARAM和LPARAM参数

2024-03-31 10:12
文章标签 参数 wparam lparam

本文主要是介绍关于WPARAM和LPARAM参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

从前,Windows 是 16 位的。每条message信息都可以携带两段数据,分别称为 WPARAM 和 LPARAM。在消息参数传递中对指针类型使用强制类型转换,这是一种常见用法。第一个参数是一个 16 位值("word"),因此称为 W;第二个参数是一个 32 位值("long"),因此称为 L。
W 参数用于传递句柄和整数。L 参数用于传递指针。
当 Windows 转换为 32 位时,WPARAM 参数也变为 32 位值。一个字的大小变成了32bit。(在 64 位 Windows 中,两个参数都是 64 位值!)。
了解这些术语的起源很有帮助。如果查看一下窗口消息的设计,就会发现如果消息使用指针,指针通常会在 LPARAM 中传递,而如果消息使用句柄或整数,则会在 WPARAM 中传递。(如果一条信息两个都包含,则整数放在 WPARAM 中,指针放在 LPARAM 中)。
学会了这一点,记忆窗口消息的参数就容易多了。相反,如果一条消息违反了这一规则,那么你的大脑就会说: "不,这不对"。
Once upon a time, Windows was 16-bit. Each message could carry with it two pieces of data, called WPARAM and LPARAM. It is the common practice of passing casted pointers as message parameters. The first one was a 16-bit value (“word”), so it was called W. The second one was a 32-bit value (“long”), so it was called L.
You used the W parameter to pass things like handles and integers. You used the L parameter to pass pointers.
When Windows was converted to 32-bit, the WPARAM parameter grew to a 32-bit value as well. The word changes to 32-bit. (And in 64-bit Windows, both parameters are 64-bit values!)
It is helpful to understand the origin of the terms. If you look at the design of window messages, you will see that if the message takes a pointer, the pointer is usually passed in the LPARAM, whereas if the message takes a handle or an integer, then it is passed in the WPARAM. (And if a message takes both, the integer goes in the WPARAM and the pointer goes in the LPARAM.)
Once you learn this, it makes remembering the parameters for window messages a little easier. Conversely, if a message breaks this rule, then it sort of makes your brain say, “No, that’s not right.”
LPARAM 是 LONG_PTR 的类型定义,在 win32 中是 long(有符号 32 位),在 x86_64 中是 __int64 (有符号 64 位)。
WPARAM 是 UINT_PTR 的类型定义,在 win32 中是无符号 int(32 位无符号),在 x86_64 中是__int64(64 位无符号)。
LPARAM is a typedef for LONG_PTR which is a long (signed 32-bit) on win32 and __int64 (signed 64-bit) on x86_64.
WPARAM is a typedef for UINT_PTR which is an unsigned int (unsigned 32-bit) on win32 and unsigned __int64 (unsigned 64-bit) on x86_64.
====== 分割线 ====== 
Windows的SDK里代码,经常有WPARAM和LPARAM这两个参数,作为一个消息结构体的两个成员,传递信息。WPARAM通常用来表示句柄或数值,LPARAM通常用来表示一个指针。在平时编写代码时,也会使用这两个参数来传递数据,这时就要注意区分两个类型。
LPARAM因为是会被用作指针的类型,所以使用时,赋值要使用指针类型,或者类型的长度不能小于指针的类型。而WPARAM作为普通数值使用,可以使用当前平台的一个字,或者是固定的长度类型。
今天遇到的问题,就和这两个类型相关。示例代码如下,使用的是unsigned int类型,传递的参数名借用了WPARAM和LPARAM。
#include <stdio.h>
void func(unsigned int LPARAM, unsigned int WPARAM)
{
  unsigned int * value;
  printf("Input LPARAM: 0x%lX \n", LPARAM);
  value = (unsigned int *) LPARAM;
  *value = 100;
  printf("WPARAM is %d\n", WPARAM);
}
int main()
{
  unsigned int foo, bar;
  printf("foo addr: 0x%lX \n", &foo);
  func(&foo, bar);
  return 0;
}
        
这段代码,在ARM 32平台上执行是没问题的,但执行在ARM 64和x86-64的Ubuntu虚拟机平台上,就崩了。
我PC的处理器: Intel(R) Core(TM) i5-8400H CPU @ 2.50GHz   2.50 GHz, 64-bit operating system, x64-based processor, Windows 64系统,上面安装的64 bit的Ubuntu虚拟机。
编译并之执行上面代码:
$ gcc -o x86 test.c
$ ./x86
foo addr: 0x7FFE0163AD10
Input LPARAM: 0x163AD10
Segmentation fault (core dumped)
因为在64 位系统上,unsigned int类型大多还是4字节,long int才是8字节,而指针类型,是8字节,这样一转换,就直接挂了。
所以使用LPARAM和WPARAM参数的较好的方式如下:
#include <stdint.h>
#include <stdio.h>
void func(intptr_t LPARAM, uint32_t WPARAM)
{
  int * value;
  printf("Input LPARAM: 0x%lX \n", LPARAM);
  value = (int *) LPARAM;
  *value = 100;
  printf("Input WPARAM is %d \n", WPARAM);
}
int main()
{
  unsigned int foo, bar;
  bar = 1000;
  printf("foo addr size %ld %ld, value 0x%lX\n", sizeof(&foo), sizeof(unsigned long) , (unsigned long)&foo);
  func((intptr_t)&foo, bar);
  printf("foo is %d, bar is %d. \n", foo, bar);
  return 0;
}
     
输出:
$ gcc -o x86 test.c
$ ./x86
foo addr size 8 8, value 0x7FFE82FD8750
Input LPARAM: 0x7FFE82FD8750
Input WPARAM is 1000
foo is 100, bar is 1000.
LPARAM类型使用intptr_t类型,在stdint.h中定义,32位系统就是4字节,64位系统就是8字节,适配系统,大小自动调节。
WPARAM类型使用uint32_t类型,在stdint.h中定义,使用固定大小,正常应该能满足需求,并且32位、64位系统的int 类型大多同样是4字节。
参考:
1,Microsoft
What do the letters W and L stand for in WPARAM and LPARAM? - The Old New Thing
2,Stackoverflow
c# - What are the definitions for LPARAM and WPARAM? - Stack Overflow

这篇关于关于WPARAM和LPARAM参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/864088

相关文章

SpringBoot接收JSON类型的参数方式

《SpringBoot接收JSON类型的参数方式》:本文主要介绍SpringBoot接收JSON类型的参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、jsON二、代码准备三、Apifox操作总结一、JSON在学习前端技术时,我们有讲到过JSON,而在

JAVA虚拟机中 -D, -X, -XX ,-server参数使用

《JAVA虚拟机中-D,-X,-XX,-server参数使用》本文主要介绍了JAVA虚拟机中-D,-X,-XX,-server参数使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录一、-D参数二、-X参数三、-XX参数总结:在Java开发过程中,对Java虚拟机(JVM)的启动参数进

解读docker运行时-itd参数是什么意思

《解读docker运行时-itd参数是什么意思》在Docker中,-itd参数组合用于在后台运行一个交互式容器,同时保持标准输入和分配伪终端,这种方式适合需要在后台运行容器并保持交互能力的场景... 目录docker运行时-itd参数是什么意思1. -i(或 --interactive)2. -t(或 --

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -

Python调用另一个py文件并传递参数常见的方法及其应用场景

《Python调用另一个py文件并传递参数常见的方法及其应用场景》:本文主要介绍在Python中调用另一个py文件并传递参数的几种常见方法,包括使用import语句、exec函数、subproce... 目录前言1. 使用import语句1.1 基本用法1.2 导入特定函数1.3 处理文件路径2. 使用ex

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python如何使用seleniumwire接管Chrome查看控制台中参数

《Python如何使用seleniumwire接管Chrome查看控制台中参数》文章介绍了如何使用Python的seleniumwire库来接管Chrome浏览器,并通过控制台查看接口参数,本文给大家... 1、cmd打开控制台,启动谷歌并制定端口号,找不到文件的加环境变量chrome.exe --rem

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

Java向kettle8.0传递参数的方式总结

《Java向kettle8.0传递参数的方式总结》介绍了如何在Kettle中传递参数到转换和作业中,包括设置全局properties、使用TransMeta和JobMeta的parameterValu... 目录1.传递参数到转换中2.传递参数到作业中总结1.传递参数到转换中1.1. 通过设置Trans的