Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

Just like the title says, I am wanting to know if it is possible to determine if an HDMI device is connected using C#.

like image 413
Eric Packwood Avatar asked Aug 30 '10 13:08

Eric Packwood


People also ask

Can HDMI be detected?

Manually Detect Display Step 1: Launch the Windows Settings Menu and select System. Step 2: On the left-hand pane, select Display. Step 3: Scroll to the Multiple displays section and tap the Detect button. Now check if your PC detects the connected HDMI monitor.

How do I know if my HDMI is connected?

Look at the status of your HDMI port in the dialog box. If the status reads "This device is working properly," your HDMI port is functioning. If there is a message indicating that you need to troubleshoot your device, your HDMI port is in working condition but has a problem that is likely fixable.

How can you tell if HDMI cable is in and out?

Note that the "HDMI in" ports will be labeled "In" or may be designated by labels such as "Input 1" or "Input A." The "HDMI out" ports will be labeled "Out" or designated with labels like "Output 1" or "Output A."


1 Answers

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

like image 138
Sorry IwontTell Avatar answered Oct 02 '22 12:10

Sorry IwontTell