Home > Software engineering >  A great god, please help me to modify the code to program monitoring process or specified window
A great god, please help me to modify the code to program monitoring process or specified window

Time:10-12


# include "stdafx. H"
# include & lt; Windows. H>
# include & lt; iostream>
HHOOK g_MsgHook;
//LRESULT CALLBACK MsgHookproc (int nCode, WPARAM WPARAM, LPARAM LPARAM)//CALLBACK function
//{
//MessageBoxW (nullptr, L "to" robbery, L "is from the to remind", MB_OK);
//return CallNextHookEx (g_MsgHook, nCode wParam, lPARAM);
//};

Typedef bool FUN (*) ();/* define FUN as Boolean function pointer */
Int _tmain (int arg c, _TCHAR * argv [])
{
//g_MsgHook=SetWindowsHookEx (WH_GETMESSAGE MsgHookproc, nullptr, NULL);
HINSTANCE hDll=LoadLibrary (L "DLL, the DLL");/* to apply for a DLL loaded handle */
If (hDll)
{
//bool (__cdecl *) (void)
Bool fun (*) ()=(bool (*) ()) GetProcAddress call (hDll, "StartHook");/* to retrieve the output in library (DLL) function address, */
//FUN FUN=(FUN) GetProcAddress call (hDll, "StartHook");/* */
{
If (fun)
{
Fun ();
}
}
}
Char szInput [MAXBYTE]={0}; Definition/* 255 */string array
While (true)
{
STD: : cin & gt;> SzInput;
If (0==STRCMP (szInput, "yes"))
{
break;
}
}
return 0;
}




This is the DLL:
//dllmain. CPP: defines the entry point for the DLL application,
# include "stdafx. H"
# include & lt; iostream>
HINSTANCE g_h;
//HINSTANCE g_h=(HINSTANCE) : : FindWindow (L "TXGuiFoundation," L "QQ");
HHOOK g_MsgHook;
LRESULT a CALLBACK MsgHookproc (int nCode, WPARAM WPARAM, LPARAM LPARAM)//CALLBACK function
{
If (HC_ACTION==nCode)/* HC_ACTION keyboard or mouse information */
{
/* PMSG message structure, typede after use can declare */
PMSG PMSG=(PMSG) lPARAM;
The switch (PMSG - & gt; Message)
{
Case WM_CHAR:/* * character messages sent/
{

//which characters it by pressing the
Char szkey=PMSG - & gt; WParam.
Char szWindowsName [MAXBYTE]={0};
Char szBuffer [MAXBYTE]={0};
GetWindowTextA (PMSG - & gt; HWND szWindowsName, MAXBYTE);
Sprintf_s (szBuffer MAXBYTE, "access to the message window % s % c", szWindowsName, szkey);
MessageBoxA (nullptr, szBuffer szWindowsName, MB_OK);
}
break;
Default:
break;
}

}
//MessageBoxW (nullptr, L "to" robbery, L "is from the to remind", MB_OK);
Return CallNextHookEx (g_MsgHook, nCode wParam, lPARAM);
};

Extern "C"

__declspec (dllexport) bool StartHook ()/* */export function
{

G_MsgHook=SetWindowsHookEx (WH_GETMESSAGE MsgHookproc, g_h, NULL);
If (g_h)
return true;
return false;

}

Bool Unhook ()
{
Return UnhookWindowsHookEx (g_MsgHook);
}



BOOL APIENTRY DllMain (HMODULE HMODULE,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
The switch (ul_reason_for_call)
{
Case DLL_PROCESS_ATTACH:
G_h=hModule;
Case DLL_THREAD_ATTACH:
Case DLL_THREAD_DETACH:
Case DLL_PROCESS_DETACH:
Unhook ();
break;
}
return TRUE;
}

CodePudding user response:

When installing a hook the specified callback function MsgHookproc initially: join GetProcessName () to obtain process name, such a judgment about whether you want to monitor process, so is injected,
  • Related