C++ MinGW 下获取EDID代码

#include <iostream>
#include <windows.h>
#include <setupapi.h>
#include <iomanip>

std::wstring GetHKEY(HKEY key);

int main(int argc, char** argv) {
    const LPCWSTR msgCaption = L"EDID.cpp";

    DWORD dwSize;
    GUID* ptrGUID = NULL;

    BOOL bResult = SetupDiClassGuidsFromNameW(L"Monitor", NULL, NULL, &dwSize);
    if (bResult == FALSE)
    {
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            ptrGUID = new GUID[dwSize];
            bResult = SetupDiClassGuidsFromNameW(L"Monitor", ptrGUID, dwSize, &dwSize);
            if (!bResult)
            {
                MessageBoxW(NULL, L"Unable to retrieve GUID of the specified class.", msgCaption, MB_ICONERROR);
                return 1;
            }
        }
        else
        {
            MessageBoxW(NULL, L"Unable to retrieve GUID of the specified class.", msgCaption, MB_ICONERROR);
            return 1;
        }
    }

    HDEVINFO devINFO = INVALID_HANDLE_VALUE;
    devINFO = SetupDiGetClassDevsW(ptrGUID, NULL, NULL, DIGCF_PRESENT);
    if (devINFO == INVALID_HANDLE_VALUE)
    {
        MessageBoxW(NULL, L"Failed to retrieve device information of the GUID class.", msgCaption, MB_ICONERROR);
        return 1;
    }

    SP_DEVINFO_DATA devDATA;
    devDATA.cbSize = sizeof(SP_DEVINFO_DATA);

    BOOL devFOUND = TRUE;
    for (DWORD index = 0; devFOUND; index++)
    {
        devFOUND = SetupDiEnumDeviceInfo(devINFO, index, &devDATA);
        if (devFOUND)
        {
            if (index != 0)
                std::wcout << std::endl;

            HKEY devKEY = SetupDiOpenDevRegKey(devINFO, &devDATA, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);

            std::wprintf(L"Registry Key: \"%s\"\n", GetHKEY(devKEY).c_str());

            BYTE byteBUFFER[256] = { 0 };
            DWORD regSize = sizeof(byteBUFFER);
            DWORD regType = REG_BINARY;
            LONG lResult = RegQueryValueExW(devKEY, L"EDID", NULL, &regType, byteBUFFER, &regSize);
            if (lResult != ERROR_SUCCESS) {
                std::wcout << "!ERROR: " << lResult << std::endl;
            }
            else
            {
                for (short i = 0; i < sizeof(byteBUFFER); i++)
                    std::wcout << std::hex << std::setfill(L'0') << std::setw(2) << int(byteBUFFER[i]) << L" ";
                std::wcout << std::endl;
            }
        }
    }

    delete[] ptrGUID;
    ptrGUID = nullptr;
    std::cin.get();
    return 0;
}

std::wstring GetHKEY(HKEY key)
{
    std::wstring keyPath;
    if (key != NULL)
    {
        HMODULE dll = LoadLibraryW(L"ntdll.dll");
        if (dll != NULL) {
            typedef NTSTATUS(__stdcall* NtQueryKeyType)(
                    HANDLE  KeyHandle,
                    int KeyInformationClass,
                    PVOID  KeyInformation,
                    ULONG  Length,
                    PULONG  ResultLength);

            NtQueryKeyType func = reinterpret_cast<NtQueryKeyType>(::GetProcAddress(dll, "NtQueryKey"));

            if (func != NULL) {
                DWORD size = 0;
                DWORD result = 0;
                result = func(key, 3, 0, 0, &size);
                if (result == ((NTSTATUS)0xC0000023L))
                {
                    size = size + 2;
                    wchar_t* buffer = new (std::nothrow) wchar_t[size / sizeof(wchar_t)];
                    if (buffer != NULL)
                    {
                        result = func(key, 3, buffer, size, &size);
                        if (result == ((NTSTATUS)0x00000000L))
                        {
                            buffer[size / sizeof(wchar_t)] = L'\0';
                            keyPath = std::wstring(buffer + 2);
                        }

                        delete[] buffer;
                    }
                }
            }

            FreeLibrary(dll);
        }
    }

    return keyPath;
}

可以直接运行,运行之后会获得

 这样的一堆16进制数,然后可以放入网站进行解析。推荐一个解析地址:

EDID Decode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值