Re: get text from listbox

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thorsten,

Thanks for that thorough reply.
Will see if I can follow your suggestions and post some output back. I figured out a way
now to always get the right hwnd of this listbox programmatically, so that will help.


RBS


"Thorsten Albers" <albersRE@xxxxxxxxxxxxxxxxxxx> wrote in message news:01c5f76a$b649a180$LocalHost@xxxxxxxxxxx
RB Smissaert <bartsmissaert@xxxxxxxxxxxxxxxx> schrieb im Beitrag
<#IjeWms9FHA.1224@xxxxxxxxxxxxxxxxxxxx>...
The garbled text is only about 5 characters.

The garbled text is not "only about 5 characters": It is 64 characters
because this is the amount of characters you have dimensioned the string
("listStr = Space$(64)"). If only 5 characters are getting printed, the 6th
character is a Null byte
Since your list box hasn't the listbox class style LBS_HASSTRINGS only the
first 4 of the 5 characters really do have a meaning (= item data of the
respective list item; ""If you create the list box with an owner-drawn
style but without the LBS_HASSTRINGS style, the buffer pointed to by the
lpszBuffer parameter will receive the 32-bit value associated with the item
(the item data)").
Instead of
r = SendMessageStr(lLBhwnd, LB_GETTEXT, i, listStr)
you should use
pData = SendMessage(lLBhwnd, LB_GETITEMDATA, i, 0)
and
Debug.Print Hex$(pData)


This is an example output for all the listbox items:
?BCà
h&Ex
У?x
¤?
°Dv
@.Cà
 ÿv
ø:?`
(hv
ðC`
`C
(C
ØÄrà
...

We know that these garbage strings must be the item data value of the
respective list item; therefore only the first 4 characters do matter
because the item data is a DWORD value.

?BC
h&E
У?
¤?
°Dv
@.C
 ÿv
ø:?
(hv
ðC
`C
(C
ØÄr
...

The first character is the low byte of the low word of the DWORD item data
value, the 4th character is the high byte of the high word. This high byte
seems to be the same with all list items, assuming that the box character
has the same character code with all items.
Assuming that the control which you have used to display the 'garbage' is
set to MS Sans Serif there are only some box characters: 7Fh to 81h, 8Dh to
90h, and 9Dh to 9Eh.
Such a character code very well could be the high byte of the high word of
a DWORD pointer. I would bet that the character code of the box character
was 7Fh and that these 'garbage' strings indeed are pointers à la
7Fxxxxxx
7Fxxxxxx
7Fxxxxxx
...
addressing memory in a virtual address space of 2 GB (memory allocation
always starts at the highest available address).


To prove that these are pointers get the item data of the list items as
suggested above and post the sequence of some of the hexadecimal values
here in this grooup. Together we should be able to work out if they indeed
are pointers and, if so, to what kind of data they are pointing.

Some hints:

- If the item data consists of relatively low numbers (0 ...) increased
from the one to the next always by the same offset, the presumably are
array index numbers. If so, forget about to get the strings.
- If the item data consists of relatively high numbers like
 7xxxxxxx
 6xxxxxxx
 5xxxxxxx
it is very likely that we have pointers to the list item data.
Check, if the offset from one value to the next is >always the same<. If
so, it is even more likely that we have pointers to the list item data.
At this point it is necessary to know the offset from one value to the
other:
a) If it is 4, the pointers are likely to be pointers to pointers.
b) If it is <> 4, the pointers are likely to be pointers to an array of
structures with each of the pointers pointing to the respective array item
structure.

a)
Item data (pData)
1 7xxxxxxx
2 7xxxxxxx (= 1 + 4)
3 7xxxxxxx (= 2 + 4)
4 7xxxxxxx (= 3 + 4)

b)
Item data (pData)
1 7xxxxxxx
2 7xxxxxxx (= 1 + n <> 4)
3 7xxxxxxx (= 2 + n <> 4)
4 7xxxxxxx (= 3 + n <> 4)

With a):
- Read the value to which the respective item data value seems to point
 pNew = 4 Bytes from memory starting at pData
- Try to identify the values of pNew the same way you have tried to
identify the values of pData; if the offset of these is <> 4 proceed as
shown for b)

With b):
If the offset from pData(n) to pData(n + 1) is always the same and <> 4,
the offset presumably gives the size of the structure. So, make a memory
dump of the data to which pData seems to point, lets say for the first 10
list items.
If all assumptions have been correct so far, i.e. if we really are dealing
with an array of structures, with these memory dumps it should be not too
difficult to work out the meaning of at least some of the structure fields.
Again you could try to classify structure fields as pointers by checking
their values (like we have done above) and the offsets from value to value.
If the one or the other field seems to hold a pointer, it presumably is a
string pointer; check it by reading some bytes from the memory to which the
assumed pointer points.


The listbox has 7 columns, 1 for an icon and 6 with strings.
The number of charaters in all these 6 strings together can come up to
about
300 characters.

Judging from this it is likely that - the item data of each one of the list items is a pointer to a structure, and that - each structure has at least a) either one string pointer pointing to a string that holds all 6 displayed strings separated by some kind of a separator (Null byte, tabulator, or the like) or 6 string pointers pointing to the respective displayed string, and b) an icon handle

If you want to try this approach, remember that the memory is owned by
another application, i.e. that you can't use CopyMemory() but have to the
way of reading the memory Kevin has shown you.

--
----------------------------------------------------------------------
THORSTEN ALBERS         Universität Freiburg
                                               albers@
                                                      uni-freiburg.de
----------------------------------------------------------------------


.



Relevant Pages

  • Re: Is this string input function safe?
    ... return a pointer to mallocated memory holding one input string, ... complains about use of deallocated pointers, ... mallocating an appropriate amount of memory. ... the contents of the buffer are indeterminate (for different ...
    (comp.lang.c)
  • Re: get text from listbox
    ... The first character is the low byte of the low word of the DWORD item data ... addressing memory in a virtual address space of 2 GB (memory allocation ... To prove that these are pointers get the item data of the list items as ... string pointer; check it by reading some bytes from the memory to which the ...
    (microsoft.public.vb.winapi)
  • Re: list optimization
    ... > As far as my first goal is to save as much memory as possible i prefer ... you may simple use a compromise of using an array and pointers between ... struct address *pNext ... string that holds the name, different names but same address 1 or 2 ...
    (comp.lang.c)
  • Re: std::map question
    ... If you are allocating memory dyanmically and it is possible for exceptions ... is an object that is responsible for managing the memory. ... Also, reference counted pointers can provide other benefits, ... especially when you consider that the string is probably managing ...
    (comp.lang.cpp)
  • Re: fscanf issues, please help.
    ... those pointers point to strings of whatever length. ... An array of pointers is an array of pointer, ... they pont to some random places in memory. ... be an integer, the next one a string, followed by a hash etc., ...
    (comp.lang.c)