在Windows XP/7中获取EDID的Win32代码

3

参考下方代码,用于在Mac OS X中完成类似任务的代码:https://dev59.com/2XM_5IYBdhLWcg3ww2Kb - Jared Updike
3个回答

3

首先我使用WMI Code Creator编写了一个C#版本:

        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\WMI",
                "SELECT * FROM WmiMonitorID");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("WmiMonitorID instance");
                Console.WriteLine("-----------------------------------");
                //Console.WriteLine("Active: {0}", queryObj["Active"]);
                Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
                dynamic snid = queryObj["SerialNumberID"];

                Console.WriteLine("SerialNumberID: (length) {0}", snid.Length);
                Console.WriteLine("YearOfManufacture: {0}", queryObj["YearOfManufacture"]);
                dynamic code = queryObj["ProductCodeID"];
                string pcid = "";
                for (int i = 0; i < code.Length; i++)
                {
                    pcid = pcid + Char.ConvertFromUtf32(code[i]);
                    //pcid = pcid +code[i].ToString("X4");
                }
                Console.WriteLine("ProductCodeID: " +  pcid);
            }
        }
        catch (ManagementException e)
        {
            Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
        }

以下是我找到并调整以便与WmiMonitorID类(EDID结构)中的InstanceName字段一起使用的C ++代码。不要忘记将setupapi.lib添加到您的链接器>附加库构建设置中。
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")

int EnumMonitorIDs()
{
    ret.clear();

    HRESULT hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x" 
            << hex << hres << endl;
        return 1;                  // Program has failed.
    }

    hres =  CoInitializeSecurity(
        NULL, 
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );

    if (FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x" 
            << hex << hres << endl;
        CoUninitialize();
        return 1;
    }

    IWbemLocator *pLoc = NULL;
    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);
 
    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    IWbemServices *pSvc = NULL; 
    BSTR AbackB = SysAllocString(L"root\\WMI");
    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         AbackB, // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );
    SysFreeString(AbackB);

    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }

    BSTR wql = SysAllocString(L"WQL");
    BSTR select = SysAllocString(L"SELECT * FROM WmiMonitorID");
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        wql,
        select,
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);
    SysFreeString(wql);
    SysFreeString(select);

    if (FAILED(hres))
    {
        cout << "Query for operating system name failed."
            << " Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    IWbemClassObject *pclsObj = 0;
    ULONG uReturn = 0;   
    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
            &pclsObj, &uReturn);

        if (0 == uReturn)
        {
            break;
        }

        // ok, we have the EDID record, pull some fields out of it

        VARIANT vtProp;
        hr = pclsObj->Get(L"InstanceName", 0, &vtProp, 0, 0);
        if (SUCCEEDED(hr))
        {
            wcout << "----------------" << endl << "InstanceName : " << vtProp.bstrVal << endl;
            VariantClear(&vtProp);
        }       


        pclsObj->Release();
    }

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();

    return 0;
}

3

在Windows XP中,WMI不支持监视器类。获取EDID的文档方式仍然是使用Setup API。

更详细的调查和VC++代码示例可在此处获得。


GetSizeForDevID(const CString& TargetDevID, ...)为什么不使用TargetDevID的值?你在多显示器设置或非镜像模式下的笔记本电脑上测试过吗?(例如,扩展桌面模式)。顺便说一句,谢谢提供链接。 - Jared Updike
所以你推荐的获取EDID的方法在我扩展桌面时不起作用;除非我切换到镜像模式,否则我只能检索到内部LCD的EDID。所以我的WMI方法虽然脆弱且有点反常,但对于我的目的来说仍然更优越,即了解扩展或镜像模式下连接显示器的产品ID/制造商ID。这让我感到很难过,因为我需要一个更可靠的非WMI方法。 - Jared Updike
我收回之前的说法,这个方法确实有效,使用双重 while 循环 (在 EnumDisplayDevices(...) 中循环),当我去掉 while 检查中的 bFoundDevice 部分(请参阅我的其他答案)时。 - Jared Updike
抱歉回复晚了 - 是的,当时我只对第一个有效的EDID感兴趣。 - Ofek Shilon

1

基于Ofek Shilon的博客文章,并进行调整以获取所有设备ID(制造商ID +产品ID字符串):

DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
DWORD dev = 0;  // device index
int id = 1;        // monitor number, as used by Display Properties > Settings

Str DeviceID;

while (EnumDisplayDevices(0, dev, &dd, 0))
{
    DISPLAY_DEVICE ddMon;
    ZeroMemory(&ddMon, sizeof(ddMon));
    ddMon.cb = sizeof(ddMon);
    DWORD devMon = 0;

    while (EnumDisplayDevices(dd.DeviceName, devMon, &ddMon, 0))
    {
        DeviceID.Sprintf("%s", ddMon.DeviceID);
        DeviceID = DeviceID.Slice(8);
        if (DeviceID.Index("\\") > 0)
            DeviceID = DeviceID.Slice(0, DeviceID.Index("\\"));

        printf ("DEVICEID = %s --------\n", DeviceID.utf8());
    }

    devMon++;

    ZeroMemory(&ddMon, sizeof(ddMon));
    ddMon.cb = sizeof(ddMon);
  }

  ZeroMemory(&dd, sizeof(dd));
  dd.cb = sizeof(dd);
  dev++;
}

注意:这里的Str是一个自定义字符串类,但可以很容易地重构为使用任何其他类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接