开发者

VC++ : how to hook/unhook a particular single thread

We are hooking TextOut(),ExtTextOut() and DrawText() methods GLOBALLY .

i.e. hhook = SetWindowsHookEx(WH_CBT, function_address, module_handle, 0);

But we want to hook/unhook only a particular exe. Can someone tell us how to check all the existing threads and get the required exe and hook/unhook only that.

Please provide h开发者_如何学Goelp.

Thank you


You can enumerate the processes using the PSAPI specifically EnumProcesses

You'll need to #include "psapi.h" from the SDK and add PSAPI.lib to the linker inputs.

Ex:

DWORD aiPID[1000], iCb=1000;
DWORD iCbneeded = 0;

if (!EnumProcesses(aiPID, iCb, &iCbneeded)) return(E_FAIL);

int iNumProc=iCbneeded/sizeof(DWORD);
for(int i=0; i < iNumProc; i++)
{
    // First, get a handle to the process
    HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, aiPID[i]);
    if (!hProc) continue;

    TCHAR szName[MAX_PATH] = _T("<unknown>");

    HINSTANCE hMod = NULL;
    if (EnumProcessModules(hProc, &hMod, sizeof(hMod), &iCbneeded)) 
    {
        GetModuleFileNameEx(hProc, hMod, (LPTSTR)szName, MAX_PATH);
    }

    CloseHandle(hProc);
}

Edit: Sorry - That only gives you the lists of processes... to get the threads for each see ListProcessThreads passing in the PID for each enumerated process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