使用WinApi获得与“高级显示设置”面板中相同的显示名称

时间:2019-07-02 00:44:40

标签: c++ windows winapi

使用与通过WinApi获取显示名称有关的其他问题(使用EnumDisplayDevicesW并将设备名称作为第一个参数传递时类似的其他方法)类似的方法,我已经能够取得部分成功。我遇到的问题是我获取的信息不完整。可以通过右键单击桌面,选择“显示设置”,然后在底部选择“高级显示设置”来访问“高级显示设置”面板,显示以下显示:

DELL P2414H(DisplayPort)
AOC AG271QG
BenQ PJ

但是,通过使用EnumDisplayDevicesW调用,我提取了以下内容:

AOC AG271QG
DELL P2414H(DisplayPort)
Generic PnP Monitor

虽然顺序对我来说并不重要,但问题是我得到的是“通用PnP监视器”,而不是更有用的“ BenQ PJ”(这不是我想要的确切模型,但仍然提供至少一些信息)。我该如何提取“ BenQ PJ”而不是“ Generic PnP Monitor”(最好保留在WinApi中)?

1 个答案:

答案 0 :(得分:1)

您可以通过 EDID

获取Monitor信息。

EDID可以通过WMI读取

例如,使用 WmiOpenBlock 等进行测试,以减少C ++中WMI的代码=>

我得到我的显示器:

Instance Name = DISPLAY\PHLC085\4&20634529&0&UID65793_0
User Friendly Name = 247ELH
Manufacturer Name = PHL
Product Code ID = C085
Serial Number ID = AU01307001613

包含并定义=>

#define _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <tchar.h>
#include <initguid.h>
#include <wmistr.h>
DEFINE_GUID(WmiMonitorID_GUID, 0x671a8285, 0x4edb, 0x4cae, 0x99,0xfe,0x69,0xa1,0x5c,0x48,0xc0,0xbc );
typedef struct WmiMonitorID {
    USHORT ProductCodeID[16];
    USHORT SerialNumberID[16];
    USHORT ManufacturerName[16];
    UCHAR WeekOfManufacture;
    USHORT YearOfManufacture;
    USHORT UserFriendlyNameLength;
    USHORT UserFriendlyName[1];
} WmiMonitorID, *PWmiMonitorID;
#define OFFSET_TO_PTR(Base, Offset) ((PBYTE)((PBYTE)Base + Offset))

typedef HRESULT(WINAPI*WOB) (IN LPGUID lpGUID, IN DWORD nAccess, OUT LONG*);
WOB WmiOpenBlock;
typedef HRESULT(WINAPI*WQAD) (IN LONG hWMIHandle, ULONG* nBufferSize, OUT UCHAR * pBuffer);
WQAD WmiQueryAllData;
typedef HRESULT(WINAPI*WCB) (IN LONG);
WCB WmiCloseBlock;

测试代码=>

HRESULT hr = E_FAIL;
LONG hWmiHandle;
PWmiMonitorID MonitorID;
HINSTANCE hDLL = LoadLibrary(L"Advapi32.dll");
WmiOpenBlock = (WOB)GetProcAddress(hDLL, "WmiOpenBlock");
WmiQueryAllData = (WQAD)GetProcAddress(hDLL, "WmiQueryAllDataW");
WmiCloseBlock = (WCB)GetProcAddress(hDLL, "WmiCloseBlock");
if (WmiOpenBlock != NULL && WmiQueryAllData && WmiCloseBlock)
{
    WCHAR pszDeviceId[256] = L"";
    hr = WmiOpenBlock((LPGUID)&WmiMonitorID_GUID, GENERIC_READ, &hWmiHandle);
    if (hr == ERROR_SUCCESS)
    {
        ULONG nBufferSize = 0;
        UCHAR *pAllDataBuffer = 0;
        PWNODE_ALL_DATA pWmiAllData;
        hr = WmiQueryAllData(hWmiHandle, &nBufferSize, 0);
        if (hr == ERROR_INSUFFICIENT_BUFFER)
        {
            pAllDataBuffer = (UCHAR*)malloc(nBufferSize);
            hr = WmiQueryAllData(hWmiHandle, &nBufferSize, pAllDataBuffer);
            if (hr == ERROR_SUCCESS)
            {
                while (1)
                {
                    pWmiAllData = (PWNODE_ALL_DATA)pAllDataBuffer;
                    if (pWmiAllData->WnodeHeader.Flags & WNODE_FLAG_FIXED_INSTANCE_SIZE)
                        MonitorID = (PWmiMonitorID)&pAllDataBuffer[pWmiAllData->DataBlockOffset];
                    else
                        MonitorID = (PWmiMonitorID)&pAllDataBuffer[pWmiAllData->OffsetInstanceDataAndLength[0].OffsetInstanceData];

                    ULONG nOffset = 0;
                    WCHAR *pwsInstanceName = 0;
                    nOffset = (ULONG)pAllDataBuffer[pWmiAllData->OffsetInstanceNameOffsets];
                    pwsInstanceName = (WCHAR*)OFFSET_TO_PTR(pWmiAllData, nOffset + sizeof(USHORT));
                    WCHAR wsText[255] = L"";
                    swprintf(wsText, L"Instance Name = %s\r\n", pwsInstanceName);
                    OutputDebugString(wsText);

                    WCHAR *pwsUserFriendlyName;
                    pwsUserFriendlyName = (WCHAR*)MonitorID->UserFriendlyName;
                    swprintf(wsText, L"User Friendly Name = %s\r\n", pwsUserFriendlyName);
                    OutputDebugString(wsText);

                    WCHAR *pwsManufacturerName;
                    pwsManufacturerName = (WCHAR*)MonitorID->ManufacturerName;
                    swprintf(wsText, L"Manufacturer Name = %s\r\n", pwsManufacturerName);
                    OutputDebugString(wsText);

                    WCHAR *pwsProductCodeID;
                    pwsProductCodeID = (WCHAR*)MonitorID->ProductCodeID;
                    swprintf(wsText, L"Product Code ID = %s\r\n", pwsProductCodeID);
                    OutputDebugString(wsText);

                    WCHAR *pwsSerialNumberID;
                    pwsSerialNumberID = (WCHAR*)MonitorID->SerialNumberID;
                    swprintf(wsText, L"Serial Number ID = %s\r\n", pwsSerialNumberID);
                    OutputDebugString(wsText);

                    if (!pWmiAllData->WnodeHeader.Linkage)
                        break;
                    pAllDataBuffer += pWmiAllData->WnodeHeader.Linkage;
                }
                free(pAllDataBuffer);
            }
        }
        WmiCloseBlock(hWmiHandle);
    }
}