Jump to content

Code Study (Get Information from Monitor)


Go to solution Solved by argumentum,

Recommended Posts

Good evening to everyone on the forum.

I was guided to start a new topic and not comment on old ones, so I opened this one.

I'm studying the code found, but I couldn't make it work for more than one monitor.

I tried adding loop, while, for and others, and nothing worked.

Could you teach me where to do it?

Code Study (Get Information from Monitor)

Link to comment
Share on other sites

16 hours ago, Nine said:

Maybe try my monitor management example (see my signature).

@Nine

 

I translated it into my language and asked to look at your signature, what would that be?

I checked the 214 pages of your actions on the forum and didn't find it.

Can you teach me what it is and where to find it?

Link to comment
Share on other sites

Thank you very much, your code solves me partially, but I will study to learn.

The code shown below gives me the advantage of getting this information remotely or locally.

And that's when I can't loop to all monitors.

 

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$strComputer = InputBox("Computer Name", "Enter Computer Name:", StringLower(@ComputerName), "", 100, 120)
If @error = 1 Then Exit

If Ping($strComputer, 200) = 0 Then
    MsgBox(0, "Error", $strComputer & " could not be reached.")
    Exit
EndIf

$objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")

If $objWMIService = "" Then
    MsgBox(0, "WMI Error", "WMI not running on " & $strComputer & ".")
    Exit
EndIf

$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_DesktopMonitor", "WQL", _
        $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$j = 0
