Hey fellas, call me stupid if you must but I just can't get this to work the way I want it:


LOCAL Count:BYTE

invoke SendMessage,IDC_LISTBOX2,LB_GETCOUNT,0,0
mov Count,al
dec Count
Loop1:
invoke MessageBox,hWin,0,0,0
dec Count
cmp Count,0
jne Loop1


Here's what's supposed to happen: The program should get the number of items in the listbox, and do the stuff in the loop procedure as many times as there are items. Unfortunately, instead of the message box popping up twice if there were two things in the listbox, it pops up A LOT of times (I have a feeling it's 255). I am just completely stumped on this. Please help!
Posted on 2004-09-08 18:41:04 by yo|dude|mon
This code looks wrong - you're using a dialog, aren't you? (Judging by IDC_LISTBOX2). SendMessage wants a window handle, not a control ID. What you want is SendDlgItemMessage - it's the same as your current code, except you need to add the dialog hwnd as the first parameter.

255 popups sounds about right - wouldn't surprise me if at least some windows versions return "-1" if the window is invalid (there's a good chance it will be with a dialog control constant), this is 0xFFFFFFFF, and your "count" will thus be 0xFF or 255.
Posted on 2004-09-08 19:24:44 by f0dder
There must be an problem with your listbox and the SendMessage are returning the error message LB_ERR in EAX, whose value is -1, or 255, in byte size.
Posted on 2004-09-08 19:29:56 by Marginais
It looks like the loop is badly formed. Just look at it and imagine what would happen if the amount returned is 0 or 1. You'll see that it will not exit the loop on time. In all other cases it will loop once less than necessary because of the extra "dec" you inserted. The only way you can handle the case of zero items is to put some kind of jump at the beginning to skip over the loop if there are zero items.



invoke SendMessage,IDC_LISTBOX2,LB_GETCOUNT,0,0
or al,al
jz EndLoop1
mov Count,al
Loop1:
invoke MessageBox,hWin,0,0,0
dec Count
jnz Loop1
EndLoop1:


Don't forget that "dec" sets the zero flag if the result is zero, so you don't have to use "cmp".
Posted on 2004-09-08 19:41:38 by yessopotamus
Assuming that IDC_LISTBOX2 is part of a dialog box,



invoke SendDlgItemMessage,[dlghwnd],IDC_LISTBOX2,LB_GETCOUNT,0,0
or al,al
jz EndLoop1
mov Count,al
Loop1:
invoke MessageBox,hWin,0,0,0
dec Count
jnz Loop1
EndLoop1:
Posted on 2004-09-08 22:03:12 by roticv
Or, if you have one to spare, you could put the counter in a register (except eax,ecx,edx), to avoid use of a memory variable.



push ebx

invoke SendDlgItemMessage,[dlghwnd],IDC_LISTBOX2, LB_GETCOUNT,0,0
test eax, eax
jz EndLoop1
mov ebx, eax

Loop1:
invoke MessageBox,hWin,0,0,0
dec ebx
jnz Loop1
EndLoop1:

pop ebx
Posted on 2004-09-09 02:26:23 by f0dder
Not directly related to the loop itself but istead of

invoke MessageBox,hWin,0,0,0

it is much safer to use

invoke MessageBox,hWin,Offset szNULL,Offset szNULL,0

where


.DATA
szNULL DB 0

Antonis
Posted on 2004-09-09 02:43:59 by akyprian
I'd rather write:
mov eax,offset $+6
invoke MessageBox,hWin,eax,eax,0
Posted on 2004-09-09 11:50:09 by Sephiroth3
It is amazing how you write such a code. Simply Brilliant
Posted on 2004-09-09 11:52:21 by roticv
Don't worry about the whole SendMessage thing (I had a feeling that it would be confusing). This should explain it:


.data?
IDC_LISTBOX2 dd ?

..............

.if uMsg == WM_INITDIALOG
invoke GetDlgItem,hWin,701
mov IDC_LISTBOX2, eax
.............

I just felt that this way would be easier.

Sorry about the confusion. I'll try some of your examples.
Posted on 2004-09-09 15:24:52 by yo|dude|mon
Yeah, it works now. Thanks a LOT!!
Posted on 2004-09-09 15:27:22 by yo|dude|mon