'Get the state of the items in a CheckListBox using SendMessage API

I'm using a software to manipulate another one, like a software tester environment, with the objective of getting some values and executing some tasks. For this I'm using SendMessage API and it's working very well in almost all Windows controls, except in the CheckListBox. The "slave" software was created in Delphi, so, I'm not sure if that CheckListBox is a standard Windows control, anyway, this is the documentation from MS:
https://docs.microsoft.com/en-us/windows/win32/controls/list-boxes
In this control I can get the quantity of items, the texts, which one is selected, and a lot of other info, but I can not get to know if the items are checked or not. I tried to use some tools like "Accessibility Insights for Windows" and AutoIt, but no results.
Any idea on how to get the state of the check boxes inside a Delphi CheckListBox?
Thanks in advance.



Solution 1:[1]

As posted by @CherryDT, that is the way to access the state of the CheckListBox. I used AutoIt to test the tip because at this moment this is the fastest way I have to test it.
The code:

;------------------------------------------------------------------------------
;   Retuns the state of the indicated item in the CheckListBox control.
;   Parameter:
;       $iPID: process ID (PID)
;       $hWnd: the handle of the CheckListBox control
;       $iIndex: index of the item in the list (0 based)
;------------------------------------------------------------------------------
Func CtrlListBox_GetState($iPID, $hWnd, $iIndex)
   Local $hProc                             ; Handle of the process.
   Local $pItem                             ; Pointer to the item.
   Local $pData = DllStructCreate("byte")   ; Data structure.
   Local $iQty                              ; Size of data read.
   Local Const $LB_STATE_SHIFT = 8          ; State position in the memory.

   $hProc = _WinAPI_OpenProcess(0x1F0FFF, False, $iPID)     ; 0x1F0FFF = PROCESS_ALL_ACCESS
   $pItem = _GUICtrlListBox_GetItemData($hWnd, $iIndex) + $LB_STATE_SHIFT
   _WinAPI_ReadProcessMemory($hProc, $pItem, DllStructGetPtr($pData), DllStructGetSize($pData), $iQty)

   Return DllStructGetData($pData, 1)
EndFunc

;------------------------------------------------------------------------------
;   Retuns the state of the indicated item in the CheckListBox control.
;   Parameter:
;       $iPID: process ID (PID)
;       $hWnd: the handle of the CheckListBox control
;       $iIndex: index of the item in the list (0 based)
;       $bState: state deseired (true or false)
;------------------------------------------------------------------------------
Func CtrlListBox_SetState($iPID, $hWnd, $iIndex, $bState)
   Local $hProc                             ; Handle of the process.
   Local $pItem                             ; Pointer to the item.
   Local $pData = DllStructCreate("byte")   ; Data structure.
   Local $iQty                              ; Size of data read.
   Local Const $LB_STATE_SHIFT = 8          ; State position in the memory.

   if($bState <> 0) Then
      DllStructSetData($pData, 1, True)
   Else
      DllStructSetData($pData, 1, False)
   EndIf

   $hProc = _WinAPI_OpenProcess(0x1F0FFF, False, $iPID)     ; 0x1F0FFF = PROCESS_ALL_ACCESS
   $pItem = _GUICtrlListBox_GetItemData($hWnd, $iIndex) + $LB_STATE_SHIFT
   _WinAPI_WriteProcessMemory($hProc, $pItem, DllStructGetPtr($pData), DllStructGetSize($pData), $iQty)

   Return DllStructGetData($pData, 1)
EndFunc

Solution 2:[2]

I was facing the similar problem while trying to get the status of checkbox inside the CheckedListBox of UI developed in VB.Net. I wanted to get the status(check/Uncheck) so that I can execute required event from my test automation framework developer in Python + Winium. This was especially required to not accidently uncheck the already checked item.

During my research, I got to know that it is not easily possible using Winium so I added a button on the UI for resetting all the checkboxes before checking the required ones.

Hope it might help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gustavo E. Hennemann
Solution 2 Avish Gattani