private static bool GetActualEDID(out string DeviceID, List <ScreenInformation> lsi) { IntPtr pGuid = Marshal.AllocHGlobal(Marshal.SizeOf(GUID_CLASS_MONITOR)); Marshal.StructureToPtr(GUID_CLASS_MONITOR, pGuid, false); IntPtr hDevInfo = SetupDiGetClassDevsEx( pGuid, null, IntPtr.Zero, DIGCF_PRESENT, IntPtr.Zero, null, IntPtr.Zero); DeviceID = string.Empty; if (null == hDevInfo) { Marshal.FreeHGlobal(pGuid); return(false); } for (int i = 0; Marshal.GetLastWin32Error() != ERROR_NO_MORE_ITEMS; ++i) { SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); if (SetupDiEnumDeviceInfo(hDevInfo, i, ref devInfoData) > 0) { UIntPtr hDevRegKey = SetupDiOpenDevRegKey( hDevInfo, ref devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); if (hDevRegKey == null) { continue; } ScreenInformation si = PullEDID(hDevRegKey); if (si != null) { lsi.Add(si); } RegCloseKey(hDevRegKey); } } Marshal.FreeHGlobal(pGuid); return(true); }
private static ScreenInformation PullEDID(UIntPtr hDevRegKey) { ScreenInformation si = null; StringBuilder valueName = new StringBuilder(128); uint ActualValueNameLength = 128; byte[] EDIdata = new byte[1024]; IntPtr pEDIdata = Marshal.AllocHGlobal(EDIdata.Length); Marshal.Copy(EDIdata, 0, pEDIdata, EDIdata.Length); int size = 1024; for (uint i = 0, retValue = ERROR_SUCCESS; retValue != ERROR_NO_MORE_ITEMS; i++) { retValue = RegEnumValue( hDevRegKey, i, valueName, ref ActualValueNameLength, IntPtr.Zero, IntPtr.Zero, pEDIdata, ref size); // EDIdata, pSize); string data = valueName.ToString(); if (retValue != ERROR_SUCCESS || !data.Contains("EDID")) { continue; } if (size < 1) { continue; } byte[] actualData = new byte[size]; Marshal.Copy(pEDIdata, actualData, 0, size); string hex = System.Text.Encoding.ASCII.GetString(actualData); si = new ScreenInformation { Manufacturer = hex.Substring(90, 17).Trim().Replace("\0", string.Empty).Replace("?", string.Empty), Model = hex.Substring(108, 17).Trim().Replace("\0", string.Empty).Replace("?", string.Empty), FullEDID = BitConverter.ToString(actualData) }; } Marshal.FreeHGlobal(pEDIdata); return(si); }