Solved

Question about List View Control (C++)

Posted on 2001-08-28
6
725 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

617 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