Jump to content

Get attached monitor information


norry
 Share

Recommended Posts

I recently posted this in reply to a Help thread but wanted to share it here so I could hopefully get some feedback on how I could write my code better?

I haven't seen this approach when trying to query attached monitors and it has the added advantage that when a monitor is removed or replaced the information is updated. In other approaches the information was read from the registry and that isn't refreshed straight away.

Example returned info

Monitors Connected:

Manufacturer: LPL ProductCode: DE00 Serial: 0 Built: 0/2007

VW202 Manufacturer: ACI ProductCode: 20A2 Serial: 71LB066000 Built: 14/2007

DELL P2310H Manufacturer: DEL ProductCode: D028 Serial: N920R02A0CNL Built: 6/2010

Note the first monitor returned that does not have a friendlyname; this is because it's the laptop screen.

ConsoleWrite(_MonitorInfo())

Func _MonitorInfo()
Local $WMIWmiMonitorID
Local $sManufacturerName, $sSerialNumberID, $YearOfManufacture, $WeekOfManufacture, $sUserFriendlyName
Local $i
Local $sMonitorInfo
Local $strcomputer = "."
Local $objWMIServiceWMI = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!" & $strcomputer & "rootWMI")
;WmiMonitorID not supported on xp etc.
If @OSVersion <> "WIN_XP" And @OSVersion <> "WIN_XPe" And @OSVersion <> "WIN_2000" And @OSVersion <> "WIN_2003" Then
  If IsObj($objWMIServiceWMI) = 1 Then
   $WMIWmiMonitorID = $objWMIServiceWMI.ExecQuery("SELECT * FROM WmiMonitorID WHERE Active='True'")
   $sMonitorInfo = @CRLF & "Monitors Connected:  " & @CRLF
   For $objComputer In $WMIWmiMonitorID
    $WeekOfManufacture = $objComputer.WeekOfManufacture
    $YearOfManufacture = $objComputer.YearOfManufacture
    For $i = 0 To UBound($objComputer.UserFriendlyName) - 1
     $sUserFriendlyName = $objComputer.UserFriendlyName($i)
     If StringLen($sUserFriendlyName) <> 0 Then
      $sMonitorInfo = $sMonitorInfo & String(Chr($sUserFriendlyName))
     EndIf
    Next
    $sMonitorInfo = $sMonitorInfo & " Manufacturer: "
    For $i = 0 To UBound($objComputer.ManufacturerName) - 1
     $sManufacturerName = $objComputer.ManufacturerName($i)
     If StringLen($sManufacturerName) <> 0 Then
      ;Returns a character corresponding to an ASCII code.
      $sMonitorInfo = $sMonitorInfo & String(Chr($sManufacturerName))
     EndIf
    Next
    $sMonitorInfo = $sMonitorInfo & " ProductCode: "
    For $i = 0 To UBound($objComputer.ProductCodeID) - 1
     $sProductCodeID = $objComputer.ProductCodeID($i)
     If StringLen($sProductCodeID) <> 0 Then
      $sMonitorInfo = $sMonitorInfo & String(Chr($sProductCodeID))
     EndIf
    Next
    $sMonitorInfo = $sMonitorInfo & " Serial: "
    For $i = 0 To UBound($objComputer.SerialNumberID) - 1
     $sSerialNumberID = $objComputer.SerialNumberID($i)
     If StringLen($sSerialNumberID) <> 0 Then
      $sMonitorInfo = $sMonitorInfo & String(Chr($sSerialNumberID))
     EndIf
    Next
    $sMonitorInfo = $sMonitorInfo & " Built: " & $WeekOfManufacture & "/" & $YearOfManufacture & @CRLF
   Next
  Else
   $sMonitorInfo = "Error: Not an Object" & @CRLF
  EndIf
EndIf
Return $sMonitorInfo
EndFunc   ;==>_MonitorInfo
Edited by norry
Link to comment
Share on other sites

  • 1 year later...

This script works on WIN_7 with one minor tweak.  Add backslashes in the appropriate places of line 9.  It should read like this:

    Local $objWMIServiceWMI = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strcomputer & "\root\WMI")

Also, $sProductCodeID was not defined as a local variable.  Obviously this is only a problem if the AutoIt option MustDeclareVars is set though.

Edited by BrunoJ
Link to comment
Share on other sites

Run ScriptoMatic (can be found in the Example Scripts forum) and select class "Win32_VideoController" for the horizontal and vertical resolution.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

WMIMonitorId only returns very few information. No resolutionn information.

All other Monitor classes seem to not provide the information as well.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...