Click here to Skip to main content
13,553,300 members
Click here to Skip to main content

Stats

161.3K views
8.2K downloads
128 bookmarked
Posted 6 Apr 2003
Licenced

InterSpy - An integrated Windows message trace and filter utility

, 15 Apr 2003
A utility providing enhanced Windows message debugging.
// InterSpy.cpp: implementation of the CInterSpy class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "InterSpy.h"
#include "ISMsgManager.h"

#include "afxpriv.h"
#include <atlbase.h>

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

//////////////////////////////////////////////////////////////////////

CInterSpy::CInterSpy() : m_dwFlags(0), m_hwndOutput(NULL)
{
}

CInterSpy::~CInterSpy()
{
}

BOOL CInterSpy::Initialize(DWORD dwFlags)
{
	if (GetInstance().InitHooks(HM_CALLWNDPROC | HM_GETMESSAGE))
	{
		GetInstance().SetFlags(dwFlags);

		return TRUE;
	}

	return FALSE;
}

void CInterSpy::IncludeMsg(UINT uMsg)
{
	GetInstance().CInterSpy::IncludeMsg(uMsg, TRUE);
}

void CInterSpy::ExcludeMsg(UINT uMsg)
{
	GetInstance().CInterSpy::IncludeMsg(uMsg, FALSE);
}

void CInterSpy::OnCallWndProc(const MSG& msg)
{
	ProcessMessage(msg);
}

void CInterSpy::OnGetMessage(const MSG& msg)
{
	ProcessMessage(msg);
}

void CInterSpy::SetFlags(DWORD dwFlags) 
{
	m_dwFlags = dwFlags;

	if (m_dwFlags & IS_NOKICKIDLE)
		IncludeMsg(WM_KICKIDLE, FALSE);

	HWND hwndOutput = GetOutputWnd(TRUE);

	if ((m_dwFlags & IS_RESETONSTART) && hwndOutput)
	{
		// build copy data msg
		IS_COPYDATA data;
		data.uCmd = IS_CMD_RESET;
		
		COPYDATASTRUCT cds = { 1, sizeof(data), &data};
		
		::SendMessage(hwndOutput, WM_COPYDATA, 0, (LPARAM)&cds);
	}
}

void CInterSpy::IncludeMsg(UINT uMsg, BOOL bInclude)
{
	if (bInclude)
		m_mapExclusions.RemoveKey(uMsg);
	else
		m_mapExclusions[uMsg] = 1;
}

HWND CInterSpy::GetOutputWnd(BOOL bStartup)
{
	static BOOL bReentrancy = FALSE;
	if (bReentrancy)
		return NULL;

	bReentrancy = TRUE;

	if (!m_hwndOutput || !::IsWindow(m_hwndOutput))
		m_hwndOutput = NULL;

	if (!m_hwndOutput)
		m_hwndOutput = ::FindWindow("InterspyWnd", NULL);

	if (bStartup && !m_hwndOutput && (m_dwFlags & IS_AUTOSTARTOUTPUT))
	{
		// lookup cached path in registry
		if (m_sOutputWndPath.IsEmpty())
		{
			CRegKey reg;
			
			if (reg.Open(HKEY_LOCAL_MACHINE, "Software\\AbstractSpoon\\InterSpy") == ERROR_SUCCESS)
			{
				char szPath[MAX_PATH + 1];
				DWORD dwLen = MAX_PATH;
				
				if (reg.QueryValue(szPath, "InterSpyWndPath", &dwLen) == ERROR_SUCCESS)
					m_sOutputWndPath = szPath;
			}
		}
		
		BOOL bCancelled = FALSE;

		do
		{
			if (m_sOutputWndPath.IsEmpty())
			{
				if (IDCANCEL == MessageBox(NULL, "Interspy could not determine the location of the InterSpy output window.\n\nClick 'OK' to browse to the location of the InterSpy output window, \nor 'Cancel' if you which to start the output window manually", 
					"InterSpy", MB_OKCANCEL))
				{
					bCancelled = TRUE;
				}
				else
				{
					CFileDialog dialog(TRUE, NULL, m_sOutputWndPath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
						"InterSpy output windows|InterSpyWnd.exe||");
					
					if (dialog.DoModal() == IDOK)
					{
						m_sOutputWndPath = dialog.GetPathName();
					}
					else
						bCancelled = TRUE;
				}
			}

			if (!bCancelled && !m_sOutputWndPath.IsEmpty())
			{
				// try to run the exe
				TRACE ("running '%s'\n", m_sOutputWndPath);
				if ((int)ShellExecute(NULL, NULL, m_sOutputWndPath, NULL, NULL, SW_SHOW) > 32)
				{
					int nCount = 10;
					
					while (nCount-- && !m_hwndOutput)
					{
						m_hwndOutput = ::FindWindow("InterspyWnd", NULL);
						Sleep(100);
					}
				}
			}
		}
		while (!m_hwndOutput && !bCancelled);

		if (bCancelled)
			m_dwFlags &= ~IS_AUTOSTARTOUTPUT;
		else
		{
			// write to registry
			CRegKey reg;
			
			reg.SetValue(HKEY_LOCAL_MACHINE, "Software\\AbstractSpoon\\InterSpy", m_sOutputWndPath, "InterSpyWndPath");
		}
	}

	bReentrancy = FALSE;

	return m_hwndOutput;
}

void CInterSpy::ProcessMessage(const MSG& msg)
{
	// check exclusions
	static UINT uTemp;

	if (m_mapExclusions.Lookup(msg.message, uTemp))
		return;

	// check duplicates
	if (m_dwFlags & IS_NODUPLICATES)
	{
		UINT uLastMsg;

		if (m_mapLastMsgs.Lookup(msg.hwnd, uLastMsg) && uLastMsg == msg.message)
			return;
	}

	m_mapLastMsgs[msg.hwnd] = msg.message;

	HWND hwndOutput = GetOutputWnd();

	if (!hwndOutput)
		return;

	// else
	CString sMsg;
	CStringArray aParams;

	if (CISMsgManager::HandleMsg(&msg, sMsg, &aParams))
	{
		// build copy data msg
		IS_COPYDATA data;

		data.uCmd = IS_CMD_ADDMSG;
		data.wParam = (WPARAM)msg.hwnd;
		data.lParam = msg.message;

		int nParams = aParams.GetSize();

		if (nParams)
		{
			sMsg += '|';

			for (int nParam = 0; nParam < nParams; nParam++)
			{
				sMsg += aParams[nParam];

				if (nParam < nParams - 1)
					sMsg += '|'; // delimeter
			}
		}

		lstrcpy(data.sMsg, sMsg);

		COPYDATASTRUCT cds = { 1, sizeof(data), &data};

		::SendMessage(hwndOutput, WM_COPYDATA, 0, (LPARAM)&cds);
	}
}

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

Share

About the Author

.dan.g.
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 16 Apr 2003
Article Copyright 2003 by .dan.g.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid