Solved

Question about List View Control (C++)

Posted on 2001-08-28
6
698 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:MichaelMaromm
6 Comments
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6433559
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.
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6434526
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.
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 50 total points
ID: 6435197
Next is may code for insert item.Reason of your error,
may be, in using memory , allocated for item:

int TMyListView::AddItem(char *szText, int iItem)
{
  LV_ITEM lvi;
    // Initialize LV_ITEM members that are common to all items.
  lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
  lvi.state = 0;
  lvi.stateMask = 0;
  lvi.pszText = LPSTR_TEXTCALLBACK;   // app. maintains text
  lvi.iImage = 0;                     // image list index

        // item label and the text of each subitem.
   char *pItem = strdup(szText);//this is place!!!!

        // Initialize item-specific LV_ITEM members.
   lvi.iItem = iItem;
   lvi.iSubItem = 0;
   lvi.lParam = (LPARAM) pItem;    // item data

        // Add the item.
  return ::PostMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) (const LV_ITEM FAR *)&lvi);
}
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:jcgd
ID: 6447045
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...
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6452130
Try use PostThreadMessage instead of PostMessage!
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6452974
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now