void MyGetListViewItem(HWND hWindow,TStrings *strlist)
{
const nMaxLen=1023;
char szBuf[nMaxLen+1];
int nLVItemCount;
DWORD dwProcessID;
HANDLE hProcess;
LVITEM lvItemLocal;
DWORD dwBytesRead, dwBytesWrite;
bool bSuccess,bWriteOK;
//注意:本文来自www.ccrun.com,by ccrun(老妖),转载请注明出处。
//为防止某些不负责任的转载者,故出此下策,在代码中加入声明,请大家原谅。
GetWindowThreadProcessId(hWindow,&dwProcessID);
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID);
if(!hProcess) //得不到指定进程的句柄
return;
//在指定进程内分配存储空间
LPVOID lpTextRemote=VirtualAllocEx(hProcess,NULL,nMaxLen+1,MEM_COMMIT,PAGE_READWRITE);
LPVOID lpListItemRemote=VirtualAllocEx(hProcess,NULL,sizeof(LVITEM),MEM_COMMIT,PAGE_READWRITE);
if((!lpTextRemote) || (!lpListItemRemote)) //不能在指定进程内分配存储空间
return;
nLVItemCount=ListView_GetItemCount(hWindow);
strlist->Add("Welcome to www.ccrun.com");
strlist->Add("ListView的Item总数: " + String(nLVItemCount));
strlist->Add("---------------------------");
for(int i=0;i<nLVItemCount;i++)
{
ZeroMemory(szBuf,nMaxLen+1);
bWriteOK= WriteProcessMemory(hProcess,lpTextRemote,(LPVOID)szBuf,nMaxLen+1,(LPDWORD)&dwBytesWrite);
if(!bWriteOK) //写内存错误
return;
lvItemLocal.iItem=i;
lvItemLocal.iSubItem=0;
lvItemLocal.mask=LVIF_TEXT;
lvItemLocal.cchTextMax=nMaxLen;
lvItemLocal.pszText=(LPTSTR)lpTextRemote;
dwBytesWrite=0;
bWriteOK=WriteProcessMemory(hProcess,lpListItemRemote,(LPVOID)&lvItemLocal,sizeof(LVITEM),(LPDWORD)&dwBytesWrite);
if(!bWriteOK) //写内存错误
return;
SendMessage(hWindow,LVM_GETITEMTEXT,(WPARAM)i,(LPARAM)lpListItemRemote);
bSuccess=ReadProcessMemory(hProcess,lpTextRemote,szBuf,nMaxLen+1,&dwBytesRead);
//从指定进程存储空间读取文本
if(!bSuccess) //不能在指定进程内读取文本
return;
strlist->Add(AnsiString(szBuf));
}//end of for(i)
//在指定进程内释放存储空间
VirtualFreeEx(hProcess,lpListItemRemote,0,MEM_RELEASE);
VirtualFreeEx(hProcess,lpTextRemote,0,MEM_RELEASE);
//关闭指定进程句柄
CloseHandle(hProcess);
}
调用的时候这样:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyGetListViewItem((void *)0x000100DC,Memo1->Lines);
}
上面的((void *)0x000100DC是我用MiniSpy取得的桌面的名柄(win2000下的桌面是个ListView),在实际调用时换成你取得的ListView的句柄即可。并且在上面的这段代码只是获取ListView的Item的Caption的。
如
HWND hListView;
//....获取ListView的句柄
MyGetListViewItem(hListView,Memo1->Lines);
如果其ViewStyle为vsReport,就要考虑读取ListView的HeadItem了。呵呵。GoodLuck.
------------------
void GetTadkList(CListBox&list)
{CString s;int i=0;
//Get first Window in window list.
CWnd* pWnd=AfxGetMainWnd();
//Walk window list.
while (pWnd)
{ // I window visible, has a caption, and does not have an owner?
if (pWnd ->IsWindowVisible () &&pWnd ->IsWindowVisible () &&
pWnd ->GetWindowTextLength () &&! pWnd ->GetOwner ())
{
//Add caption o window to list box.
pWnd ->GetWindowText(s);
list.AddString (s);
pWnd=pWnd->GetWindow (GW_HWNDNEXT);
}
}
使用方法:1、在工程中加入上述函数
2、在工程中加入列表框对象list
3、调用GetTadkList(list)函数,桌面窗口全部加在了列表框中。
-----------------------
#include <shlobj.h>
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
#pragma comment(lib,"shell32.lib")
int PrintDesktop()
{
LPMALLOC pMalloc;
LPITEMIDLIST pidlProgFiles = NULL;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfFirstFolder = NULL;
IShellFolder *psfDeskTop = NULL;
IShellFolder *psfProgFiles = NULL;
LPENUMIDLIST ppenum = NULL;
ULONG celtFetched;
HRESULT hr;
STRRET strDispName;
TCHAR pszDisplayName[MAX_PATH];
ULONG uAttr;
CoInitialize( NULL );
hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->QueryInterface(IID_IShellFolder,(LPVOID *) &psfProgFiles);
psfDeskTop->Release();
hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS , &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
printf("%s/n",pszDisplayName);
if(!psfFirstFolder)
{
uAttr = SFGAO_FOLDER;
psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr);
if(uAttr & SFGAO_FOLDER)
{
//Here Can Do Enum Sub Folder.
//hr = psfProgFiles->BindToObject(pidlItems, NULL, IID_IShellFolder, (LPVOID *) &psfFirstFolder);
//psfFirstFolder->Release();
//psfFirstFolder = NULL;
}
}
pMalloc->Free(pidlItems);
}
printf("/n",pszDisplayName);
pMalloc->Free(pidlProgFiles);
pMalloc->Release();
psfProgFiles->Release();
CoUninitialize();
return 0;
}
----------------
LPMALLOC pMalloc;
LPITEMIDLIST pidlProgFiles = NULL;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfFirstFolder = NULL;
IShellFolder *psfDeskTop = NULL;
IShellFolder *psfProgFiles = NULL;
LPENUMIDLIST ppenum = NULL;
ULONG celtFetched;
HRESULT hr;
STRRET strDispName;
TCHAR pszDisplayName[MAX_PATH];
ULONG uAttr;
CString str;
int i = 0;
CoInitialize( NULL );
hr = SHGetMalloc(&pMalloc);//)返回一个指向IMalloc的接口
//返回一个ITEMIDLIST 结构的文件夹的路径(program files)
hr = SHGetFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY/*CSIDL_PROGRAM_FILES*/, NULL, NULL, &pidlProgFiles);
//返回一个桌面的IShellFolder接口指针
hr = SHGetDesktopFolder(&psfDeskTop);
//返回指定子文件夹的IShellFolder接口指针
hr = psfDeskTop->BindToObject(pidlProgFiles, NULL, IID_IShellFolder, (LPVOID *) &psfProgFiles);
psfDeskTop->Release();
//枚举文件夹中的所有对象
hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
//返回一个子文件夹或一个文件对象的名称
psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
//将结构转换为一个字符串
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
//printf("%s/n", pszDisplayName );
str.Format("(%02d)%s",i++,pszDisplayName);
m_strList.AddString(str);
if(!psfFirstFolder)
{
uAttr = SFGAO_FOLDER;
//获取子文件夹或文件对象的属性
psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr);
if(uAttr & SFGAO_FOLDER)
{
hr = psfProgFiles->BindToObject(pidlItems, NULL, IID_IShellFolder, (LPVOID *) &psfFirstFolder);
}
}
pMalloc->Free(pidlItems);
}
printf("/n/n");
str.Empty();
int j = 0;
ppenum->Release();
m_strList.AddString("**************************************");
m_strList.AddString("**************************************");
m_strList.AddString("**************************************");
if(psfFirstFolder)
{
hr = psfFirstFolder->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
psfFirstFolder->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
//cout << pszDisplayName << '/n';
// printf("%s/n", pszDisplayName );
str.Format("<%02d>%s",j++,pszDisplayName);
m_strList.AddString(str);
pMalloc->Free(pidlItems);
}
}
ppenum->Release();
pMalloc->Free(pidlProgFiles);
pMalloc->Release();
psfProgFiles->Release();
psfFirstFolder->Release();
CoUninitialize();
---------------------
本文介绍了如何通过编程方式跨进程读取其他应用程序中ListView控件的内容,包括使用WriteProcessMemory和ReadProcessMemory函数获取ListView项的Caption,以及列举了获取Windows桌面程序和程序文件夹列表的方法。
2763

被折叠的 条评论
为什么被折叠?



