Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / C++

PasswordSpy - Retrieving lost passwords using Windows hooks

Rate me:
Please Sign up or sign in to vote.
4.84/5 (66 votes)
16 Dec 20037 min read 649.8K   14.7K   252  
A practical application of setting Windows hooks
#include "stdafx.h"
#include "PwdSpy.h"
#include "PopupTipWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define BORDER_X	5
#define BORDER_Y	5

//***********************************************
CPopupTipWnd::CPopupTipWnd()
{
	m_pParentWnd = NULL;
}

//***********************************************
CPopupTipWnd::~CPopupTipWnd()
{
}

BEGIN_MESSAGE_MAP(CPopupTipWnd, CWnd)
	//{{AFX_MSG_MAP(CPopupTipWnd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//***********************************************
BOOL CPopupTipWnd::Create(CWnd *pParentWnd)
{
	_ASSERTE(pParentWnd);

	m_pParentWnd = pParentWnd;

	DWORD dwStyle = WS_BORDER | WS_POPUP;
	DWORD dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;

	CString strWndClass = AfxRegisterWndClass(CS_SAVEBITS, 0, (HBRUSH)(COLOR_INFOBK + 1));

	BOOL bCreated = CreateEx(dwExStyle,
		strWndClass,
		NULL,
		dwStyle,
		0, 0, 0, 0,
		NULL, NULL, NULL);

	return bCreated;
}

//***********************************************
void CPopupTipWnd::ShowPopupWindow(CString strText, CPoint point, CRect rect)
{
	// If there is no password, instead hide the window and be done with it
	if(strText.IsEmpty())
	{
		HidePopupWindow();
		return;
	}

	CClientDC dc(this);

	// Use same font as parent window
	CFont *pOldFont = dc.SelectObject(m_pParentWnd->GetFont());

	// Calculate the window size.
	CSize sizeText = dc.GetTextExtent(strText);
	CSize sizeWindow;
	sizeWindow.cx = sizeText.cx + 2 * BORDER_X;
	sizeWindow.cy = sizeText.cy + 2 * BORDER_Y;

	// Draw information in window
	dc.SetBkMode(TRANSPARENT);
	dc.DrawText(strText, CRect(0, 0, sizeWindow.cx, sizeWindow.cy), DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	dc.SelectObject(pOldFont);

	// Calculate window rectangle position on screen
	CRect rectWindow;
	rectWindow.left = rect.left;
	rectWindow.right = rectWindow.left + sizeWindow.cx;
	rectWindow.top = rect.top - (sizeWindow.cy + 20);
	if(rectWindow.top <= 0)
		rectWindow.top = rect.bottom + 20;
	rectWindow.bottom = rectWindow.top + sizeWindow.cy;

	// Display window
	SetWindowPos(&wndTop,
		rectWindow.left,
		rectWindow.top,
		rectWindow.Width(),
		rectWindow.Height(),
		SWP_SHOWWINDOW | SWP_NOACTIVATE);
}

//***********************************************
void CPopupTipWnd::HidePopupWindow(void)
{
	ShowWindow(SW_HIDE);
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions