There is no generic solution to this problem, so you need to specify which O/S, inc. version, etc you are using.
And so it would probably be better to re-ask the question in the appropriate, system specific forum. Or move this thread there.
PS One of the starting points in the Windows world is the EnumDisplayDevices() call. But I think there might be a more modern way to do it for newer versions of Windows, but I'm not sure. And I have no idea what the Linux, Android, etc solution is.