Click here to Skip to main content
15,897,187 members
Articles / Desktop Programming / Win32

Writing a basic Windows debugger

Rate me:
Please Sign up or sign in to vote.
4.92/5 (81 votes)
24 Jan 2011CPOL14 min read 211.1K   12.6K   218  
Learn how you can write your own Windows debugger.
// DebugMeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DebugMe.h"
#include "DebugMeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDebugMeDlg dialog




CDebugMeDlg::CDebugMeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDebugMeDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDebugMeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CDebugMeDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BREAK, &CDebugMeDlg::OnBnClickedBreak)
	ON_BN_CLICKED(IDC_OUTPUT, &CDebugMeDlg::OnBnClickedOutput)
	ON_BN_CLICKED(IDC_RAISE_AV, &CDebugMeDlg::OnBnClickedRaiseAV)
END_MESSAGE_MAP()


// CDebugMeDlg message handlers

BOOL CDebugMeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDebugMeDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDebugMeDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CDebugMeDlg::OnBnClickedBreak()
{
	if ( !IsDebuggerPresent() )
		AfxMessageBox(L"No debugger is attached currently.");
	else
		DebugBreak();

}

void CDebugMeDlg::OnBnClickedOutput()
{
	CString strOutput;
	GetDlgItemText(IDC_OUTPUTDEBUG, strOutput);
	OutputDebugString(strOutput);
}

void CDebugMeDlg::OnBnClickedRaiseAV()
{
	// Just raise AV
	int* pInvalidPtr = NULL;

	*pInvalidPtr = 0xDEAD;
}

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
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

Comments and Discussions