Solved

Question about List View Control (C++)

Posted on 2001-08-28
6
686 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:jcgd
Comment Utility
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
Comment Utility
Try use PostThreadMessage instead of PostMessage!
0
 
LVL 4

Expert Comment

by:jtwine100697
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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

11 Experts available now in Live!

Get 1:1 Help Now