Onderstaande code is gebasseerd op http://support.microsoft.com/default.aspx?scid=kb;EN-US;q155895.
Met als verschillen:
-Het ipv de htreeitem met de opgegeven text terug te geven, wordt er een boolean teruggegeven die aangeeft of het opgegeven treeitem (of de root) de opgegeven substring bevat
-Het moet voor externe processen werken (ik wil dus op de text van een treeitem in een ander programma kunnen controleren)
Het probleem is dat de tekstbuffer (szBuffer) leeg blijft (
) en hoop dat jullie weten wat er fout kan zijn of een beter idee hebben (ik ben me er bewust van dat VirtualAllocEx alleen onder NT4/2000/XP werkt)
Ik wil dit trouwens toepassen op Kazaa om de status van downloads/uploads uit te lezen voor hetvolgende progje: http://www.netsetup.nl/marcel/fta
Ik hoop dat er hier enkele mensen zijn die enigzins wijs uit deze code kunnen worden
en mij ook nog kunnen helpen.
Met als verschillen:
-Het ipv de htreeitem met de opgegeven text terug te geven, wordt er een boolean teruggegeven die aangeeft of het opgegeven treeitem (of de root) de opgegeven substring bevat
-Het moet voor externe processen werken (ik wil dus op de text van een treeitem in een ander programma kunnen controleren)
Het probleem is dat de tekstbuffer (szBuffer) leeg blijft (
Ik wil dit trouwens toepassen op Kazaa om de status van downloads/uploads uit te lezen voor hetvolgende progje: http://www.netsetup.nl/marcel/fta
Ik hoop dat er hier enkele mensen zijn die enigzins wijs uit deze code kunnen worden
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| //---------------------------------------------------------------
// TreeitemContainsText
//
// Should return true if the specified (external) treeitem (by hWnd and hItem)
// contains the substring szItemName (but it does not, anyone know why?)
//---------------------------------------------------------------
BOOL TreeitemContainsText(HWND hWnd, HTREEITEM hItem,LPCTSTR szItemName)
{
#define MAXTEXTLEN 256
// If hItem is NULL, use root item.
if (hItem == NULL)
hItem = (HTREEITEM)SendMessage(hWnd, TVM_GETNEXTITEM,TVGN_ROOT, 0);
char szBuffer[MAXTEXTLEN+1];
//initialize the local TVITEM struct
TV_ITEM item;
item.hItem = hItem;
item.mask = TVIF_TEXT;
item.pszText = szBuffer;
item.cchTextMax = MAXTEXTLEN;
DWORD dwProcessId;
//get the process ID of hWnd
GetWindowThreadProcessId(hWnd, &dwProcessId);
//open a handle to the remote process
HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, 0, dwProcessId);
//Allocate memory in the remote process's address space
void *lpMem = VirtualAllocEx(hProcess, 0, 40, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
void *lpMemtext = VirtualAllocEx(hProcess, 0, MAXTEXTLEN, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
//NOTE: The text data will be written at *lpMemtext .
item.pszText = (char *)lpMemtext;
//Write the local TVITEM to the remote memory block
WriteProcessMemory(hProcess, lpMem, &item, 40,NULL);
//Tell the treeview control to fill the remote TVITEM structure
SendMessage(hWnd, TVM_GETITEM, 0, (LPARAM)(TV_ITEM FAR*)lpMem);
//Read the remote text string into our local array buffer
ReadProcessMemory(hProcess,lpMemtext, szBuffer, MAXTEXTLEN, NULL);
//Free the memory in the remote process's address space
VirtualFreeEx(hProcess, lpMem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, lpMemtext, 0, MEM_RELEASE);
CloseHandle(hProcess);
// Does the treeitem contain the text?
if (strstr(szBuffer, szItemName) != NULL)
{
return true;
}
else
{
return false;
}
} |