Freigeben über


Control SysTreeView and SysListView in another window/process

Last year I developed one tool to control the SysTreeView and SysListView in Clarify (one internal tool). Here I'd like to share a part of sour code.

How to enumerate the windows
===============================
    HWND hClarifyWnd;
    EnumWindows(EnumClarifyProc, (LPARAM)&hClarifyWnd);

BOOL CALLBACK EnumClarifyProc(HWND hWnd, LPARAM lParam)
{
    if( IsWindowVisible(hWnd) ) {
        HWND* phCwnd = (HWND*)lParam;
        int nLen = GetWindowTextLength(hWnd);
        LPSTR lpszTitle = new CHAR[nLen + 1];
        GetWindowText(hWnd, lpszTitle, nLen+1);
        if(strstr(lpszTitle, CLARIFY_TITLE)){
            //assign window handle
            *phCwnd = hWnd;
            delete lpszTitle;
            return TRUE;
        }
        delete lpszTitle;
    }
    return TRUE;
}

How to control the controls in another window/process
====================================
bool SendMessageWrapper(HWND hWnd, HANDLE hProcess, HTREEITEM hItem, int TVM_FLAG, TV_ITEM *tvItemParam, char* item)
{
    TV_ITEM tvItm,*tvItem;
    if (tvItemParam!=NULL)
        tvItem = tvItemParam;
    else
        tvItem = &tvItm;

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

    char *pItem;
    pItem = (char*) VirtualAllocEx(hProcess, NULL, MAX_CHAR_LEN, MEM_COMMIT, PAGE_READWRITE);

    if (ptvItem==NULL || pItem==NULL)
        return false;

    (*tvItem).hItem = hItem;
    (*tvItem).mask = TVIF_TEXT | TVIF_CHILDREN;
    (*tvItem).pszText = pItem;
    (*tvItem).cchTextMax = MAX_CHAR_LEN;

    WriteProcessMemory(hProcess, ptvItem, tvItem, sizeof(TVITEMEX), NULL);

    SendMessage(hWnd, TVM_FLAG, 0, (LPARAM)ptvItem);

    ReadProcessMemory(hProcess, pItem, item, MAX_CHAR_LEN, NULL);
    ReadProcessMemory(hProcess, ptvItem, tvItem, sizeof(TVITEMEX), NULL);

    // free
    VirtualFreeEx(hProcess, ptvItem, 0, MEM_RELEASE);
    VirtualFreeEx(hProcess, pItem, 0, MEM_RELEASE);

    return true;
}
 

ClarifyHelperMsg.cpp