winapi - Win32 kode for at få EDID i Windows XP/7

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg fandt denne side og kunne ikke få nyttige oplysninger ud af det (det søger i registreringsdatabasen for noget, men finder det aldrig og går i en uendelig sløjfe). [4]


Da dette spørgsmål om at få monitorens serielle UID ('EDID information') spørger, vil jeg gerne have de samme oplysninger, men set ud fra Win32 C-koden (eller C/C ++ DDK-kode eller hvad som helst) i stedet for Linux.]] [6]

Bedste reference


WMI understøttede ikke overvågningsklasser i Windows XP.
Den dokumenterede måde at få EDID på var - og stadig er - med Setup API.


Langere undersøgelse og en VC ++-kodeeksempel er tilgængelige her. [7]

Andre referencer 1


Først fik jeg en C # version, der arbejder med WMI Code Creator: [8]


        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);
        }


Nedenfor er C ++-koden, jeg fandt og tweaked for at arbejde med feltet InstanceName i WmiMonitorID klasse (EDID struktur), som jeg ønskede at læse. Glem ikke at tilføje setupapi.lib til din linker> Yderligere biblioteker bygge indstilling. [9] [10]


#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 rootcimv2 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);
        wcout << "----------------" << endl << "InstanceName : " << vtProp.bstrVal << endl;
        VariantClear(&vtProp);



        pclsObj->Release();
    }

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

    return 0;
}

Andre referencer 2


Baseret på Ofek Shilons blog-indlæg og tweaked for at få alle enheds-id'er (producent ID + produkt-ID-strenge):


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 --------
", DeviceID.utf8());
    }

    devMon++;

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

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


N. B. Str her er en brugerdefineret streng klasse, men bør være let at refactor at bruge noget.