1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <windows.h>
#include <process.h>
#include <oleacc.h>
#pragma comment (lib, "oleacc.lib")
#include <cstdio>
void CALLBACK HandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
IAccessible* pAcc = NULL;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);
if ((hr == S_OK) && (pAcc != NULL))
{
VARIANT varResult = {};
pAcc->get_accRole(varChild, &varResult);
if (varResult.lVal == ROLE_SYSTEM_PAGETAB || varResult.lVal == NULL)
{
BSTR bstrName;
pAcc->get_accName(varChild, &bstrName);
if (event == EVENT_OBJECT_CREATE)
{
printf("Create: ");
}
else if (event == EVENT_OBJECT_DESTROY)
{
printf("Destroy: ");
}
printf("%S\n", bstrName);
SysFreeString(bstrName);
}
pAcc->Release();
}
}
unsigned __stdcall hook(void* args)
{
HWND hWindow = FindWindow(TEXT("Notepad++"), NULL);
if (hWindow != NULL)
{
DWORD ProcessId, ThreadId;
ThreadId = GetWindowThreadProcessId(hWindow, &ProcessId);
CoInitialize(NULL);
HWINEVENTHOOK hHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_DESTROY, NULL, HandleWinEvent, ProcessId, ThreadId, WINEVENT_OUTOFCONTEXT);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0);
UnhookWinEvent(hHook);
CoUninitialize();
}
return 0;
}
int main()
{
unsigned ThreadId;
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, hook, NULL, 0, &ThreadId);
for (;; Sleep(100))
{
if (GetAsyncKeyState(VK_END) & 0x8000) // Press End to exit
{
PostThreadMessage(ThreadId, WM_QUIT, 0, 0);
WaitForSingleObject(hThread, 3000);
break;
}
}
CloseHandle(hThread);
return 0;
}
|