Solved

Question about List View Control (C++)

Posted on 2001-08-28
6
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

739 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