2011-06-15 136 views
2

我發現this頁面,並且無法獲得任何有用的信息(它在註冊表中搜索某些內容,但從未找到它並進入無限循環)。Win32代碼在Windows XP/7中獲取EDID

由於this question regarding getting the monitor serial UID ("EDID information")問我想從Win32 C代碼(或C/C++ DDK代碼或其他)代替Linux的角度來看同樣的信息。

+0

僅供參考,代碼爲Mac OS X中的類似任務:http://stackoverflow.com/questions/1236498/how-to-get-the-display-name-with-the-display-id-in-mac -os-x – 2011-06-23 18:55:46

回答

3

WMI不支持Windows XP中的監視器類。 獲得EDID的記錄方式是 - 並且仍然是 - 使用安裝程序API。

更長的調查和一個VC++代碼示例are available here

+0

GetSizeForDevID(const CString&TargetDevID,...)如何不使用TargetDevID的值?你是否在多監視器設置或非鏡像模式的筆記本電腦上測試了這個? (例如,擴展桌面模式)。感謝您的鏈接。 – 2011-11-28 21:09:51

+0

因此,當我擴展桌面時,您推薦的獲取EDID的方法不起作用;除非我切換到鏡像模式,否則內部LCD是我可以檢索的唯一EDID。因此,我的WMI方法雖然脆弱,但是對於我的目的而言,它仍然是優越的,即在擴展模式或鏡像模式下了解連接監視器的產品ID /製造商ID。這讓我很難過,因爲我需要非可靠的非WMI方法。 – 2011-11-28 23:11:15

+0

我拿回來了,這個方法確實有效,用while(EnumDisplayDevices(...))循環,當我取出while檢查的bFoundDevice部分時(參見我的其他答案) – 2011-11-28 23:53:03

3

首先我有一個C#版本使用WMI Code Creator工作:

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

下面是C++代碼我found和調整在WmiMonitorID類(EDID結構),我想讀與實例名外地工作。不要忘記將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); 
     wcout << "----------------" << endl << "InstanceName : " << vtProp.bstrVal << endl; 
     VariantClear(&vtProp); 



     pclsObj->Release(); 
    } 

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

    return 0; 
} 
0

基於奧菲克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是一個自定義的字符串類,但應該很容易重構使用任何東西。