Subclassing a listbox and the speed of the postal service

This is a discussion on Subclassing a listbox and the speed of the postal service within the Windows Programming forums, part of the Platform Specific Boards category; Hi,
I'm currently subclassing a listbox, with a view to turning it into a property viewer as featured in the ...

Subclassing a listbox and the speed of the postal service

Hi,

I'm currently subclassing a listbox, with a view to turning it into a property viewer as featured in the Visual Basic IDE. The thing is, I've added a new message to the WindowProc which adds a property to the window and at this early stage that effectively means some basic formatting and using LB_ADDSTRING twice. It looks something like this:-

The property's name is immediately added to the listbox (Because this window is a subclassed listbox, I'm sending it to itself). After the data is formatted into the szTemp string it's also added. However, while the first LB_ADDSTRING message seems to work properly (The name is visible in the listbox), the second one, while a valid string is passed to it, doesn't (Lovely big row of "í" ).

What is strange is that ret, which is supposed to give the index of the new string, doesn't appear to change between the two commands. Replacing "szTemp" with a string constant (such as "0") works, so I think the memory the string's occupying is being freed before the call is finished, some sort of reentrant problem. Help?

But all that happens in the switch statement is that the data type of the property is determined and the appropriate format specifiers in sprintf used. Like I said, a valid string is produced from it, and used in the LB_ADDSTRING message, but when the string is displayed by the listbox it's garbled.

I'm thinking a "static char *szTemp[32];" might work, but there might be other implications with static variables, so I dunno

Don't have a clue based on what you've posted, but here's some things to think about:

Have you tried filling the string variable with an explicit, dummy value and see what happens when you add that? (I note you have already tried "0" as a literal.)

What is your subclass listbox procedure doing with the LB_ADDSTRING?

Are you handling the listbox's memory allocation or are you letting the system handle it? (if the system is dealing with it the string should be copied).

Have you tested the mechanism against a) a normal listbox and b) a simplified subclassed listbox?

Try outputting the formatted string to a file or MessageBox? Although there are no guarantees that it may identify a particular problem, you should be aware that there are sometimes differences between debug and 'release' builds that can manifest themselves in different ways.