You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#检测机顶盒HDMI连接异常:WPF嵌入Windows Media Player场景求助

问题分析

原代码依赖WmiMonitorConnectionParams WMI类检测HDMI连接,该类仅枚举系统识别为"显示器"的设备。机顶盒作为主输出时,可能被系统归类为其他设备类型,或者该WMI类在机顶盒环境下无法正确获取实例。此外,代码仅判断VideoOutputTechnology=5(标准HDMI),但部分HDMI衍生接口(如嵌入式HDMI)对应其他枚举值,导致漏判。

代码修改建议

方案1:扩展HDMI相关枚举值判断

保留原WMI类,扩大VideoOutputTechnology的匹配范围,覆盖更多HDMI相关类型:

public static int HdmiConnectionStatus()
{
    int HDMI_Monitors = 0;
    // 定义所有HDMI相关的VideoOutputTechnology枚举值
    var hdmiTechValues = new HashSet<int> { 5, 15, 13 }; // 5=HDMI,15=嵌入式HDMI,13=MHL
    ManagementClass mClass = new ManagementClass(@"\\localhost\ROOT\WMI:WmiMonitorConnectionParams");
    Log.Info("HdmiConnectionStatus ManagementClass ");
    
    if(mClass != null)
    {
        Log.Info(mClass.ToString());
        foreach (ManagementObject mObject in mClass.GetInstances())
        {
            var ss = mObject["VideoOutputTechnology"];
            Log.Info("HdmiConnectionStatus HDMI port Info :" + ss);
            if (int.TryParse(ss.ToString(), out int techValue))
            {
                if (hdmiTechValues.Contains(techValue))
                {
                    HDMI_Monitors += 1;
                }
            }
        }
    }
    else
    {
        Log.Info(" HdmiConnectionStatus Null ManagementClass ");
    }

    return HDMI_Monitors;
}

方案2:改用更通用的WMI类组合检测

如果方案1仍无效,改用Win32_VideoController和Win32_DesktopMonitor组合,这类WMI类对机顶盒等嵌入式设备的兼容性更好:

public static int HdmiConnectionStatus()
{
    int hdmiCount = 0;
    // 先获取视频控制器的输出信息
    ManagementClass videoControllerClass = new ManagementClass("Win32_VideoController");
    foreach (ManagementObject controller in videoControllerClass.GetInstances())
    {
        string adapterType = controller["AdapterType"]?.ToString() ?? "";
        string videoModeDescription = controller["VideoModeDescription"]?.ToString() ?? "";
        // 判断控制器是否支持HDMI输出
        if (adapterType.Contains("HDMI") || videoModeDescription.Contains("HDMI"))
        {
            // 再关联检测对应的显示器是否连接
            ManagementObjectCollection monitors = controller.GetRelated("Win32_DesktopMonitor");
            foreach (ManagementObject monitor in monitors)
            {
                bool isConnected = Convert.ToInt32(monitor["Availability"]) == 3; // 3=运行中/已连接
                if (isConnected)
                {
                    hdmiCount++;
                }
            }
        }
    }
    return hdmiCount;
}

额外注意事项

  • 确保程序以管理员权限运行,WMI部分类需要权限才能访问完整设备信息。
  • 机顶盒环境下,可能需要禁用系统的"显示器检测优化",确保所有输出设备被WMI正确枚举。

内容的提问来源于stack exchange,提问作者Nation

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:36:27