Want to get the item text of a treeiew in another application

Hi experts,
I browsed through various soloutions given in ee , but none had a complete solution .Kindly help me with a source code to get the item text of a treeview control which is in another application running on win 2000.I have already posted this question once but none replied.

I want to loop through all the nodes in the treeview and retreive the text of each item and store it in an array .

pls help
Who is Participating?
yes, but it is not easy with win32.
you could execute the target application as a child process and let it inherit system handles
and have access to a share a data segment.

> Obtain process id of B by GetWindowThreadProcessId(hTV,&pid)
> Get the process handle of B by OpenProcess(PROCESS_VM_WRITE| PROCESS_VM_OPERATION | PROCESS_VM_READ)"
> Allocate memory in the treeview process address space using pMem=VirtualAllocEx().

this seems to be the easiest way if you have the access to the process.

> Use pMem as a buffer for  TreeView_GetItem()

addition: use it for both TVITEM structure AND the text buffer.
access violation and "crashing" won't occur any more.

sundeep, I think now you have the solution you asked for.
check win32 reference to find out how to use this functions.
as I said, vb is not really suitable for such programs, so check if you can use something else.
for example C++. :-)
if you have further specific questions you may want to open new questions ;-)

good luck,
treeview control can be used in different ways and the solution depends on that. first question is if it is "owner drawn". if yes, then only it's application knows about the data (the text). the same applies to list boxes etc. ((you can still read it out, but you will need a simple OCR along with more comlicated control if you want to fully automate the process. It is possible to do that - it's about 3-4 days of work for experienced programmer, I've done that once.))

if no - much better case - then you can get the items by sending TVM_ messages to the control. ((I'd estimate that task to 1-3 hrs for exp. programmer))

practically, you need to do the following:

- use a window inspector (like WinSight) to find out how to get to the handle of the control and get the real handle to experiment with
- having the handle of the control, use TVM_GETNEXTITEM to get the root item handle (use TVGN_ROOT flag)
- also use TVM_GETNEXTITEM to get some subsequent item's handles
- use TVM_GETITEM along with item handle to get information about an item
- at this point, you will find out if you're getting some text from the control or not, and you'll get the idea how to get all of them.


PS: if you need help with a distinct part of the code, please be more specific.
sundeep07_erAuthor Commented:
tnx very much.but Ihave browsed through ee and understood that a "GPF" ERROR occures whenever a tvm_getitem is used .(I have seen the target app.crash .)
how to avoid this,how to access the memory of another process, and stuff like that are what I want to know

please help.I have been searching for a complete solution for a long time but in vain.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

> a "GPF" ERROR occures whenever a tvm_getitem is used

that is surely not true, as this is the only way to get item information.

> how to access the memory of another process

generally speaking, by putting it in debug mode.
but of what use would it be?

if the information you want is visible on the screen, you can copy it from there.

please give us some background information, perhaps there is an workaround.

in the meantime, test if you can get the handle of that treeview control and test if
you can get the root item handle from it.

good luck,
hey sundeep for the gpf.. (i doubt it though) did you allocate the the memory for the string if you are reteriving the text of each node? in other words when using TVIF_TEXT flag?

for reading other programs's memory you need to open the process and then you can use ReadProcessMemory, hey georg i dont think we need to go to debug mode for this... your thoughts!??

Have fun,
sundeep07_erAuthor Commented:
hello pratap,
I did not write any code for getting the text of a treeview item.But always wanted to.
So to learn things I browsed  ee and found its a real tough task (for me!) since i am very very new to vb .

please refer


the expert who answered ,

 " For Windows NT/2000/xp:
Allocate memory in the treeview process address space using pMem=VirtualAllocEx(). Use pMem as a buffer for  TreeView_GetItem(). You can write and read pMem using ReadProcessMemory/WriteProcessMemory.

a) Obtain process id of B by GetWindowThreadProcessId(hTV,&pid)

b) Get the process handle of B by OpenProcess(PROCESS_VM_WRITE| PROCESS_VM_OPERATION | PROCESS_VM_READ)"

how do i do these thing? its all greek and latin.

please help one more time.

hey sundeep.. its a bit hard if you use vb for this... its kind of advanced.. it can be done though..

let me explain the process a bit

since your target treeview lies in a different application (or process) you cannot directly reterive all the details.. especially the text of the item. although the calls to get the Handles will succeeed. this is mainly because of the address space being different. lets say there is a target application which has a treeview with HWND 0x04560B2A (you can find it using the Spy++ which comes with vs6.0)
//you could try this out in a vc console application if you want to, dont forget to include windows.h and commctrl.h
      char txt[255],t2[255];
      HWND htree=(HWND)0x04560B2A;
      MessageBox(NULL,txt,"treeitem",0); //will display
      MessageBox(NULL,txt,"the title text",0);

      MessageBox(NULL,txt,"selected item",0);

      TVITEM tv;
      tv.mask=TVIF_TEXT | TVIF_HANDLE;
      t2[200]='\0';  //to prevent a buffer overrun
      wsprintf(txt,"%s %8.8x %d",t2, dw,TRUE);
      MessageBox(NULL,txt,"Attempt to get text",0);

the above code tries to get the root item first.. as suggested by george.. which suceeds without any problem. the second call getwindowtext also passes. the problem comes in the 4th call when we try to get the text of the treeview item using TVIF_TEXT flag. you will notice that the above code will return a null for the string always and will fail.. to avoid this problem what we do is allocate(for the structure and the string) the memory in the target process itself (which is not just a malloc call) and then pass that as parameter to the TVM_GETITEM call. once sendmessage function call returns the buffer in the target application would have been filled.. now to read that we use ReadProcessMemory...

hope this is clear?

have fun,

unfortunately, vb is not an adequate language for doing this.

I don't think (but I'm not sure if) it is really necessary to allocate the space for retuned data in the conxtext of the owner process.

read my first comment again and try these steps.

- first use some window inspector to get informatiln about controls in that window and about this specific control.

- second, try to send a simple TVM_GETCOUNT message to it (use the current handle obtained with the window inspector), and check the result. It should contain the count of the items in that treeview. (Read in your on-line help about the SendMessage function.)

try to accomplish these two steps first. there is no need to reserve any memeory, get process id or handle etc. after that, you will know much more and you will be in much better position to decide what to do next.

good luck,

the call fails if you dont allocate the memory.. TVM_GETCOUNT will succeed though.. since its just a dword return value, its when passing the structure and the string buffer that the call fails, you can check my code if you want to.. it will fail at the TVM_GETITEM call

Have fun,
it seems that the problem is following:

the tree control is created with the option not to store item properties.
when it needs item properties, it sends a message to the controls window function.
the target application receives the message, and it tries to
use the passed pointer to the TVITEM structure (and text buffer). since the pointed memory
belongs to another application, access is denied and an access violation exception is thrown.
if the target app doesn't catch the exception it will "crash".

so, to access items of treeview controls created in such way,
it is necessary to create a buffer that is accessible
by the target application. it would not have to be in its space.

georg, is there any other way to make a memory address accessible to a target application (for which we dont have the source)? apart from its own memory?

georg, not on the lines of these question but.. when  you dont control the target application launching the target app as a child process and having it inherit the handles .. how good will that be? ultimately at one point we will have to deal with the target's memory space

Have fun
sundeep07_erAuthor Commented:
Thanks very much george and pratap.
I have hope now.
I understood the concept.I will study more on this and get back to u guys 'cause I know I can't do this without any expert's help

Tnx very much.
Sandeep, did you find a viable solutions to do this. This is something i am trying to do, would be great if you could share your discovered solution.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.