I have to do a strange declaration

Hello to all,
I created this code which shows a popup window when any date is matched in a listview control:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
LV_ITEM		lvi;
SYSTEMTIME st;

int count=SendMessage(hWndListView, LVM_GETITEMCOUNT, 0, 0);

memset(&lvi, 0, sizeof(lvi));
for (int i=0; i<count; i++){
	
TCHAR subitem[100];
TCHAR szBuf[256];



long lLen = 128;
lvi.iItem =i;
lvi.iSubItem=2;
SendMessage(hWndListView, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)&lvi);
lvi.pszText = subitem;

lvi.cchTextMax = lLen;
GetLocalTime(&st);
GetDateFormat(LOCALE_SYSTEM_DEFAULT,DATE_SHORTDATE,&st,NULL,szBuf, sizeof(szBuf));



if (strcmp(lvi.pszText,szBuf)==0){
	



TCHAR item[100];

long lLen = 128;
lvi.iItem =i;
lvi.iSubItem=0;
SendMessage(hWndListView, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)&lvi);
lvi.pszText = subitem;

lvi.cchTextMax = lLen;
  
  SetWindowText(hwndST,lvi.pszText);
	ShowWindow(alarm,SW_SHOW);
}

}
return(0);

The code works fine, but I don't realize why I have to insert the declaration: TCHAR item[100]; Without which this code doesn't work.
This is to learn more and more the API win32 language about which I am a newbie.
Thanks to all.

Last edited on
You need to set lvi.cchTextMax before you call SendMessage(..., LVM_GETITEMTEXT otherwise it won't know how much text to copy over.

In your case you need to set lvi.cchTextMax = sizeof(subitem)/sizeof(TCHAR);.

I suspect you're getting text back that's longer than the 100 bytes available in subitem. By declaring another 100 byte buffer, you're providing space to overwrite into. Without it, you're overwritting something important.
Last edited on
Hello kbw,
now it's more order in my code and it works. I'm using now only the "subitem" buffer. Thank you! You intend to tell me that i can increase the buffer size?
Last edited on
You don't need a larger buffer as you're testing for a date, so you wouldn't match on a long string anyway. But you must always set the buffer size whether the buffer is small or large.
Last edited on
OK kbw,
Goodbye
Topic archived. No new replies allowed.