Is it possible to detect if there is an HDMI device connected using C#?

I think it is possible. you can use the device manager to find out whether a HDMI cable is attached. I had worked on a code to find whether USB is connected and also whether a monitor is connected or not. if u notice any change in the device manager when the HDMI cable is plugged in, you can use that to detect HDMI


I don't think there is a single API (like DirectX) that would allow you to query for that directly. You'd probably need to write some custom routines for ATI/NVidia/etc. cards for that and wrap it into your own mini-API.


I came up with powershell solution:

$HDMI_Monitors = 0;
$wmiobject = (get-wmiobject -namespace root\WMI -computername localhost -Query "Select * from WmiMonitorConnectionParams")
foreach ($letter in $wmiobject)
{
    if($letter["VideoOutputTechnology"] -eq 5) #HDMI cable have value of 5 
    {
        HDMI_Monitors += 1;
    }
}
Write-Host "Number of connected HDMI cables : $HDMI_Monitors"

This will list no. of connected HDMI cables.
It crawls all displays and count only HDMI from VideoOutputTechnology. HDMI always have 5 value.
Credits:here

Update:1 c# code:

int HDMI_Monitors = 0;
ManagementClass mClass = new ManagementClass(@"\\localhost\ROOT\WMI:WmiMonitorConnectionParams");
foreach (ManagementObject mObject in mClass.GetInstances())
{
    if (mObject["VideoOutputTechnology"].Equals(5)) //Because D3DKMDT_VOT_HDMI = 5
    {
        HDMI_Monitors += 1;
    }
}
    Console.WriteLine("Number of connected HDMI cables : " + HDMI_Monitors.ToString());

Reason:WmiMonitorConnectionParams array returns number of external displays and their info including VideoOutputTechnology,InstanceName,Active. We need VideoOutputTechnology to check whether value is 5 or not and then count it. D3DKMDT_VOT_HDMI=5 Credits:docs.microsoft.com & comment & wutils.com..

Update:2 vbscript code:

Dim HDMI_Monitors 
HDMI_Monitors = 0
For Each Instance In GetObject("WINMGMTS:\\localhost\ROOT\WMI").InstancesOf("WmiMonitorConnectionParams", 1) 
    If Instance.VideoOutputTechnology = 5 Then 
        HDMI_Monitors =  + 1
    End if
Next 
Wscript.Echo "No. of connected HDMI cables :" & HDMI_Monitors 

Same ideology as explained in c# code. Just for visual basic ,vbscript,VBA,vbs etc. Credits: wutils.com.

Update: 3 C++ code

#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")

int main()
{
    int HDMI_Monitors  = 0;
    IWbemLocator *pLoc = NULL;
    IWbemServices *pSvc = NULL;
    IEnumWbemClassObject *pEnumerator = NULL;
    IWbemClassObject *pclsObj = NULL;
    ULONG uReturn = 0;
    HRESULT hres = NULL;
    
    hres = CoInitializeEx (0, COINIT_MULTITHREADED);
    hres = CoInitializeSecurity (NULL, -1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);
    hres = CoCreateInstance (CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,IID_IWbemLocator, (LPVOID *) & pLoc);
    hres = pLoc->ConnectServer (_bstr_t (L"\\\\localhost\\root\\WMI"),NULL,NULL,0,NULL,0,0,&pSvc);
    hres = CoSetProxyBlanket (pSvc,RPC_C_AUTHN_WINNT,RPC_C_AUTHZ_NONE,NULL,RPC_C_AUTHN_LEVEL_CALL,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE);
    hres = pSvc->ExecQuery (L"WQL", L"SELECT * FROM WmiMonitorConnectionParams",WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
    
        while (pEnumerator)
        {
            HRESULT hr = pEnumerator->Next (WBEM_INFINITE, 1, &pclsObj, &uReturn);
            if (0 == uReturn || FAILED (hr))
            {
              break;
            }
            
            VARIANT vtProp;
            hr = pclsObj->Get (L"VideoOutputTechnology", 0, &vtProp, 0, 0); 
            if(vtProp.uintVal == 5)
            {
                HDMI_Monitors+=1;
            }
            VariantClear (&vtProp);
            pclsObj->Release ();
            pclsObj = NULL; 
        }
        
        std::cout << "Number of connected HDMI cables : " << HDMI_Monitors;
        return 0;           
}

Credits: here


In a sense you couldn't possibly. They could have a DVI->HDMI connector plugged in so it is plugged in as HDMI but the machine only knows it as DVI, or a hand full of other connection types that can be be dongled (teehee) to HDMI.

Tags:

C#