Unable to use LVM_GETITEMTEXT without target application crashing in C++

家住魔仙堡 提交于 2019-12-01 04:20:19

问题


I'm trying to capture data from a SysListView32 class (according to Spy++) from another application. Sending a LVM_GETITEMCOUNT message to this handle always returns the correct number of items. There is a child window which is SysHeader32 which presumably contains header titles.

When I try to send a LVM_GETITEMTEXT message to the target application, it crashes. The relevant code for this message is below:

LPTSTR lpText;
LVITEM* lvItem;
lvItem = new LVITEM;
lvItem->iSubItem = 0;
lvItem->cchTextMax = 255;
lvItem->pszText = lpText;
//SysListViewHandle is the HWND to the SysListView32 'content' window
SendMessage(SysListViewHandle, LVM_GETITEMTEXT, 1, (long)lvItem);

Each 'cell' in the list contains text no more than 50 characters, so the max text size should be fine.

The list structure which I wish to get the data from has 16 columns and a variable number of entries, more than 5, so the wParam should be fine. The styles this list use are WS_CHILDWINDOW, WS_VISIBLE, WS_TABSTOP, WS_HSCROLL, LVS_REPORT with extended styles of WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR, WS_EX_NOPARENTNOTIFY, WS_EX_CLIENTEDGE, LVS_GRIDLINES and LVS_FULLROWSELECT.

UISpy is able to probe this list and find the actual data within so I presumed it would be a walk in the park to get using messages, but this has proved not the case =/ any assistance would be greatly appreciated.

EDIT: Worth mentioning the error log on crashing is: Unhandled exception at 0x77582b87 in applicationname.exe: 0xC0000005: Access violation writing location 0x01bc48b0. Call stack location comctl32.dll Disassembly: 77582B87 mov dword ptr [esi],1


回答1:


Your problem is that since the list view exists in another process, the memory you allocate is not valid in that other process. I refer you to an article over at The Code Project which offers a solution.

What's more, you do not appear to have allocated any memory for lpText so it would fail in your own process.



来源:https://stackoverflow.com/questions/4616281/unable-to-use-lvm-getitemtext-without-target-application-crashing-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!