Link to home
Start Free TrialLog in
Avatar of MichaelMaromm
MichaelMaromm

asked on

Question about List View Control (C++)

Hi,

I have created a dialog box with a list view control
in one thread (thread 1).
and I want to send a messages to the list view from
other thread.
I also passed the HWND of the list view control to the other thread.

My problems are :
1. When I try to send the LVM_INSERTITEM message to the list ctrl by  SendMessge API function from the other therad, the SendMessage function does not return.

2. I have also tried to use PostMessage instead of
   SendMessage, but then, the list view control shows
   a characters without any connection to the  
   'lvItem.pszText' string that I passed to the message.

By the way :
The list view control accepts the message
with the wanted string. (I checked it with Spy++)


Please tell me what should I check to fix it ?

Thanks
Michael
Avatar of jtwine100697
jtwine100697
Flag of United States of America image

This should really be in the Windows Programming area, or possibly the MFC area.  But...

1: There should not be a reason what that happens.  Is Thread 1 doing anything when you try to send the message from Thread 2?

2: This will happen if the buffer pointer to by "pszText" is no longer valid by the time the message gets processed (for example, it was a stack-allocated buffer, and the scope of allocation has been passed.

You should provide a code snippet of how you are initializing the LVITEM structure, what flags you are setting, not using OWNERDATA style, etc.

-=- James.
Avatar of VincentLawlor
VincentLawlor

May be off track here but have you looked as using

SendMessageCallback

Sounds like your pointer to your character array is being deleted before the window can process the message.

Vin.
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Juan Carlos
try this: Create a listview control in your thread, and attach it to ListView control of the other thread (plstvw->Attach(HWND), if work use InsertItem directly: plstvw->GetListCtrl().InsertItem...
Try use PostThreadMessage instead of PostMessage!
PostMessage(...), PostThreadMessage(...) and SendMessageCallback(...) both have the same potential problem: The buffer you pass in the LVITEM structure needs to "hang around" long enough to be used by the ListView control.  

Example: if the buffer was allocated off of the stack, and your function returns right after the PostMessage(...)/PostThreadMessage(...)/SendMessageCallback(...) call, the buffer contents are likely to get corrupted, and the ListView control may display garbage or possibly crash.

I continue to believe that Problem #1 is where all of your problems are: If SendMessage is not returning, that is likely due to the target thread not maintaining its message pump.

-=- James.