$msg = ""
If IsObj ($colItems) Then
    For $objItem In $colItems
        $PNPDeviceID = $objItem.PNPDeviceID
        $screenwidth = $objItem.ScreenWidth
        If $screenwidth = 0 Then ExitLoop
        $monedid = RegRead("\\" & $strComputer & "\HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\" & $PNPDeviceID & "\Device Parameters", "EDID")
        Dim $edidarray[StringLen($monedid) ]
        $edidarray[0]= (StringLen($monedid) / 2) + 1
        For $i = 1 To StringLen($monedid) Step 2
            $j = $j + 1
            $edidarray[$j] = Dec(StringMid($monedid, $i, 2))
        Next
    Next
    $ser = _FindMonitorSerial()
    $name = _FindMonitorName()
    MsgBox(0, "Monitor Information", "Monitor Name: " & $name & @CRLF & "Serial Number: " & $ser)
Else
    MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_DesktopMonitor")
EndIf

Func _FindMonitorSerial()
    $sernumstr = ""
    $sernum = 0
    For $i = 1 To (UBound($edidarray) / 2) - 4
        If $edidarray[$i] = "0" And $edidarray[$i + 1] = "0" And $edidarray[$i + 2] = "0" And $edidarray[$i + 3] = "255" And $edidarray[$i + 4] = "0" Then
            $sernum = $i + 4
        EndIf
    Next
    If $sernum <> 0 Then
        $endstr = 0
        $sernumstr = ""
        For $i = 1 To 13
            If $edidarray[$sernum + $i] = "10" Then
                $endstr = 1
            Else
                If $endstr = 0 Then
                    $sernumstr = $sernumstr & Chr($edidarray[$sernum + $i])
                EndIf
            EndIf
        Next
    Else
        $sernumstr = ""
    EndIf
    Return $sernumstr
EndFunc

Func _FindMonitorName()
    $name = 0
    For $i = 1 To (UBound($edidarray) / 2) - 4
        If $edidarray[$i] = "0" And $edidarray[$i + 1] = "0" And $edidarray[$i + 2] = "252" And $edidarray[$i + 3] = "0" Then
            $name = $i + 3
        EndIf
    Next
    If $name <> 0 Then
        $endstr = 0
        $namestr = ""
        For $i = 1 To 13
            If $edidarray[$name + $i] = "10" Then
                $endstr = 1
            Else
                If $endstr = 0 Then
                    $namestr = $namestr & Chr($edidarray[$name + $i])
                EndIf
            EndIf
        Next
    Else
        $namestr = ""
    EndIf
    Return $namestr
EndFunc

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi @Nine
 

Thanks for the patience to teach me.
Searching a lot, I managed to get parts of some codes and change them to my needs.
I'm sharing it here with you and everyone who needs it.

Thanks again and be well!

 

Global $MonCont = 0
    Global $ArrayMonName[] = ["Not found", "Not found", "Not found"] ; An Array
    Global $ArrayMonSerial[] = ["Not found", "Not found", "Not found"] ; An Array

    $MonName01 =  "Not Found"
    $MonSerial01 = "Not Found"
    $MonName02 =  "Not Found"
    $MonSerial02 = "Not Found"
    $MonName03 =  "Not Found"
    $MonSerial03 = "Not Found"
    $MonitorAsset00 = "N/A"
    $MonitorAsset01 = "N/A"
    $MonitorAsset02 = "N/A"


GUICtrlCreateGroup("MonitorID:", 550, 290, 300, 117)


GUICtrlCreateLabel("Fabricante:", 570, 315, 60, 17)
GUICtrlCreateLabel("Serial:", 675, 315, 50, 17)
GUICtrlCreateLabel("Asset:", 775, 315, 50, 17)









FuncMonitorID()

Func FuncMonitorID()


    Local $oComErr, $oWmi, $oInstances

    $oComErr = ObjEvent("AutoIt.Error", com_error_handler)
    #forceref $oComErr

    ;Get WMI object
    $oWmi = ObjGet("winmgmts:\\" & $strComputer & "\root\WMI")
    If Not IsObj($oWmi) Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to create WMI object")

    ;Select object instances
    $oInstances = $oWmi.ExecQuery("SELECT * FROM WMIMonitorID")
    If $oInstances.Count = 0 Then Exit MsgBox($MB_ICONWARNING,"Warning","No objects found")

        $Linha = 340

    ;Display instance properties
    For $oInstance in $oInstances

        $MonCont = $MonCont + 1



        With $oInstance
           ;ConsoleWrite(@CRLF)
           ;ConsoleWrite("InstanceName      = " & .InstanceName      & @CRLF)
           ;ConsoleWrite("YearOfManufacture = " & .YearOfManufacture & @CRLF)
           ;ConsoleWrite("WeekOfManufacture = " & .WeekOfManufacture & @CRLF)

            $ArrayMonName[$MonCont] = convert_int_array_to_string(.UserFriendlyName)
            $ArrayMonSerial[$MonCont] = convert_int_array_to_string(.SerialNumberId)

            ;MsgBox(0,0,$ArrayMonName[$MonCont]&" "&$ArrayMonSerial[$MonCont],0)


            GUICtrlCreateLabel($ArrayMonName[$MonCont],565, $Linha , 90, 17)
            GUICtrlCreateLabel($ArrayMonSerial[$MonCont], 660, $Linha, 95, 17)

            $Linha =  $Linha + 30

           ; If IsArray(.UserFriendlyName) Then ConsoleWrite("UserFriendlyName  = " & convert_int_array_to_string(.UserFriendlyName) & @CRLF)
           ; If IsArray(.ManufacturerName) Then ConsoleWrite("ManufacturerName  = " & convert_int_array_to_string(.ManufacturerName) & @CRLF)
           ; If IsArray(.SerialNumberId)   Then ConsoleWrite("SerialNumberId    = " & convert_int_array_to_string(.SerialNumberId)   & @CRLF)
           ; If IsArray(.ProductCodeID)    Then ConsoleWrite("ProductCodeID     = " & convert_int_array_to_string(.ProductCodeID)    & @CRLF)

        EndWith
    Next

EndFunc

Func convert_int_array_to_string($aUInts)
    Local $s  = ""

    For $i = 0 To UBound($aUInts) - 1
        If $aUInts[$i] = 0 Then ExitLoop
        $s &= Chr($aUInts[$i])
    Next

    Return $s
EndFunc

Func com_error_handler($oComError)
    ConsoleWrite(@CRLF)
    ConsoleWrite(StringFormat("Script Line  = %s", $oComError.ScriptLine) & @CRLF)
    ConsoleWrite(StringFormat("Win Err Desc = %s", StringStripWS($oComError.WinDescription, $STR_STRIPTRAILING)) & @CRLF)
    ConsoleWrite(StringFormat("Error Number = %i (0x%x)", $oComError.Number, $oComError.Number) & @CRLF)
    ConsoleWrite(StringFormat("Error Desc   = %s", $oComError.Description) & @CRLF)
    Exit
EndFunc




If $MonCont < 1 Then

        $MonName01 =  "Monitor Not Found"
        $MonSerial01 = "Monitor Not Found"
        $MonName02 =  "Monitor Not Found"
        $MonSerial02 = "Monitor Not Found"
EndIf


If $MonCont > 0 Then

        $MonName01 =  $ArrayMonName[1]
        $MonSerial01 = $ArrayMonSerial[1]
        $MonitorAsset00 = GUICtrlCreateInput("N/A", 770, 340, 65, 17, BitOR($GUI_SS_DEFAULT_INPUT,$ES_UPPERCASE))

EndIf

If $MonCont > 1 Then

        $MonName02 =  $ArrayMonName[2]
        $MonSerial02 = $ArrayMonSerial[2]
        $MonitorAsset01 = GUICtrlCreateInput("N/A", 770, 370, 65, 17, BitOR($GUI_SS_DEFAULT_INPUT,$ES_UPPERCASE))
EndIf


If $MonCont > 3 Then

        $MonName03 =  $ArrayMonName[3]
        $MonSerial03 = $ArrayMonSerial[3]
EndIf

 

Link to comment
Share on other sites

  • Solution
; generated with https://www.autoitscript.com/forum/files/file/338-scriptomatic-warraysupport/

#include "Array.au3"

Opt("MustDeclareVars",1)
Opt("TrayIconDebug",1)
Opt("TrayAutoPause",0)
Global $_nice_COMerrorArray, $_nice_COMerrorObj
_nice_COMerrorHandler(1) ; COM error handler.

ToolTip("mouse over the trayicon for debug info.", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)

Global $a = _WMI_WmiMonitorID()
If @error Then
    Switch @error
        Case 1
            MsgBox(262144, "WMI ScriptOMatic error", "No WMI Objects Found for class: ""WmiMonitorID""" )
        Case 2
            MsgBox(262144, "WMI ScriptOMatic error", "ObjGet failed")
        Case 3
            MsgBox(262144, "WMI ScriptOMatic error", "ObjCreate failed")
        Case 4
            MsgBox(262144, "WMI ScriptOMatic error", "ConnectServer failed")
        Case Else
            MsgBox(262144, "WMI ScriptOMatic error", "unknown error")
    EndSwitch
Else
    Switch @extended
        Case 0
            ToolTip("Done.", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
        Case 1
            ToolTip("Nothing, you've got nothing."&@CR&@CR&"Adjust the query", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",2,4)
        Case 2
            ToolTip("More than you can chew."&@CR&@CR&"Adjust the ""$iLimit"" or the query", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",2,4)
    EndSwitch
    If $_nice_COMerrorArray[0][0] Then _nice_COMerrorHandler(2)
    _ArrayDisplay( $a ,"ScriptOMatic - ""WmiMonitorID""")
EndIf


Func _WMI_WmiMonitorID( $iLimit = 1000 , $sComputer = "." , $sUser = "" , $sPassword = "" )

    ; Generated by AutoIt ScriptOMatic

    ; Description: Monitor ID


    ; Class Methods:
    ;
    ; ( this Class did not list methods )


    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $colItems = ""
    Local $sReturn=""
    Local $iExt = 0
    If 1 > Int( $iLimit ) Then $iLimit = 1
    Local $aReturn[1000][ Int( $iLimit ) + 1]
    $aReturn[0][0] = 0
    $aReturn[0][1] = 0
    Local $aErr[1][2] = [[0, 0]]

    If $sComputer & $sUser & $sPassword = "." Then
        ToolTip("...ObjGet", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
        Local $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\root\WMI")
        If Not IsObj($oWMIService) Then Return SetError(2, 99, $aErr)
    Else
        ToolTip("...ObjCreate", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
        Local $wmiLocator = ObjCreate("WbemScripting.SWbemLocator")
        If Not IsObj($wmiLocator) Then Return SetError(3, 99, $aErr)
        ToolTip("...ConnectServer", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
        Local $oWMIService = $wmiLocator.ConnectServer($sComputer, "\root\WMI", $sUser, $sPassword)
        If Not IsObj($oWMIService) Then Return SetError(4, 99, $aErr)
    EndIf

    ToolTip("...ExecQuery", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
    Local $colItems = $oWMIService.ExecQuery("SELECT * FROM WmiMonitorID", _
                         "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    ToolTip("...ExecQuery executed."& @CR &"...waiting for data.", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
    If IsObj($colItems) Then

        ; Class Properties: Names:
        ;

        $aReturn[ 1 ][ 0 ] = "Active"
        $aReturn[ 2 ][ 0 ] = "InstanceName"
        $aReturn[ 3 ][ 0 ] = "ManufacturerName"
        $aReturn[ 4 ][ 0 ] = "ProductCodeID"
        $aReturn[ 5 ][ 0 ] = "SerialNumberID"
        $aReturn[ 6 ][ 0 ] = "UserFriendlyName"
        $aReturn[ 7 ][ 0 ] = "UserFriendlyNameLength"
        $aReturn[ 8 ][ 0 ] = "WeekOfManufacture"
        $aReturn[ 9 ][ 0 ] = "YearOfManufacture"
        $aReturn[0][0] = 9
        For $objItem In $colItems
            $aReturn[0][1] = $aReturn[0][1] + 1
            If Not Mod( $aReturn[0][1] , 100 ) Then ToolTip("...adding "& $aReturn[0][1] &" of "&$iLimit &" ??", @DesktopWidth - 30,@DesktopHeight - 130, "ScriptOMatic - WmiMonitorID",1,4)
            If $aReturn[0][1] > $iLimit Then
                $iExt = 2
                ExitLoop
            EndIf

            ; Class Properties: Values:
            ;

            $aReturn[ 1 ][ $aReturn[0][1] ] = $objItem.Active
            $aReturn[ 2 ][ $aReturn[0][1] ] = $objItem.InstanceName
            $aReturn[ 3 ][ $aReturn[0][1] ] = Array_Join( $objItem.ManufacturerName )
            $aReturn[ 4 ][ $aReturn[0][1] ] = Array_Join( $objItem.ProductCodeID )
            $aReturn[ 5 ][ $aReturn[0][1] ] = Array_Join( $objItem.SerialNumberID )
            $aReturn[ 6 ][ $aReturn[0][1] ] = Array_Join( $objItem.UserFriendlyName )
            $aReturn[ 7 ][ $aReturn[0][1] ] = $objItem.UserFriendlyNameLength
            $aReturn[ 8 ][ $aReturn[0][1] ] = $objItem.WeekOfManufacture
            $aReturn[ 9 ][ $aReturn[0][1] ] = $objItem.YearOfManufacture
        Next
        If $aReturn[0][1] = 0  Then $iExt = 1
        ReDim $aReturn[$aReturn[0][0] + 1][$aReturn[0][1] + 1]
        Return SetError( 0 , $iExt , $aReturn )
    Else
        Return SetError(1 ,99 ,$aErr)
    EndIf
EndFunc


Func Array_Join($aArray, $sSeparator = " , ")
    Local $n, $sOut = ""
    If IsObj($aArray) Then
        For $value In $aArray
            $sOut &= $value & $sSeparator
        Next
        Return StringTrimRight($sOut, StringLen($sSeparator))
    Else
        For $n = 0 To UBound($aArray) - 1
            If $aArray[$n] = 0 Then ContinueLoop
            $sOut &= Chr($aArray[$n]); & $sSeparator
        Next
        Return $sOut
        Return StringTrimRight($sOut, StringLen($sSeparator))
    EndIf
EndFunc   ;==>Array_Join

#Region    # ValueToDescription Functions #



#EndRegion # ValueToDescription Functions #

Func _nice_COMerrorHandler($i = 0)
    If $i == 1 Then
        Dim $_nice_COMerrorArray[301][9]
        $_nice_COMerrorArray[0][0] = 0
        $_nice_COMerrorObj = ObjEvent("AutoIt.Error", "_nice_COMerrorHandler")
        Return
    EndIf
    If $i == 2 Then
        If Not @Compiled Then
            Local $n = 0, $c = 0, $s = FileRead(@ScriptFullPath)
            Local $a = StringSplit($s, @CRLF, 1)
            For $n = 1 To $_nice_COMerrorArray[0][0]
                If Int($_nice_COMerrorArray[$n][7]) > $a[0] Then ContinueLoop
                $_nice_COMerrorArray[$n][8] = StringStripWS($a[Int($_nice_COMerrorArray[$n][7])], 3)
            Next
            If StringInStr($s, "; only for the ANSI compiled version" & @CRLF) Then
                For $n = 1 To $a[0]
                    If StringInStr($a[$n], "; only for the ANSI compiled version") Then $c += 1
                    If $n > 50 Then ExitLoop
                Next
                For $n = 1 To $_nice_COMerrorArray[0][0]
                    $_nice_COMerrorArray[$n][7] = $_nice_COMerrorArray[$n][7] - $c
                Next
            EndIf
        EndIf
        $_nice_COMerrorArray[1][0] = ""
        ReDim $_nice_COMerrorArray[$_nice_COMerrorArray[0][0] + 1][9]
        _ArrayDisplay($_nice_COMerrorArray, "ScriptOMatic - COM Errors intercepted ( the script will continue after this screen )")
        Return
    EndIf
    If $_nice_COMerrorArray[0][0] = 300 Then
        $_nice_COMerrorArray[0][8] = "ScriptLine: only first 300 errors shown !!!"
        Return
    EndIf
    If StringInStr($_nice_COMerrorArray[1][0], "|" & $_nice_COMerrorObj.scriptline & "|") Then Return
    $i = $_nice_COMerrorArray[0][0] + 1
    $_nice_COMerrorArray[0][0] = $i
    $_nice_COMerrorArray[$i][1] = "0x" & Hex($_nice_COMerrorObj, 8)
    $_nice_COMerrorArray[$i][2] = $_nice_COMerrorObj.windescription
    $_nice_COMerrorArray[$i][3] = $_nice_COMerrorObj.source
    $_nice_COMerrorArray[$i][4] = $_nice_COMerrorObj.helpfile
    $_nice_COMerrorArray[$i][5] = $_nice_COMerrorObj.helpcontext
    $_nice_COMerrorArray[$i][6] = $_nice_COMerrorObj.lastdllerror
    $_nice_COMerrorArray[$i][7] = $_nice_COMerrorObj.scriptline
    $_nice_COMerrorArray[1][0] = $_nice_COMerrorArray[1][0] & "|" & $_nice_COMerrorObj.scriptline & "|"
    If $i == 1 Then
        $_nice_COMerrorArray[0][1] = "ErrorNumber:"
        $_nice_COMerrorArray[0][2] = "WinDescription:"
        $_nice_COMerrorArray[0][3] = "Source:"
        $_nice_COMerrorArray[0][4] = "HelpFile:"
        $_nice_COMerrorArray[0][5] = "HelpContext:"
        $_nice_COMerrorArray[0][6] = "LastDLLerror:"
        $_nice_COMerrorArray[0][7] = "ScriptLineNumber:"
        $_nice_COMerrorArray[0][8] = "ScriptLine:"
    EndIf
EndFunc   ;==>_nice_COMerrorHandler

...some thing like this. That runs. To pay it forward ;) 

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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...