Click here to Skip to main content
15,881,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a tree view from where I need to fetch the text from all the tree view items one by one. The TreeView_GetItem() is returning 0, could not find the problem.

I m posting the code. Please help me, I'm new to VC.

C++
BOOL CGuiEzAutomator::GetBoolVal(const char* szWndName, const char* szTreeCtrlId)
{
    BOOL bRet = FALSE;

    TV_ITEM *tvi;


    // get teh tree view control handle
    HWND hWnd = GetTreeViewHandleForDW(szWndName, szTreeCtrlId);

    HANDLE hProcess = OpenProcessHandle(hWnd);


if(NULL != hWnd)
    {

    int nCount = TreeView_GetCount(hWnd);

    HTREEITEM hItem = TreeView_GetRoot( hWnd );

    tvi=(TV_ITEM*)VirtualAllocEx(hProcess, NULL, sizeof(TV_ITEM), MEM_COMMIT, PAGE_READWRITE);

    TV_ITEM* ptvi = new TV_ITEM;

        memset(ptvi, 0, sizeof(TV_ITEM));

        ptvi->mask = TVIF_HANDLE;
        ptvi->pszText = (LPSTR) VirtualAllocEx(hProcess, NULL, 400 * sizeof(TCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        ptvi->cchTextMax = 400;
        ptvi->hItem = hItem;

        while(ptvi->hItem)
        {
        BOOL WRITE =    WriteProcessMemory(hProcess, &tvi, ptvi, sizeof(TV_ITEM), NULL);

    //  CString Text = GetItemText(hItem);

            BOOL ret = TreeView_GetItem(hWnd, ptvi);

            DWORD dwErr = ::GetLastError();

            if (BOOL ret = TreeView_GetItem(hWnd, ptvi))
            {

                ReadProcessMemory(hProcess, &ptvi, &tvi, sizeof(TV_ITEM), NULL);

                TCHAR * sText = new TCHAR(ptvi->cchTextMax);

                CString sRetText = ptvi->pszText;

            //  if((ptvi->pszText) == "Given Text")
            //  {
                    //TO DO
            //  }
            }
                // Go to next sibling item.
                    ptvi->hItem = TreeView_GetNextItem(hWnd, ptvi->hItem, TVGN_NEXT);

        }

        // Free the memory in the remote process's address space
        VirtualFreeEx(hProcess, ptvi, 0, MEM_RELEASE);
    }
    return TRUE;
}
Posted
Updated 21-Jun-10 22:27pm
v2

Generally, doing this
BOOL ret = TreeView_GetItem(hWnd, ptvi);

twice without resetting the ptvi->pszText member is a bad idea, since it might tamper with the tree views internal structures.

It can't work since it doesn't even compile, and I would not expect the call to work since it needs to access the ptvi struct which resides in your address space. Maybe a VirtualAlloc there instead of a new?

Assuming of course you're trying to query a tree view in another process.
 
Share this answer
 
Hi Thanks for ur reply

I just changed code little bit like this

C++
BOOL CGuiEzAutomator::GetBoolVal(const char* szWndName, const char* szTreeCtrlId)
{
    BOOL bRet = FALSE;

    TV_ITEM *tvi;


    // get teh tree view control handle
    HWND hWnd = GetTreeViewHandleForDW(szWndName, szTreeCtrlId);

    HANDLE hProcess = OpenProcessHandle(hWnd);

    HGLOBAL hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(TCHAR) * 10240);
    LPTSTR pClipData = (LPTSTR) GlobalLock(hClipData);
    pClipData[0] = 0;


    if(NULL != hWnd)
    {


    int nCount = TreeView_GetCount(hWnd);

    HTREEITEM hItem = TreeView_GetRoot( hWnd );

    tvi=(TV_ITEM*)VirtualAllocEx(hProcess, NULL, sizeof(TV_ITEM), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

    TVITEMEX item;
        item.hItem = hItem;
        item.mask = TVIF_TEXT | TVIF_HANDLE ;
        item.pszText = (LPTSTR) (tvi + 1);
        item.cchTextMax = 100;

        while(item.hItem)
        {
        BOOL WRITE =    WriteProcessMemory(hProcess, tvi, &item, sizeof(item), NULL);

            if (BOOL ret = TreeView_GetItem(hWnd, tvi))
                {

                    BOOL READ = ReadProcessMemory(hProcess, tvi, &pClipData[0], 1024, NULL);

                    CString csCurrentNode(pClipData);

                if(csCurrentNode.CompareNoCase("Given Text //assuming ") == 0 )
                {
                    bRet = TRUE;
                }
            }
                // Go to next sibling item.
                    item.hItem = TreeView_GetNextItem(hWnd, item.hItem, TVGN_NEXT);

        }

        // Free the memory in the remote process's address space
        VirtualFreeEx(hProcess, tvi, 0, MEM_RELEASE);
    }

    GlobalFree(hClipData);
    CloseHandle(hProcess);
    return bRet;
}



By doing this also i m getting the Text of the hItem

Not getting where i m lacking

Help! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900