不能将 "PVOID" 类型的值分配到 "PUCHAR" 类型的实体
时间: 2024-06-16 20:01:58 浏览: 11
"PVOID" 和 "PUCHAR" 在不同的编程环境中代表不同的数据类型。通常情况下:
- "PVOID" 是一种通用指针类型,在Windows和一些C编译器中,它可能指向任意类型的对象,无论是基本类型(如char、int)还是更复杂的结构或对象。
- "PUCHAR" 则特指无符号字符型(unsigned char)的指针,它通常用于操作字节数据。
将 "PVOID" 类型的值赋给 "PUCHAR" 类型的变量是不安全的,因为它们指向的数据类型不匹配。"PVOID" 指向的是一个可以是任何类型的地址,而 "PUCHAR" 只能指向单个字节。如果 "PVOID" 指向的是一个结构或对象,那么直接赋值可能会导致内存越界(如果结构体大于一个字节),或者是类型转换错误,除非你知道 "PVOID" 确实指向的是无符号字符。
为了正确地将 "PVOID" 转为 "PUCHAR",你需要确保 "PVOID" 指向的确实是一个字符类型的内存区域,并且能够安全地进行类型转换。在实际编程中,这种转换通常会在特定上下文中,比如处理内存缓冲区或特定的数据结构时才会进行,而且需要谨慎处理。
相关问题
void* 强制类型转化成Buffer*
将void*指针强制转换为Buffer*指针的过程需要注意以下几点:
1. 首先需要确保void*指针所指向的内存区域确实是Buffer对象,否则将会导致访问非法内存的错误。
2. 其次,需要使用C++中的强制类型转换运算符static_cast,将void*指针转换为Buffer*指针。示例代码如下:
```
void* pVoid = new Buffer();
Buffer* pBuffer = static_cast<Buffer*>(pVoid);
```
3. 在转换后使用Buffer*指针访问成员时,需要确保成员函数都是虚函数,以避免静态绑定带来的问题。
MIniport驱动将显示器的edid注册到Windows系统中
下面是一个示例代码,可以在Miniport驱动中使用DDC/CI协议读取显示器的EDID并将其注册到Windows系统中:
```c
#include <ntddk.h>
#include <ntddvdeo.h>
NTSTATUS RegisterEDID(PHW_DEVICE_EXTENSION hwDeviceExtension)
{
NTSTATUS status = STATUS_SUCCESS;
VIDEO_REQUEST_PACKET vrp;
UCHAR ddcCommandBuffer[2] = { DDC_COMMAND_GET_VCP_FEATURE, DDC_FEATURE_EDID };
UCHAR ddcResponseBuffer[128];
ULONG ddcResponseSize;
RtlZeroMemory(&vrp, sizeof(VIDEO_REQUEST_PACKET));
vrp.IoControlCode = IOCTL_VIDEO_QUERY_AVAIL_MODES;
vrp.StatusBlock.Information = 0;
status = VideoPortZeroMemory(ddcResponseBuffer, sizeof(ddcResponseBuffer));
if (!NT_SUCCESS(status)) {
return status;
}
status = VideoPortGetVpStatus(hwDeviceExtension->HwDeviceExtension, &vrp);
if (!NT_SUCCESS(status) || vrp.StatusBlock.Information == 0) {
return status;
}
RtlZeroMemory(&vrp, sizeof(VIDEO_REQUEST_PACKET));
vrp.IoControlCode = IOCTL_VIDEO_QUERY_CURRENT_MODE;
vrp.StatusBlock.Information = 0;
status = VideoPortZeroMemory(ddcResponseBuffer, sizeof(ddcResponseBuffer));
if (!NT_SUCCESS(status)) {
return status;
}
status = VideoPortGetVpStatus(hwDeviceExtension->HwDeviceExtension, &vrp);
if (!NT_SUCCESS(status) || vrp.StatusBlock.Information == 0) {
return status;
}
RtlZeroMemory(&vrp, sizeof(VIDEO_REQUEST_PACKET));
vrp.IoControlCode = IOCTL_VIDEO_QUERY_DEVICE_INFO;
vrp.InputBuffer = ddcCommandBuffer;
vrp.InputBufferLength = sizeof(ddcCommandBuffer);
vrp.OutputBuffer = ddcResponseBuffer;
vrp.OutputBufferLength = sizeof(ddcResponseBuffer);
status = VideoPortGetVpStatus(hwDeviceExtension->HwDeviceExtension, &vrp);
if (!NT_SUCCESS(status)) {
return status;
}
ddcResponseSize = vrp.StatusBlock.Information;
// Parse the EDID from the DDC response buffer
// ...
// Register the EDID with the Windows system
UNICODE_STRING interfaceName;
RtlInitUnicodeString(&interfaceName, L"ACME\\MyMonitorEDID");
status = IoRegisterDeviceInterface(hwDeviceExtension->PhysicalDeviceObject, &GUID_DEVINTERFACE_MONITOR, NULL, &interfaceName);
if (!NT_SUCCESS(status)) {
return status;
}
PVOID edidBuffer = ExAllocatePoolWithTag(NonPagedPool, ddcResponseSize, 'edid');
if (edidBuffer == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlCopyMemory(edidBuffer, ddcResponseBuffer, ddcResponseSize);
status = IoSetDeviceInterfacePropertyData(&interfaceName, &DEVPKEY_MONITOR_EDID, LOCALE_NEUTRAL, 0, DEVPROP_TYPE_BINARY, ddcResponseSize, edidBuffer);
if (!NT_SUCCESS(status)) {
ExFreePoolWithTag(edidBuffer, 'edid');
return status;
}
ExFreePoolWithTag(edidBuffer, 'edid');
return status;
}
```
请注意,这只是一个示例代码,具体实现可能因不同的硬件和驱动程序而有所不同。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)