Sign in to follow this  
Followers 0
Maikey81

Make label for each item in Array from WMI

7 posts in this topic

I'm trying to create a bunch of labels with in each label the drive letter and label from each disk.

I'm trying it with the code beneath. My GUI is empty. No Labels are created. Does somebody see what I'm doing wrong?

#include <GUIConstantsEx.au3>
#include <Process.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>

$oMyError = ObjEvent("AutoIt.Error", "oMyError") ; Install a custom error handler

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")


    Dim $DriveLabel, $sDriveLabels, $i, $l, $n
    Dim $aDriveLabels[26][2]
    Dim $Labels[26]
    $colItems = $objWMIService.ExecQuery("SELECT Name, VolumeName FROM Win32_LogicalDisk", "WQL", 0x30)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $aDriveLabels[$i][0] = $objItem.Name
            $aDriveLabels[$i][1] = $objItem.VolumeName
            $i += 1
            $l += 25
            $Labels[$objItem] = GUICtrlCreateLabel($aDriveLabels[$i][0] & " -> " & $aDriveLabels[$i][1], 50, $l)
        Next
    EndIf
    ReDim $aDriveLabels[$i][2]
    GUICreate("TEST", 400, 300, -1, -1, $WS_CAPTION)
    GUICtrlSetData($Labels[$objItem])
    GUISetState(@SW_SHOW)
    
While 1
$msg = GUIGetMsg()  
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
WEnd

Share this post


Link to post
Share on other sites



Hi Maikey,

1. You need to shift the GuiCreate() Statement before you create your labels

2. If you count up $i before you fill your labels, you're always selecting the next array, which is not filled and therefor empty; you can use $i -1 for example.

Aaaand: Welcome to the AutoIT forum! :-)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Hi Maikey,

1. You need to shift the GuiCreate() Statement before you create your labels

2. If you count up $i before you fill your labels, you're always selecting the next array, which is not filled and therefor empty; you can use $i -1 for example.

Aaaand: Welcome to the AutoIT forum! :-)

Hi Hannes,

Thank you for your reply and thanks for welcoming me :mellow:.

I tried to use your comments and made up the following.

#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <Process.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>

$oMyError = ObjEvent("AutoIt.Error", "oMyError") ; Install a custom error handler

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

;$aMyDriveLabels = GetWMI($ip)
;_ArrayDisplay($aMyDriveLabels)
;MsgBox(0, "Test _ComputerGetBIOS", "Product: " & $aMyDriveLabels[0][1])


    Dim $DriveLabel, $sDriveLabels, $i, $l, $n
    Dim $aDriveLabels[26][2]
    Dim $Labels[$objItem], $objItem

    GUICreate("TEST", 400, 300, -1, -1, $WS_CAPTION)
    GUICtrlSetData($Labels[$objItem])
    GUISetState(@SW_SHOW)


    $colItems = $objWMIService.ExecQuery("SELECT Name, VolumeName FROM Win32_LogicalDisk", "WQL", 0x30)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $aDriveLabels[$i][0] = $objItem.Name
            $aDriveLabels[$i][1] = $objItem.VolumeName
            $Labels[$objItem] = GUICtrlCreateLabel($aDriveLabels[$i][0] & " -> " & $aDriveLabels[$i][1], 50, $l)
            $i += 1
            $l += 25
        Next
    EndIf
    ReDim $aDriveLabels[$i][2]
    
While 1
$msg = GUIGetMsg()  
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
WEnd

It now gives an error:

drives.au3 (26) : ==> Variable used without being declared.:
Dim $Labels[$objItem], $objItem
Dim $Labels[^ ERROR

Share this post


Link to post
Share on other sites

Hi Maikey,

now that one is obvious. Trying to declare an array called $Labels with a defined length of $objItem will fail because you declare $objItems just after that. BUT in this case it would be better to declare it with a fixed width of 26 (or 27 if you want to use the first element as a counting space) $Labels[26].

:mellow:


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Hi Maikey,

now that one is obvious. Trying to declare an array called $Labels with a defined length of $objItem will fail because you declare $objItems just after that. BUT in this case it would be better to declare it with a fixed width of 26 (or 27 if you want to use the first element as a counting space) $Labels[26].

:)

Hi Hannes,

I realized it myself. But even when I used $Labels[26], it gave an error. But I managed to get it to work. And I must say, a lot less script and easier to understand :mellow:.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $ip = "localhost"
$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GUICreate("TEST", 400, 300, -1, -1, $WS_CAPTION)

$colItems = $objWMIService.ExecQuery("SELECT Name, VolumeName FROM Win32_LogicalDisk", "WQL", 0x30)
If IsObj($colItems) Then
    $label = ""
    For $objItem In $colItems
        $Label = $Label & $objItem.Name & " -> " & $objItem.VolumeName & @CRLF
    Next
EndIf

GUICtrlCreateLabel($Label, 10, 20)
GUISetState(@SW_SHOW)

While 1
$msg = GUIGetMsg()  
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
WEnd

Now it won't create a label for each item, but one label with all items.

Share this post


Link to post
Share on other sites

Just another hint:

If you use the built-in function DrivegetDrive("all") you'll have all the drive letters in an array.

And with DriveGetLabel("Driveletter") you can get the current label of the drive.

Maybe you can handle this easier than a 2-dim array.

:mellow:


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Just another hint:

If you use the built-in function DrivegetDrive("all") you'll have all the drive letters in an array.

And with DriveGetLabel("Driveletter") you can get the current label of the drive.

Maybe you can handle this easier than a 2-dim array.

:mellow:

That's something to remember. I shall take a look at it.

One other question. How can I make a button disabled, until a value is selected in 2 comboboxes?

I have this and it works, but when moving the mouse de button flickers:

While 1
$msg = GUIGetMsg()  

If GUICtrlRead($sList) = "" Then
    GUICtrlSetState($doImage, $GUI_DISABLE)
ElseIf  GUICtrlRead($dList) = "" Then
    GUICtrlSetState($doImage, $GUI_DISABLE)
Else
    GUICtrlSetState($doImage, $GUI_ENABLE)
EndIf

Select
    Case $msg = $GUI_EVENT_CLOSE
        GUIDelete()
        ExitLoop
    Case $msg = $doImage
        Image()
Case $msg = $restartbtn
        GUIDelete()
        ExitLoop
EndSelect
WEnd

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