在Windows 7/8/10 Win32 C ++中检测/识别显示器连接的端口(HDMI,其他)

我能够检测到所有连接到我的系统的显示器。 我能够使用以下Windows API调用获取大量的相关信息:

EnumDisplayMonitors

GetMonitorInfo

EnumDisplayDevices

我正在开发的系统是带2个显示器的AIO(一体机)。 它有一个外部HDMI输出,我可以连接和外部显示器。 然后,通过端口复制器,我可以连接第四台显示器。 所有的显示器扩展我的桌面,他们都工作正常。

我的任务是检测通过HDMI端口连接的显示器。 我能够使用上述API提取的监视器信息不会提供任何特定的信息,通过它我将能够检测端口types(例如HDMI或USB)。

有谁知道使用什么API? 谢谢!

Solutions Collecting From Web of "在Windows 7/8/10 Win32 C ++中检测/识别显示器连接的端口(HDMI,其他)"

您可以使用EnumDisplayDevices方法获取监视器的DeviceID ,然后使用WmiMonitorConnectionParams WMI类(从Windows Vista开始可用)和VideoOutputTechnology属性,该属性返回D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY枚举。

尝试下一个示例代码

 #include "stdafx.h" #include <atlstr.h> #define _WIN32_DCOM #include <iostream> using namespace std; #include <comdef.h> #include <Wbemidl.h> # pragma comment(lib, "wbemuuid.lib") typedef enum _D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY { D3DKMDT_VOT_UNINITIALIZED = -2, D3DKMDT_VOT_OTHER = -1, D3DKMDT_VOT_HD15 = 0, D3DKMDT_VOT_SVIDEO = 1, D3DKMDT_VOT_COMPOSITE_VIDEO = 2, D3DKMDT_VOT_COMPONENT_VIDEO = 3, D3DKMDT_VOT_DVI = 4, D3DKMDT_VOT_HDMI = 5, D3DKMDT_VOT_LVDS = 6, D3DKMDT_VOT_D_JPN = 8, D3DKMDT_VOT_SDI = 9, D3DKMDT_VOT_DISPLAYPORT_EXTERNAL = 10, D3DKMDT_VOT_DISPLAYPORT_EMBEDDED = 11, D3DKMDT_VOT_UDI_EXTERNAL = 12, D3DKMDT_VOT_UDI_EMBEDDED = 13, D3DKMDT_VOT_SDTVDONGLE = 14, #if (DXGKDDI_INTERFACE_VERSION >= DXGKDDI_INTERFACE_VERSION_WDDM1_3_M1) D3DKMDT_VOT_MIRACAST = 15, #endif D3DKMDT_VOT_INTERNAL = 0x80000000, D3DKMDT_VOT_SVIDEO_4PIN = D3DKMDT_VOT_SVIDEO, D3DKMDT_VOT_SVIDEO_7PIN = D3DKMDT_VOT_SVIDEO, D3DKMDT_VOT_RF = D3DKMDT_VOT_COMPOSITE_VIDEO, D3DKMDT_VOT_RCA_3COMPONENT = D3DKMDT_VOT_COMPONENT_VIDEO, D3DKMDT_VOT_BNC = D3DKMDT_VOT_COMPONENT_VIDEO } D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY; _D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY GetConnectorInfo(LPCWSTR ShortDeviceID) { _D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY result = D3DKMDT_VOT_OTHER; BSTR strNetworkResource; strNetworkResource = L"\\\\.\\root\\WMI"; HRESULT hres; hres = CoInitializeEx(0, COINIT_MULTITHREADED); if (FAILED(hres)) { cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; cout << _com_error(hres).ErrorMessage() << endl; cout << "press enter to exit" << endl; cin.get(); std::exit(1); } hres = CoInitializeSecurity( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved ); if (FAILED(hres)) { cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl; cout << _com_error(hres).ErrorMessage() << endl; CoUninitialize(); cout << "press enter to exit" << endl; cin.get(); std::exit(1); } IWbemLocator *pLoc = NULL; hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc); if (FAILED(hres)) { cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl; cout << _com_error(hres).ErrorMessage() << endl; CoUninitialize(); cout << "press enter to exit" << endl; cin.get(); std::exit(1); } IWbemServices *pSvc = NULL; hres = pLoc->Connectserver( _bstr_t(strNetworkResource), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (eg Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED(hres)) { cout << "Could not connect. Error code = 0x" << hex << hres << endl; cout << _com_error(hres).ErrorMessage() << endl; pLoc->Release(); CoUninitialize(); cout << "press enter to exit" << endl; cin.get(); std::exit(1); } hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED(hres)) { cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl; cout << _com_error(hres).ErrorMessage() << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); cout << "press enter to exit" << endl; cin.get(); std::exit(1); } IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery(L"WQL", L"SELECT InstanceName, VideoOutputTechnology FROM WmiMonitorConnectionParams", WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { cout << "ExecQuery failed" << " Error code = 0x" << hex << hres << endl; cout << _com_error(hres).ErrorMessage() << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); cout << "press enter to exit" << endl; cin.get(); std::exit(1); } IWbemClassObject *pclsObj = NULL; ULONG uReturn = 0; BOOL bFound = false; while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if (0 == uReturn || FAILED(hr)) break; VARIANT vtProp; CString DeviceID; hr = pclsObj->Get(L"InstanceName", 0, &vtProp, 0, 0);// String if (!FAILED(hr)) { if ((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)) { } else { DeviceID.Format(L"%s", vtProp.bstrVal); DeviceID = DeviceID.Mid(8, DeviceID.Find(L"\\", 9) - 8); bFound = DeviceID.Compare(ShortDeviceID) == 0; } } VariantClear(&vtProp); if (bFound) { hr = pclsObj->Get(L"VideoOutputTechnology", 0, &vtProp, 0, 0);// Uint32 if (!FAILED(hr)) { if ((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)) result = D3DKMDT_VOT_OTHER; else { result = (_D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY)vtProp.uintVal; } } } VariantClear(&vtProp); pclsObj->Release(); pclsObj = NULL; if (bFound) break; } pSvc->Release(); pLoc->Release(); pEnumerator->Release(); if (pclsObj != NULL) pclsObj->Release(); CoUninitialize(); return result; } void GetDisplayInfo() { DISPLAY_DEVICE lpDisplayDevice; lpDisplayDevice.cb = sizeof(lpDisplayDevice); DWORD iDevNum = 0; CString DeviceID; while (EnumDisplayDevices(0, iDevNum, &lpDisplayDevice, 0)) { DISPLAY_DEVICE lpDisplayDevice2; ZeroMemory(&lpDisplayDevice2, sizeof(lpDisplayDevice2)); lpDisplayDevice2.cb = sizeof(lpDisplayDevice2); DWORD devMon = 0; while (EnumDisplayDevices(lpDisplayDevice.DeviceName, devMon, &lpDisplayDevice2, 0)) { if (lpDisplayDevice2.StateFlags & DISPLAY_DEVICE_ACTIVE && !(lpDisplayDevice2.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER)) { DeviceID.Format(L"%s", lpDisplayDevice2.DeviceID); wcout << DeviceID.GetString() << endl; DeviceID = DeviceID.Mid(8, DeviceID.Find(L"\\", 9) - 8); _D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY VideoOutputTechnology = GetConnectorInfo(DeviceID.GetString()); switch (VideoOutputTechnology) { case D3DKMDT_VOT_HDMI: wcout << " VideoOutputTechnology : HDMI" << endl; break; case D3DKMDT_VOT_INTERNAL: wcout << " VideoOutputTechnology : Internal Monitor" << endl; break; default: wcout << " VideoOutputTechnology : " << VideoOutputTechnology << endl; break; } } devMon++; ZeroMemory(&lpDisplayDevice2, sizeof(lpDisplayDevice2)); lpDisplayDevice2.cb = sizeof(lpDisplayDevice2); } ZeroMemory(&lpDisplayDevice, sizeof(lpDisplayDevice)); lpDisplayDevice.cb = sizeof(lpDisplayDevice); iDevNum++; } } int main(int argc, char* argv[]) { GetDisplayInfo(); cout << "press enter to exit" << endl; cin.get(); return 0; } 

这将返回类似的东西

 MONITOR\CMN15BB\{4d36e96e-e325-11ce-bfc1-08002be10318}\0001 VideoOutputTechnology : Internal Monitor MONITOR\SNY7702\{4d36e96e-e325-11ce-bfc1-08002be10318}\0000 VideoOutputTechnology : HDMI