Hello,
Workin on some more array functions and this one is to copy all items in a listbox and put them into an array. The problem is that for some reason, when the items are retrieved from the array they are all the same item. I've added a function to add the retrieved items to another listbox for output.

Here's the function:
;Copy all items from a listbox to a new array.
MM_CopyListboxToArray proc objHandle:DWORD,intOption:DWORD
LOCAL objArray:DWORD
LOCAL objLBHandle:DWORD

invoke Clear_Registers

;Set Register Defaults.
mov ebx,objHandle ;Put listbox handle in ebx.
mov objLBHandle,ebx ;Copy listbox handle to objHandle.
mov esi,0 ;Max count.
mov edi,0 ;Set counter default, must be 1 because to match the listbox start.
;mov intArrayItemCount,0 ;Set/clear default variable value.

push 0
push 0
push LB_GETCOUNT
push objHandle
call SendMessage ;Get the listbox item count.
mov edx,eax ;Copy count to edx.
mov esi,eax ;Copy count to esi also to set stop point.

invoke MM_Create_Array,edx,1 ;Create Array using listbox item count.
mov objArray,eax ;Put newly created array handle in variable.
mov ebx,objLBHandle ;Put listbox handle in ebx.
mov esi,intItemCount ;Set Max Count.
mov edi,0 ;Set edi to 0.

@@:
push offset strListboxToArrayText ;256 Byte Buffer.
push edi
push LB_GETTEXT
push objHandle
call SendMessage ;Retrieve item text and place into buffer.

mov edx,objArray ;Put array handle in edx.
mov , offset strListboxToArrayText ;Put string in Array.
mov edx, ;Get ASCII string item from array.

push edx
push 0
push LB_ADDSTRING
push hListbox2
call SendMessage ;Add item to another listbox to view output.

inc edi
cmp edi,esi
jne @B

invoke Clear_Registers

mov eax,objArray ;Return newly created array handle.
ret
MM_CopyListboxToArray endp
Posted on 2003-06-13 16:57:29 by Knight Chat X
Why all items in one. I don't suppose needs another list. If you working with const lengths, may switch to other items easily. But if string values, then you may add a null terminator per item then you have to attention to this ending NULL char as item end. I hope could understand right.

Regards
Posted on 2003-06-13 17:09:41 by cakmak
It's a string array.

I'll give an example of problem:

Listbox 1 looks like this:
Test 1
Test 2
Test 3

Now, after the items are put into the array, and copied over to another listbox...

Listbox 2 looks like this:
Test 3
Test 3
Test 3

It should look just as Listbox 1...

At first I thought it was a problem with the index, but that was ruled out as it is incrementing properly to reach the end and return. Then I thought that maybe the item's aren't being place into the array properly, but couldn't find that the items were all the same, they matched what was in the first listbox. Lastly, thought maybe the issue was with the string buffer, unfortunately, throughout the loop the buffer updates for each item just fine. So the question is, why is this not working???
Posted on 2003-06-13 17:29:03 by Knight Chat X
I suppose nobody answer such info. Could you post the code part which sends-gets the array. You may test the items while sending and getting as messagebox string, better use OllyDebug, and see where the other items go. I guess you may over-writing them.
Posted on 2003-06-13 17:48:44 by cakmak
mov edx, ;Get ASCII string item from array.

I included all the code in the first post for this topic, it's pretty commented.

Never heard of OllyDebug...
Posted on 2003-06-13 18:15:12 by Knight Chat X


invoke MM_Create_Array,edx,1 ;Create Array using listbox item count.
mov objArray,eax ;Put newly created array handle in variable.
mov ebx,objLBHandle ;Put listbox handle in ebx.
mov esi,intItemCount ;Set Max Count.
mov edi,0 ;Set edi to 0.

@@:
push offset strListboxToArrayText ;256 Byte Buffer.
push edi
push LB_GETTEXT
push objHandle
call SendMessage ;Retrieve item text and place into buffer.

mov edx,objArray ;Put array handle in edx.
mov , offset strListboxToArrayText ;Put string in Array.
mov edx, ;Get ASCII string item from array.

push edx
push 0
push LB_ADDSTRING
push hListbox2
call SendMessage ;Add item to another listbox to view output.

inc edi
cmp edi,esi
jne @B
It looks like LB_ADDSTRING does not create a copy of the string you're adding, at least not before you force a redraw. This would explain all entries being the same because you are using the same pointer in all your slots -- namely, offset strListboxToArrayText.
Posted on 2003-06-13 18:17:35 by tenkey
Never heard of OllyDebug...


http://home.t-online.de/home/OllyDbg/
Posted on 2003-06-13 19:13:17 by Green Joe
I wondered that myself, but as edi is incremented and the loop cycles the number of items in the listbox, each time the buffer is used, the next time the buffer's contents are updated to the next item. Also you might notice it somehow successfully cycles through to the very last item, if it didn't, it would not add the last item, being:

Test 3

Wouldn't think I'd need to change the buffer as the buffer can be freely updated.
Posted on 2003-06-13 19:14:58 by Knight Chat X