Click here to Skip to main content
15,891,372 members
Articles / Desktop Programming / Win32

Subclassing using DLL Injection

Rate me:
Please Sign up or sign in to vote.
4.95/5 (12 votes)
13 Feb 2013CPOL4 min read 47.9K   1.4K   49  
Subclassing a window using the DLL Injection technique.
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <windowsx.h>
#include <tchar.h>

LONG_PTR oldWindowProc;

//*******************************************************************************************************
void DrawRect(HDC hDC, int x, int y )
{
	::Rectangle(  hDC,x,y,(x+50),(y+50) );
}
//*******************************************************************************************************
void DrawText( HDC hDC, int x, int y )
{
	TCHAR str[] = TEXT("UR HACKED!!!!");
	RECT r;
	r.left = x;
	r.top = y;
	r.right = x+150;
	r.bottom = y+30;
	::DrawText( hDC,str,_tcslen(str),&r,DT_CENTER );
}
//*******************************************************************************************************
LRESULT CALLBACK HackingWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch( message ) 
	{
		case WM_LBUTTONDOWN:
			{
				int x = GET_X_LPARAM(lParam);
				int y = GET_Y_LPARAM(lParam);
				HDC hDC = ::GetDC( hWnd );
				//::Rectangle(  hDC,x,y,(x+50),(y+50) );
				DrawText( hDC,x,y);
				break;
			}
		case WM_RBUTTONDOWN:
			{
					int x = GET_X_LPARAM(lParam);
					int y = GET_Y_LPARAM(lParam);
					HDC hDC = ::GetDC( hWnd );
					::Ellipse( hDC,x,y,(x+50),(y+50));
					break;
			}
		case WM_DESTROY:
			{
				PostQuitMessage(0);
				break;
			}
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
//*******************************************************************************************************
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		{
			//Get the main window of the client
			HWND hwnd = ::FindWindow(NULL,TEXT("Injectee") );

			//If the window found, then it's window proc
			if( hwnd )
			{
				oldWindowProc = ::SetWindowLongPtr( hwnd, GWL_WNDPROC, (LONG_PTR) HackingWndProc );
			}
			break;
		}
	case DLL_THREAD_ATTACH:
		{
			break;
		}
	case DLL_THREAD_DETACH:
		{
			break;
		}
	case DLL_PROCESS_DETACH:
		{
			break;
		}
	}
	return TRUE;
}
//*******************************************************************************************************

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I'm working as Senior software Engineer since 7 years and interested in MFC and COM programming.

Comments and Discussions