http://thetweaker.wordpress.com/2011/11/13/reading-monitor-physical-dimensions-or-getting-the-edid-the-right-way/
002 |
#include <SetupApi.h> |
003 |
#pragma comment(lib, "setupapi.lib") |
005 |
#define NAME_SIZE 128 |
007 |
const GUID GUID_CLASS_MONITOR = {0x4d36e96e, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}; |
010 |
bool GetMonitorSizeFromEDID( const HKEY hDevRegKey, short & WidthMm, short & HeightMm) |
012 |
DWORD dwType, AcutalValueNameLength = NAME_SIZE; |
013 |
TCHAR valueName[NAME_SIZE]; |
016 |
DWORD edidsize= sizeof (EDIDdata); |
018 |
for ( LONG i = 0, retValue = ERROR_SUCCESS; retValue != ERROR_NO_MORE_ITEMS; ++i) |
020 |
retValue = RegEnumValue ( hDevRegKey, i, &valueName[0], |
021 |
&AcutalValueNameLength, NULL, &dwType, |
025 |
if (retValue != ERROR_SUCCESS || 0 != _tcscmp(valueName,_T( "EDID" ))) |
028 |
WidthMm = ((EDIDdata[68] & 0xF0) << 4) + EDIDdata[66]; |
029 |
HeightMm = ((EDIDdata[68] & 0x0F) << 8) + EDIDdata[67]; |
037 |
bool GetSizeForDevID( const CString& TargetDevID, short & WidthMm, short & HeightMm) |
039 |
HDEVINFO devInfo = SetupDiGetClassDevsEx( |
053 |
for ( ULONG i=0; ERROR_NO_MORE_ITEMS != GetLastError(); ++i) |
055 |
SP_DEVINFO_DATA devInfoData; |
056 |
memset (&devInfoData,0, sizeof (devInfoData)); |
057 |
devInfoData.cbSize = sizeof (devInfoData); |
059 |
if (SetupDiEnumDeviceInfo(devInfo,i,&devInfoData)) |
061 |
HKEY hDevRegKey = SetupDiOpenDevRegKey(devInfo,&devInfoData, |
062 |
DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); |
064 |
if (!hDevRegKey || (hDevRegKey == INVALID_HANDLE_VALUE)) |
067 |
bRes = GetMonitorSizeFromEDID(hDevRegKey, WidthMm, HeightMm); |
069 |
RegCloseKey(hDevRegKey); |
072 |
SetupDiDestroyDeviceInfoList(devInfo); |
076 |
int _tmain( int argc, _TCHAR* argv[]) |
078 |
short WidthMm, HeightMm; |
086 |
bool bFoundDevice = false ; |
087 |
while (EnumDisplayDevices(0, dev, &dd, 0) && !bFoundDevice) |
089 |
DISPLAY_DEVICE ddMon; |
090 |
ZeroMemory(&ddMon, sizeof (ddMon)); |
091 |
ddMon.cb = sizeof (ddMon); |
094 |
while (EnumDisplayDevices(dd.DeviceName, devMon, &ddMon, 0) && !bFoundDevice) |
096 |
if (ddMon.StateFlags & DISPLAY_DEVICE_ACTIVE && |
097 |
!(ddMon.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER)) |
099 |
DeviceID.Format (L "%s" , ddMon.DeviceID); |
100 |
DeviceID = DeviceID.Mid (8, DeviceID.Find (L "\\" , 9) - 8); |
102 |
bFoundDevice = GetSizeForDevID(DeviceID, WidthMm, HeightMm); |
106 |
ZeroMemory(&ddMon, sizeof (ddMon)); |
107 |
ddMon.cb = sizeof (ddMon); |
110 |
ZeroMemory(&dd, sizeof (dd)); |