gpt4 book ai didi

c# - 是否可以检测是否有使用 C# 连接的 HDMI 设备?

转载 作者:可可西里 更新时间:2023-11-01 09:14:39 47 4
gpt4 key购买 nike

正如标题所说,我想知道是否可以使用 C# 确定是否连接了 HDMI 设备。

最佳答案

我想出了 powershell 解决方案:

$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"

这将列出编号。已连接的 HDMI 线缆
它会抓取所有显示器并仅计算来自 VideoOutputTechnologyHDMI。 HDMI 总是有 5 个值。
学分:here

更新:1个c#代码:

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());

原因:WmiMonitorConnectionParams数组返回外接显示器的数量及其信息,包括VideoOutputTechnologyInstanceNameActive。我们需要VideoOutputTechnology来检查value是否为5,然后进行计数。 D3DKMDT_VOT_HDMI=5 积分:learn.microsoft.com & comment & wutils.com. .

更新:2 vbscript代码:

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

与 c# 代码中解释的意识形态相同。仅适用于 visual basicvbscriptVBAvbs 等。致谢:wutils.com .

更新:3 C++代码

#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;
}

致谢:here

关于c# - 是否可以检测是否有使用 C# 连接的 HDMI 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3601012/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com