Windows Develop Bookmark and Share   
 index > Windows Forms General > How to retrieve the LV_ITEM's info by SendMessage
 

How to retrieve the LV_ITEM's info by SendMessage

Hi everybody


I've tried to develop a Shell Context Menu. Below is the code getting listviewitem info from Windows Explorer's listview, during the context menu open

            LV_ITEM lvItem = new LV_ITEM();
            lvItem.mask = LVIF.INDENT | LVIF.PARAM | LVIF.TEXT | LVIF.IMAGE;
            lvItem.iItem = 2;
            lvItem.lParam = IntPtr.Zero;
            lvItem.cchTextMax = 255;
            lvItem.pszText = new string(' ', 256);
            int retIndex = Helpers.SendMessage(hWnd, (uint)LVM.GETITEMA, IntPtr.Zero, ref lvItem);

            m_DebugLog.WriteLog(string.Format("ListViewItem iItem: {0}", lvItem.iItem));
            m_DebugLog.WriteLog(string.Format("ListViewItem iSubItem: {0}", lvItem.iSubItem));
            m_DebugLog.WriteLog(string.Format("ListViewItem pszText: {0}", lvItem.pszText));
            m_DebugLog.WriteLog(string.Format("ListViewItem iImage: {0}", lvItem.iImage));
            m_DebugLog.WriteLog(string.Format("ListViewItem lParam: {0}", lvItem.lParam));
below is the debug log

ListViewItem iItem: 2
ListViewItem iSubItem: 0
ListViewItem pszText: MyFile.txt
ListViewItem iImage: 5 ListViewItem lParam: 0
the problem the pointer lParam always return to 0, beside Item's text and image index work just fine. you guys have any idea?
  • Moved byTaylorMichaelLMVPTuesday, September 01, 2009 1:26 PMWinforms related (From:.NET Base Class Library)
  •  
ttwoosta  Tuesday, September 01, 2009 6:50 AM
Sorry, I missed the fact that you're making a shell extension. That's not recommended to do in managed code at all, I would strongly suggest that you consider rewriting it in native code instead. At least until the next version of the CLR is released.
Mattias, C# MVP
Mattias Sjögren  Wednesday, September 02, 2009 10:29 AM
lParam being null/0 doesn't necessarily mean something is wrong. Have you actually set it to something else? Can you post your LV_ITEM definition?
Mattias, C# MVP
Mattias Sjögren  Tuesday, September 01, 2009 1:14 PM


the lParam was a IntPrt type, here the LV_ITEM definition:
   [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct LV_ITEM
    {
        public LVIF mask;
        public Int32 iItem;
        public Int32 iSubItem;
        public UInt32 state;
        public UInt32 stateMask;
        public String pszText;
        public Int32 cchTextMax;
        public Int32 iImage;
        public IntPtr lParam;
    }

my project is similar to this Windows Explorer wildcard selection shell extension , there is a function to get the listviewitem index like this :

int CtxMenu::GetListViewIndex(const char* filename)
{
	char path[MAX_PATH + 1];

	LVITEM item = { 0 };

	item.mask = LVIF_PARAM;

	int count = ListView_GetItemCount(listView);

	for (int t = 0; t < count; ++t)
	{
		item.iItem  = t;
		item.lParam = 0;

		if (ListView_GetItem(listView, &item))
		{
			if (::SHGetPathFromIDList((LPCITEMIDLIST) item.lParam, path))
			{
				char* file = strrchr(path, '\\');

				if (file != NULL)
				{
					if (stricmp(filename, file + 1) == 0)
					{
						return t;
					}
				}
			}
		}
	}

	return -1;
}



the LV_ITEM mask is specific to LVIF_PARAM then it is passed through ListView_GetItem macro same as the SendMessage func . after that the lParam recieved a ITEMIDLIST
ttwoosta  Tuesday, September 01, 2009 9:49 PM
Sorry, I missed the fact that you're making a shell extension. That's not recommended to do in managed code at all, I would strongly suggest that you consider rewriting it in native code instead. At least until the next version of the CLR is released.
Mattias, C# MVP
Mattias Sjögren  Wednesday, September 02, 2009 10:29 AM

You can use google to search for other answers

Custom Search

More Threads

• Vista Controls coming soon?
• MDI Child form with webcontrol not showing properly after hiding in MDI scenario
• C# Form1 contents wont update if changed in Form2
• Flickering when drawing with graphics object
• CategoryAttribute's target
• ItemMouseOver Event in listview
• Close() Error HELP
• System.Console.WriteLine -> textBox.Text
• using HTMLElement.getAttribute for selecting <OPTION> in <SELECT> tag
• System.Security.Permissions exception when loading a DLL from a network