Sign in to follow this  
Followers 0
norry

Get attached monitor information

6 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Can this return the current resolution of the connected monitors?

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

nice tool, there was alot of information to be read out with this :) However, the Win32_VideoController only returned resolutions for one monitor.

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0