1,字符串缓冲区的作用不同:前者pItem用来接收ITEMTEXT,我们可以通过ReadProcessMemory函数来读取其内容;而在本篇中p_MyItemText则用来存放我们要设置的ITEMTEXT,并用WriteProcessMemory函数将其写入到目标程序中。
二:源代码:
//
/*
* Send LVM_SETITEMTEXT
* 版权所有 (C) 2005 天津 赵春生
* 2005.10.28
* http://timw.yeah.net
* http://timw.126.com
* 本程序适用于:Win2KP+SP4[Windows TaskManager(5.0.2195.6620)]
* WinXP+SP1[Windows TaskManager]
* 代码在Win2000P+SP4 + VC6+SP6测试通过
*/
#include<windows.h>
#include<commctrl.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd;
int iItem=0;
LVITEM lvitem, *plvitem;
DWORD PID;
HANDLE hProcess;
char *p_MyItemText;//目标程序中用来存放TEXT的地址
//The pszText member is the pointer to a null-terminated
// string containing the new text; it can also be NULL.
//以上信息是从API手册中获得的,故在本例中字符串长度不能>=12,
//以确保字符串后有NULL。
char str_MyItemText[12]={0};
strcpy(str_MyItemText,"天津 赵春生");
hwnd=FindWindow("#32770","Windows 任务管理器");
hwnd=FindWindowEx(hwnd,0,"#32770",0);
hwnd=FindWindowEx(hwnd,0,"SysListView32",0);
if (!hwnd)
MessageBox(NULL,"[Windows 任务管理器] 尚未启动!","错误!",NULL);
else
{
GetWindowThreadProcessId(hwnd, &PID);
hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
if (!hProcess)
MessageBox(NULL,"获取进程句柄操作失败!","错误!",NULL);
else
{
plvitem=(LVITEM*)VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
p_MyItemText=(char*)VirtualAllocEx(hProcess, NULL, 12, MEM_COMMIT, PAGE_READWRITE);
if ((!plvitem)||(!p_MyItemText))
MessageBox(NULL,"无法分配内存!","错误!",NULL);
else
{
MessageBox(NULL,"本演示程序将更改TaskManager中第6个项目中第1列的内容。","提示",NULL);
iItem=5;//5在这里是第六个(从零开始)
lvitem.iSubItem=0;//同上
lvitem.pszText=p_MyItemText;
WriteProcessMemory(hProcess, p_MyItemText, &str_MyItemText, 12, NULL);
WriteProcessMemory(hProcess, plvitem, &lvitem, sizeof(LVITEM), NULL);
//向目标程序发送LVM_SETITEMTEXT消息
SendMessage(hwnd, LVM_SETITEMTEXT, (WPARAM)iItem, (LPARAM)plvitem);
}
}
}
//释放内存
CloseHandle(hwnd);
CloseHandle(hProcess);
VirtualFreeEx(hProcess, plvitem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, p_MyItemText, 0, MEM_RELEASE);
return 0;
}