Install a hook for MSN Messager

Source: Internet
Author: User

I recently studied how to use HOOK to intercept messages from other applications, so I wrote a HOOK program to HOOK up the most commonly used communication tool.MSNAlthough there is no practical significance, as a learning study, it can help us understand how to use HOOK to inject your own DLL into an existing program space.

What we need to do is to use our own applications to intercept messages written by others. In fact, this is done between two processes, and the difficulty is here, if the same process is easy to handle, you only need to modify the processing function of the system responding to WINDOWS messages to the function compiled by ourselves, but you cannot do so now, because the two processes have their own process address space, theoretically you cannot directly access the address space of other processes, what should you do? There are still many ways to achieve this. Here we only introduce how to achieve this through HOOK.

To intercept messages of other applications, you must inject your own DLL into others' DLL address space to intercept others' messages. Only by inserting our DLL into the address space of other applications can we operate on other applications. HOOK helps us complete these tasks, you only need to use the HOOK to intercept specified messages and provide necessary processing functions. We will introduce the interception inMSNThe mouse message in the chat dialog box. The HOOK type is WH_MOUSE.

First, we need to create a DLL for HOOK. There is no specific difference between the establishment of this DLL and the establishment of a common DLL, but the methods we provide here are different in writing. Here we use the implicit DLL import method. The Code is as follows:

Header file

#pragma once
#ifndef MSNHOOK_API
#define MSNHOOK_API __declspec(dllimport)
#endif

MSNHOOK_API bool winapi SetMsnHook (DWORD dwThreadId); // install the MSN Hook Function
MSNHOOK_API void WINAPI GetText (int & x, int & y, char ** ptext); // install the MSN Hook Function
MSNHOOK_API hwnd winapi GetMyHwnd (); // install the MSN Hook Function

========================================================== ============

Dll cpp File

#include "stdafx.h"
#include "MSNHook.h"
#include

// The following sentence tells the compiler to put each variable into its own data sharing section.

#pragma data_seg("Shared")
HHOOK g_hhook = NULL;
DWORD g_dwThreadIdMsn = 0;
POINT MouseLoc={0,0};
char text[256]={0};
HWND g_Hwnd = NULL;
#pragma data_seg()

// Instruct the compiler to set the access mode for the shared section to read, write, and share

#pragma comment(linker,"/section:Shared,rws")

HINSTANCE g_hinstDll = NULL;

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hinstDll = (HINSTANCE)hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

LRESULT WINAPI GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam);

BOOL WINAPI SetMsnHook(DWORD dwThreadId)
{
OutputDebugString("SetMsnHook");
BOOL fOK = FALSE;
if(dwThreadId != 0)
{
OutputDebugString("SetMsnHook dwThreadId != 0");
g_dwThreadIdMsn = GetCurrentThreadId();

// Install the WM_MOUSE hook and the processing function GetMsgProc
G_hhook = SetWindowsHookEx (WH_MOUSE, GetMsgProc, g_hinstDll, dwThreadId );
FOK = (g_hhook! = NULL );
If (fOK)
{
FOK = PostThreadMessage (dwThreadId, WM_NULL, 0, 0 );
}
Else
{
FOK = UnhookWindowsHookEx (g_hhook );
G_hhook = NULL;
}
}
Return (fOK );
}

LRESULT WINAPI GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam)
{

Char temp [20];
Sprintf (temp, "% d", nCode );
OutputDebugString ("temp ");
If (nCode = HC_ACTION)
{
MOUSEHOOKSTRUCT * l = (MOUSEHOOKSTRUCT *) lParam;
MouseLoc = l-> pt; // place the cursor

//char text[256] = "";
HWND hWnd = WindowFromPoint(l-> pt);
if(hWnd)
{
//GetWindowText(hWnd,text,256);
SendMessage(hWnd,WM_GETTEXT,256,(LPARAM)(LPCTSTR)text);
// strcpy(text,"123455555");
SendMessage(hWnd,WM_SETTEXT,256,(LPARAM)(LPCTSTR)text);
g_Hwnd = hWnd;
}
//SendMessage(WindowFromPoint(l-> pt),WM_GETTEXT,256,(LPARAM)(LPCTSTR)psw);
}

return(CallNextHookEx(g_hhook,nCode,wParam,lParam));
}

void WINAPI GetText(int &x,int &y,char ** ptext)
{
x = MouseLoc.x;
y = MouseLoc.y;
*ptext = text;
}

HWND WINAPI GetMyHwnd()
{
return g_Hwnd;
}


The above is the DLL code for processing the hook. Below we need a startup part for this DLL to take effect, through this starting part, we can truly inject our hook functions into other functions of the system. Here we use a dialog box program. The program is very simple: one button is used to start the hook, one is used to stop, one is used to refresh the display, and the other is an EDITBOX is used to accept information.

The procedure is as follows:

// Contains the header file exported by the DLL Function
# Include "MSNHook. h"

// Implicit Import

#pragma comment(lib,"MSNHook.lib")

// Declare the import function

_ Declspec (dllimport) bool winapi SetMsnHook (DWORD dwThreadId );
_ Declspec (dllimport) void WINAPI GetText (int & x, int & y, char ** ptext );
_ Declspec (dllimport) hwnd winapi GetMyHwnd (); // install the MSN Hook Function


void CTestMSNHookDlg::OnBnClickedOk()
{

// Through SPY ++, we can see that the window class of the MSN Chat dialog box is IMWindowClass. Through this, we can get the window handle.
CWnd * pMsnWin = FindWindow (TEXT ("IMWindowClass"), NULL );
If (pMsnWin = NULL) return;

// Obtain the corresponding thread ID through the window handle
SetMsnHook (GetWindowThreadProcessId (pMsnWin-> GetSafeHwnd (), NULL ));
MSG msg;
GetMessage (& msg, NULL, 0, 0 );
SetTimer (101,100, NULL );

}

void CTestMSNHookDlg::OnTimer(UINT_PTR nIDEvent)
{

// Refresh the message
Char * pText = NULL;
Int x = 0, y = 0;
GetText (x, y, & pText );
If (x = 0 & y = 0) return;
M_Edit.Format ("% d: % s", x, y, pText );
// M_Edit = pText;
UpdateData (FALSE );

HWND hWnd = GetMyHwnd();
CWnd * pWnd = CWnd::FromHandle(hWnd);
pWnd-> GetWindowText(m_Edit);
CDialog::OnTimer(nIDEvent);
}

void CTestMSNHookDlg::OnBnClickedButton1()
{

// Close
KillTimer (101 );
SetMsnHook (0 );
OnCancel ();
}


Okay, that's all. There is a problem here. I want to get the chat information that the MSN user entered during the chat. Here, the message via WM_GETTEXT is not available. If any friend knows, let me know.

Author's Blog:Http://blog.csdn.net/windcsn/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.