以下代码来自彗星小助手里的"获取窗口内容"功能
CString CWinSpyCentent::GetListViewCentent(BOOL bReport)
{
CString strCon_tent=_T("");
CString str=_T("");
DWORD PID=0;
HWND hWnd=g_hWndSpy;//目标ListView句柄
GetWindowThreadProcessId(hWnd,&PID);
HANDLE hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
if(!hProcess){
TRACE(_T("获取进程句柄操作失败hWnd=%dPID=%d"),hWnd,PID);
return strContent;
}
LVITEMA* pLVITEM=(LVITEMA*)VirtualAllocEx(hProcess,NULL,sizeof(LVITEM),MEM_COMMIT,PAGE_READWRITE);
char* pItem=(char*)VirtualAllocEx(hProcess,NULL,16,MEM_COMMIT,PAGE_READWRITE);
if(!pLVITEM){
TRACE(_T("无法分配内存!"));
return strContent;
}
LVITEMA LVITEM;
LVITEM.mask=LVIF_TEXT;
LVITEM.cchTextMax=512;
LVITEM.pszText=pItem;
char ItemBuf[512];
int nCount=::SendMessage(hWnd,LVM_GETITEMCOUNT,0,0);
int nColCount=1;
DWORD dwStyle=::GetWindowLong(hWnd,GWL_STYLE);
if(bReport){
HWND header=(HWND)::SendMessage(hWnd,LVM_GETHEADER,0,0);
if(header){
nColCount=::SendMessage(header,HDM_GETITEMCOUNT,0,0);
if(nColCount<1)nColCount=1;
}
}
for(int iItem=0;iItem
for(int iSubItem=0;iSubItem
LVITEM.iItem=iItem;
LVITEM.iSubItem=iSubItem;
//将设置好的结构插入目标进程
WriteProcessMemory(hProcess,pLVITEM,&LVITEM,sizeof(LVITEM),NULL);
//发送LVM_GETITEM消息
::SendMessage(hWnd,LVM_GETITEMA,0,(LPARAM)pLVITEM);
//获取pszText
ReadProcessMemory(hProcess,pItem,ItemBuf,512,NULL);
str=CString(ItemBuf);
if(iSubItem)strCon_tent=strContent+_T("\t\t");
strCon_tent=strContent+str;
}
strCon_tent=strContent+_T("\r\n");
}
//释放内存
VirtualFreeEx(hProcess,pItem,0,MEM_RELEASE);
VirtualFreeEx(hProcess,pLVITEM,0,MEM_RELEASE);
CloseHandle(hProcess);
return strContent;
}
usesCommCtrl;varg_hWndSpy: HWND;strContent: string;function GetListViewCentent(bReport: BOOL): string;varstrCon_tent, str: string;PID, lpNumberOfBytesWritten: DWORD;hWnd, header:Windows.HWND;hProcess: THandle;pLVITEM: ^TLVItem;pItem: PChar;LVITEM: TLVItem;ItemBuf: array[0..511] of AnsiChar;nCount, nColCount, iItem, iSubItem: Integer;beginhWnd := g_hWndSpy; //目标ListView句柄;GetWindowThreadProcessId(hWnd, @PID);hProcess := OpenProcess(PROCESS_ALL_ACCESS,false,PID);if hProcess = 0 thenbeginOutputDebugString(PAnsiChar(Format('获取进程句柄操作失败hWnd=%dPID=%d', [hWnd,PID])));Result := strContent;Exit;end;pLVITEM := VirtualAllocEx(hProcess, nil,sizeof(TLVItem),MEM_COMMIT,PAGE_READWRITE);pItem := VirtualAllocEx(hProcess, nil,16,MEM_COMMIT,PAGE_READWRITE);if pLVITEM = nil thenbeginOutputDebugString(PAnsiChar(Format('无法分配内存!', [])));Result := strContent;Exit;end;LVITEM.mask := LVIF_TEXT;LVITEM.cchTextMax := 512;LVITEM.pszText := pItem;nCount := SendMessage(hWnd,LVM_GETITEMCOUNT,0,0);nColCount := 1;if bReport thenbeginheader := SendMessage(hWnd,LVM_GETHEADER,0,0);if header <> 0 thenbeginnColCount := SendMessage(header,HDM_GETITEMCOUNT,0,0);if nColCount < 1 then nColCount := 1;end;end;for iItem := 0 to nCount-1 dobeginfor iSubItem := 0 to nColCount - 1 dobeginLVITEM.iItem := iItem;LVITEM.iSubItem := iSubItem;//将设置好的结构插入目标进程WriteProcessMemory(hProcess, pLVITEM, @LVITEM, sizeof(TLVItem), lpNumberOfBytesWritten);//发送LVM_GETITEM消息SendMessage(hWnd,LVM_GETITEMA,0, LPARAM(pLVITEM));//获取pszTextReadProcessMemory(hProcess, pItem, @ItemBuf[0], 512, lpNumberOfBytesWritten);str := ItemBuf;if iSubItem <> 0 thenstrCon_tent := strContent + #9#9;end;strCon_tent := strContent + sLineBreak;end;//释放内存VirtualFreeEx(hProcess,pItem,0,MEM_RELEASE);VirtualFreeEx(hProcess,pLVITEM,0,MEM_RELEASE);CloseHandle(hProcess);Result := strContent;end;