This is more difficult than one might think. In order to get the information you're looking for, you have to execute the code in the same process space as the application containing the ListView control. You do that by allocating a block of memory in the target process that is large enough to hold the LVITEM structure and the string data you expect to be returned, then move that structure to the target process. The .NET Framework makes things quite a bit easier on us here with the Marshal class.

Once you've taken that into account, it's a simple matter of declaring the LVITEM structure and sending the LVM_GETITEM message to the control using the SendMessage function, as you suspected.

// P/Invoke declarationsprivateconstint LVM_FIRST =0x1000;privateconstint LVM_GETITEMCOUNT = LVM_FIRST +4;privateconstint LVM_GETITEM = LVM_FIRST +75;privateconstint LVIF_TEXT =0x0001;[DllImport("user32.dll"),CharSet=CharSet.Auto]privatestaticexternIntPtrSendMessage(IntPtr hWnd,intMsg,IntPtr wParam,IntPtr lParam);[StructLayoutAttribute(LayoutKind.Sequential)]privatestruct LVITEM
{publicuint mask;publicint iItem;publicint iSubItem;publicuint state;publicuint stateMask;publicIntPtr pszText;publicint cchTextMax;publicint iImage;publicIntPtr lParam;}

And then you would use it like this (assuming hListView is a valid handle to a ListView control):

// Declare and populate the LVITEM structure
LVITEM lvi =new LVITEM();
lvi.mask = LVIF_TEXT;
lvi.cchTextMax =512;
lvi.iItem =1;// the zero-based index of the ListView item
lvi.iSubItem =0;// the one-based index of the subitem, or 0 if this// structure refers to an item rather than a subitem
lvi.pszText =Marshal.AllocHGlobal(512);// Send the LVM_GETITEM message to fill the LVITEM structureIntPtr ptrLvi =Marshal.AllocHGlobal(Marshal.SizeOf(lvi));Marshal.StructureToPtr(lvi, ptrLvi,false);SendMessage(hListView, LVM_GETITEM,IntPtr.Zero, ptrLvi);// Extract the text of the specified itemstring itemText =Marshal.PtrToStringAuto(lvi.pszText);

最新文章

  1. MFC下打开选择文件夹并获取文件夹的绝对路径
  2. Windows Server 2008 R2 添加且制成“NFS服务器”角色后与Unix客户端匿名访问常见问题
  3. 关于 window.parent, window.top, window.self 详解
  4. Redis学习笔记8--Redis发布/订阅
  5. Browsersync + Gulp.js
  6. switch 与 python字典
  7. css002 创建样式和样式表
  8. android button 函数调用栈
  9. canvas学习之圆周运动
  10. ASP.NET MVC 返回JsonResult序列化内容超出最大限制报错的解决办法
  11. Spring MVC 流程图
  12. bzoj2427: [HAOI2010]软件安装
  13. WinMerge文件编码设置
  14. django - 总结 - 中间件
  15. javascript 字符串与正则
  16. java学习-- String
  17. pyinstaller,scrapy和apscheduler
  18. Codeforces 781E Andryusha and Nervous Barriers 线段树 单调栈
  19. 学习笔记(2)——实验室集群LVS配置
  20. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

热门文章

  1. python 自动化之路 day 09 进程、线程、协程篇
  2. 实现scp自动输入密码(判断yesno选项)
  3. java异常处理机制 (转载)
  4. php生成随机产生六位数密码的代码
  5. PHP学习心得(十)——控制结构
  6. Linux是一门真正的黑客高手艺术
  7. 基于C#的SolidWorks插件开发(2)--创建插件
  8. 关于JAVA面向对象基础整理以及个人的理解(适合初学者阅读)
  9. shell 练习
  10. 四大主流云平台对比--CloudStack, Eucalyptus, vCloud Director和OpenStack。