如何从另一个应用程序捕获事件?

如何从另一个应用程序捕获事件?

问题描述:

我有一个带有CListCtrl,样式报告,OWNER_DRAW和OWNER_DATA的程序.我不知道代码.我必须为此程序下载此列表中的任何新项目(每个项目包含3个子项).
速度是必需的阅读列表,并将其插入我的程序中,每个新项目都出现在位置0.
我找到了手柄控件并执行以下操作:
*计数元素

I have a program with a CListCtrl, Style REPORT, OWNER_DRAW and OWNER_DATA. I do not know the code. I have to download any new item in this list for my program (each consists of 3 subitems).
Speed is required reading list and insert this into my program, each new item appears on the position 0.
I find the handle controls and do this:
* Counting elements

int getItemCount(HWND mHwnd)
{
        return((int)SendMessage(mHwnd, LVM_GETITEMCOUNT, 0, 0));
}



*我确实阅读了该内容



* I do read the item so

LPSTR getItem(HWND listview, int nItem, int nType)
{
        int i;
        LVITEM lvi, *_lvi;
        char item[512], subitem[512];
        char *_item, *_subitem;
        unsigned long pid;
        HANDLE process;
        LPSTR lps="";

        GetWindowThreadProcessId(listview, &pid);
        process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid);
        _lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM),MEM_COMMIT, PAGE_READWRITE);
        _item=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,PAGE_READWRITE);       
        _subitem=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,PAGE_READWRITE);       
        lvi.cchTextMax=512;

        i=nItem;


        if(nType==0)
        {
                lvi.iSubItem=0;
                lvi.pszText=_item;
       
                WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
                SendMessage(listview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
                ReadProcessMemory(process, _item, item, 512, NULL);
                lps=item;
        }
        if(nType==1)
        {
                lvi.iSubItem=1;
                lvi.pszText=_subitem;
       
                WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
                SendMessage(listview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
                ReadProcessMemory(process, _subitem, subitem, 512, NULL);
                lps=subitem;
        }
                               
        VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);                               
        VirtualFreeEx(process, _item, 0, MEM_RELEASE);
        VirtualFreeEx(process, _subitem, 0, MEM_RELEASE);               

        free(_lvi);
        free(_item);
        free(_subitem);
       
return(lps);
}



如果不是因为我必须以一种非常愚蠢的方式来完成阅读列表,那么一切都会变得很漂亮,即在循环中while(hwnd!= NULL)....
不必要地给CPU造成负担,并且暂时是一种效率很低的方式,我损失了300到600毫秒,因为在循环中比较了最后一个元素以验证是否输入了新内容以及输入了多少项目.
目前,在未指定的时间,新产品的数量为1至10,甚至更多.程序不断检测它是否是新事物.

如何捕获外部应用程序通知新项目的事件? 大概是LVN_ITEMNOTIFY ????



everything would be beautiful if not for the fact that the reading list I have to do in quite a stupid way, namely in the loop while (hwnd! = NULL) ....
unnecessarily burden the CPU and it is quite inefficient way temporarily, I lose 300 to 600 ms because in the loop as the last elements are compared to verify whether entered something new or not and how many items entered.
New items come in quantities of 1 to 10 and more at the moment at an unspecified time. Programm continuously detects whether it is something new.
How to capture the event that an external application notifying the new item ????
Probably LVN_ITEMNOTIFY ????

Thanks for your help!

您必须使用SetWindowsHookExWH_SYSMSGFILTER挂钩类型安装挂钩过程. />
这样,系统会在将任何消息发送到在运行线程的同一桌面上运行的所有应用程序的任何消息循环中发送消息之前调用挂接过程:您有责任仅过滤您感兴趣的消息.

使用SetWindowsHookEx函数时要小心,因为这可能会导致严重的问题.我强烈建议您仔细阅读 MSDN 上的所有相关文档.
You must install an hook procedure using the SetWindowsHookEx and the WH_SYSMSGFILTER hook type.

This way, the system calls your hook procedure before sending any message to any message loop of all the applications running on the same desktop where your thread is running: is your responsibility to filter only the messages of your interest.

Be careful in using the SetWindowsHookEx function, because you can cause serious problems; I strongly recommend you to carefully read all the related documentation on the MSDN.