思路:通过反汇编确定记事本的窗口过程地址,把它替换为我们自己的窗口过程地址,在自己窗口过程中先判断是否是自定义的消息,

是则执行相应的操作,不是则把流程转到记事本原来的窗口过程中进行处理。

添加的功能:转换大小写,加解密文本,统计字数。

源代码编译成DLL

// ExNotepad.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "resource.h"
#include <tchar.h>

#define	IDR_UPPER	(128)/*转换为大写*/
#define	IDR_LOWER	(129)/*转换为小写*/
#define IDR_ENCRYPT	(130)/*加解密*/
#define IDR_COUNT	(131)/*计算字数*/

HINSTANCE	hDll;
HWND	g_hEdit	=	NULL;//编辑框句柄
DWORD	ExWndProc(CONST	DWORD Reserved, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
VOID	WINAPI	TO_UPPER(HWND hWnd);//转换到大写
VOID	WINAPI	TO_LOWER(HWND hWnd);//转换到小写
VOID	WINAPI	EnCrypt(HWND hWnd);//简单加密
BOOL	WINAPI	EnCryptProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
VOID	WINAPI	OnEnCryptCommand(HWND hDlg, UINT uID, HWND hCtrl, UINT uNotifyCode);
VOID	WINAPI	Count(HWND hWnd);//计算字数

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	hDll	=	(HINSTANCE)hModule;
    return TRUE;
}

DWORD	ExWndProc(CONST	DWORD Reserved, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
			case IDR_UPPER:
				{
					TO_UPPER(hWnd);
				}
				break;
			case IDR_LOWER:
				{
					TO_LOWER(hWnd);
				}
				break;
			case IDR_ENCRYPT:
				{
					EnCrypt(hWnd);
				}
				break;
			case IDR_COUNT:
				{
					Count(hWnd);
				}
				break;
			}
		
		}
		break;
	}

	return 0;
}

VOID	WINAPI	TO_UPPER(HWND hWnd)
{
	//读取编辑框句柄
	if (g_hEdit = GetDlgItem(hWnd, 0xF))
	{
		INT	len	=	GetWindowTextLength(g_hEdit);
		
		if (len > 0)
		{
			LPTSTR	pstrBuffer	=	new TCHAR[len+2];
			memset(pstrBuffer, 0, sizeof(TCHAR)*(len+2));
			GetWindowText(g_hEdit, pstrBuffer, len+2);
			SendMessage(g_hEdit, EM_SETMODIFY, (WPARAM)TRUE, 0);
			CharUpper(pstrBuffer);
			SetWindowText(g_hEdit, pstrBuffer);
			delete	[]pstrBuffer;
		}
	}
}

VOID	WINAPI	TO_LOWER(HWND hWnd)
{
	//读取编辑框句柄
	if (g_hEdit = GetDlgItem(hWnd, 0xF))
	{
		INT	len	=	GetWindowTextLength(g_hEdit);
		
		if (len > 0)
		{
			LPTSTR	pstrBuffer	=	new TCHAR[len+2];
			memset(pstrBuffer, 0, sizeof(TCHAR)*(len+2));
			GetWindowText(g_hEdit, pstrBuffer, len+2);
			SendMessage(g_hEdit, EM_SETMODIFY, (WPARAM)TRUE, 0);
			CharLower(pstrBuffer);
			SetWindowText(g_hEdit, pstrBuffer);
			delete	[]pstrBuffer;
		}
	}
}

VOID	WINAPI	EnCrypt(HWND hWnd)
{
	if (g_hEdit = GetDlgItem(hWnd, 0xF))
	{
		INT	len	=	GetWindowTextLength(g_hEdit);
		
		if (len > 0)
		{
			DialogBoxParam(hDll, MAKEINTRESOURCE(IDD_DLG_ENCRYPT), hWnd, EnCryptProc, NULL);
		}
	}
}

BOOL	WINAPI	EnCryptProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CLOSE:
		EndDialog(hDlg, 0);
		break;
	case WM_COMMAND:
		{
			OnEnCryptCommand(hDlg, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
		}
		break;
	default:
		return	FALSE;
	}

	return	TRUE;
}

VOID	WINAPI	OnEnCryptCommand(HWND hDlg, UINT uID, HWND hCtrl, UINT uNotifyCode)
{
	switch (uID)
	{
	case IDC_BTN_ENCRYPT://加密
		{
			HWND	hEncryptEdit	=	GetDlgItem(hDlg, IDC_EDT_KEY);
			INT	nKeyLen	=	GetWindowTextLength(hEncryptEdit);
			
			if (nKeyLen > 0)
			{
				LPTSTR	pstrKey	=	new TCHAR[nKeyLen+2];
				memset(pstrKey, 0, nKeyLen+2);
				GetWindowText(hEncryptEdit, pstrKey, nKeyLen+2);
				
				INT	len	=	GetWindowTextLength(g_hEdit);

				LPTSTR	pstrText	=	new TCHAR[len+2];
				memset(pstrText, 0, len+2);
				GetWindowText(g_hEdit, pstrText, len+2);
				SendMessage(g_hEdit, EM_SETMODIFY, (WPARAM)TRUE, 0);
				
				DWORD dwKey	=	0;

				for (INT j = 0; j < nKeyLen; j++)
				{
					dwKey	+=	pstrKey[j];
				}
				
				dwKey	%=	32;

				for (INT i = 0; i < len; i++)
				{
					pstrText[i]	-=	(TCHAR)dwKey;
				}			
				
				SetWindowText(g_hEdit, pstrText);
				delete	[]pstrText;
				delete	[]pstrKey;
			}
		}
		break;
	case IDC_BTN_DECRYPT://解密
		{
			HWND	hEncryptEdit	=	GetDlgItem(hDlg, IDC_EDT_KEY);
			INT	nKeyLen	=	GetWindowTextLength(hEncryptEdit);
			
			if (nKeyLen > 0)
			{
				LPTSTR	pstrKey	=	new TCHAR[nKeyLen+2];
				memset(pstrKey, 0, nKeyLen+2);
				GetWindowText(hEncryptEdit, pstrKey, nKeyLen+2);
				SendMessage(g_hEdit, EM_SETMODIFY, (WPARAM)TRUE, 0);
				
				INT	len	=	GetWindowTextLength(g_hEdit);

				LPTSTR	pstrText	=	new TCHAR[len+2];
				memset(pstrText, 0, len+2);
				GetWindowText(g_hEdit, pstrText, len+2);
				
				DWORD dwKey	=	0;

				for (INT j = 0; j < nKeyLen; j++)
				{
					dwKey	+=	pstrKey[j];
				}
				
				dwKey	%=	32;

				for (INT i = 0; i < len; i++)
				{
					pstrText[i]	+=	(TCHAR)dwKey;
				}			
				
				SetWindowText(g_hEdit, pstrText);
				delete	[]pstrText;
				delete	[]pstrKey;
			}
		}
		break;
	}

	return;
}

VOID	WINAPI	Count(HWND hWnd)
{
	if (g_hEdit = GetDlgItem(hWnd, 0xF))
	{
		INT	len	=	GetWindowTextLength(g_hEdit);
		
		if (len > 0)
		{
			TCHAR	strText[MAX_PATH];
			memset(strText, 0, sizeof(TCHAR)*MAX_PATH);
			wsprintf(strText, _T("当前总字数为: %d"), len);
			MessageBox(hWnd, strText, _T("字数统计"), MB_OK);
		}
	}
}

 

 

posted on 2013-08-14 20:54  5t4rk  阅读(453)  评论(0编辑  收藏  举报