Solved

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

Posted on 2004-10-27
601 Views
Last Modified: 2008-01-09
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
0
Question by:sundeep07_er
    14 Comments
     
    LVL 3

    Expert Comment

    by:georg74
    hi,
    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.

    HTH,
    georg

    PS: if you need help with a distinct part of the code, please be more specific.
    0
     

    Author Comment

    by:sundeep07_er
    Hi
    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.
    0
     
    LVL 3

    Expert Comment

    by:georg74
    > 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,
    georg
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    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,
    Pratap
    0
     

    Author Comment

    by:sundeep07_er
    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

    http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20096218.html?query=text+of+a+treeview+item+in+another+application&clearTAFilter=true

    the expert who answered ,
    says  

     " 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.
    thanks!


    0
     
    LVL 11

    Assisted Solution

    by:pratap_r
    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;
          DWORD dw=SendMessage(htree,TVM_GETNEXTITEM,TVGN_ROOT,NULL);
          wsprintf(txt,"%8.8x",dw);
          MessageBox(NULL,txt,"treeitem",0); //will display
          
          GetWindowText(htree,txt,255);
          MessageBox(NULL,txt,"the title text",0);

          dw=SendMessage(htree,TVM_GETNEXTITEM,TVGN_CARET,NULL);
          wsprintf(txt,"%8.8x",dw);
          MessageBox(NULL,txt,"selected item",0);


          TVITEM tv;
          tv.mask=TVIF_TEXT | TVIF_HANDLE;
          tv.hItem=(HTREEITEM)dw;
          memset(t2,0,sizeof(t2));
          tv.pszText=t2;
          tv.cchTextMax=sizeof(t2);
          dw=SendMessage(htree,TVM_GETITEM,0,(LPARAM)&tv);
          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,
    pratap
    0
     
    LVL 3

    Expert Comment

    by:georg74
    sundeep,

    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,
    georg
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    georg,

    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,
    pratap
    0
     
    LVL 3

    Expert Comment

    by:georg74
    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
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    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?

    Thanks
    Pratap
    0
     
    LVL 3

    Accepted Solution

    by:
    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,
    georg
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    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
    Pratap
    0
     

    Author Comment

    by:sundeep07_er
    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.
    0
     

    Expert Comment

    by:donsingh2k
    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.

    Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Suggested Solutions

    Title # Comments Views Activity
    haveThree challenge 22 81
    twoTwo  challenge 35 59
    VB 6.0 printer how to align 6 16
    How Complex Is This Java Course ? 9 32
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now