One: Procedural description
This time I'll show you how to change the contents of an item in another program ListView control. I have written two articles on similar articles, this is the third, this is similar to the first article "How to send a lvm_getitemtext message to a ListView control of another program", the difference being:
- The message sent is different: The former is read psztext content-Send Lvm_getitemtext, this is set psztext, should send lvm_setitemtext;
- string buffers function differently: The former pitem is used to receive itemtext, we can read its contents through the ReadProcessMemory function, and in this paper the P_myitemtext is used to store the itemtext we want to set. and writes it to the target program using the WriteProcessMemory function.
As a demo, the following program will change the contents of column 1th in the 6th item in TaskManager. The effect diagram after the program is run:
II: Concrete Practice
//////////////////////////////////////////////////////////////////////////////////
/*
* Send Lvm_setitemtext
* Copyright (C) 2005 Tianjin Changchun
* 2005.10.28
* Http://timw.yeah.net
* http://timw.126.com
* This procedure applies to: Win2kp+sp4[windows TaskManager (5.0.2195.6620)]
* Winxp+sp1[windows TaskManager]
* Code in WIN2000P+SP4 + VC6+SP6 test Pass
*/
//////////////////////////////////////////////////////////////////////////////////
#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;//the address in the target program where text is to be stored
The Psztext member are the pointer to a null-terminated
String containing the new text; It can also be NULL.
The above information is obtained from the API manual, so in this case the string length cannot be >=12,
To ensure that there is null after the string.
Char str_myitemtext[12]={0};
strcpy (Str_myitemtext, "Tianjin Changchun");
Hwnd=findwindow ("#32770", "Windows Task Manager");
Hwnd=findwindowex (hwnd,0, "#32770", 0);
Hwnd=findwindowex (hwnd,0, "SysListView32", 0);
if (!hwnd)
MessageBox (NULL, [Windows Task Manager] has not yet started!) "," the mistake! ", NULL);
Else
{
GetWindowThreadProcessId (hwnd, &PID);
Hprocess=openprocess (PROCESS_ALL_ACCESS,FALSE,PID);
if (!hprocess)
MessageBox (NULL, "Get process handle operation failed!") "," the mistake! ", NULL);
Else
{
Plvitem= (lvitem*) VirtualAllocEx (hprocess, NULL, sizeof (Lvitem), Mem_commit, page_readwrite);
p_myitemtext= (char*) VirtualAllocEx (hprocess, NULL, Mem_commit, page_readwrite);
if ((!plvitem) | | (!p_myitemtext))
MessageBox (NULL, "Unable to allocate memory!") "," the mistake! ", NULL);
Else
{
MessageBox (NULL, "This demo will change the contents of column 1th in the 6th item in TaskManager.) "," hint ", NULL);
IITEM=5;//5 here is the sixth (starting from scratch)
lvitem.isubitem=0;//ditto
Lvitem.psztext=p_myitemtext;
WriteProcessMemory (hprocess, P_myitemtext, &str_myitemtext, A, NULL);
WriteProcessMemory (hprocess, Plvitem, &lvitem, sizeof (Lvitem), NULL);
Send a LVM_SETITEMTEXT message to the target program
SendMessage (hwnd, Lvm_setitemtext, (WPARAM) IItem, (LPARAM) plvitem);
}
}
}
Free memory
CloseHandle (HWND);
CloseHandle (hprocess);
VirtualFreeEx (hprocess, Plvitem, 0, mem_release);
VirtualFreeEx (hprocess, P_myitemtext, 0, mem_release);
return 0;
}
Third, the above code in the WIN2000P+SP4 + VC6+SP6 test pass.