Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / ATL

Function Call Tracing in JScript

Rate me:
Please Sign up or sign in to vote.
4.58/5 (18 votes)
3 Jul 2007CPOL15 min read 53.9K   6.4K   49  
Comprehensive JScript function call tracing without code modification.
#ifndef __SITEIMPL_H__
#define __SITEIMPL_H__

#include <activscp.h>
#include "activdbg.h"

class ATL_NO_VTABLE IActiveScriptSiteImpl : public IActiveScriptSite
{
	STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject) = 0;
	_ATL_DEBUG_ADDREF_RELEASE_IMPL(IActiveScriptSiteImpl)

	STDMETHOD(GetItemInfo)(LPCOLESTR pstrName, DWORD dwReturnMask,
		IUnknown **ppiunkItem, ITypeInfo **ppti)
	{ return E_FAIL; }
 
	STDMETHOD(OnScriptError)(IActiveScriptError *pscripterror)
	{
		DWORD dwCookie;
		LONG nChar;
		ULONG nLine;
		BSTR bstr = 0;
		EXCEPINFO ei; ZeroMemory(&ei, sizeof(ei));
		pscripterror->GetSourcePosition(&dwCookie, &nLine, &nChar);
		pscripterror->GetSourceLineText(&bstr);
		pscripterror->GetExceptionInfo(&ei);
		nLine += 1;										// Line numbers returned are zero based

		OLECHAR wszOutput[1024];
		swprintf(wszOutput, OLESTR("%s\n[Line: %d] %s\n%s"),
			ei.bstrSource ? ei.bstrSource : L"Unknown Source", nLine, ei.bstrDescription,
				  bstr ? bstr : OLESTR(""));

		SysFreeString(bstr);
		SysFreeString(ei.bstrSource);
		SysFreeString(ei.bstrDescription);
		SysFreeString(ei.bstrHelpFile);
	
		return S_OK;
	}

	STDMETHOD(GetLCID)(LCID *plcid)
	{ *plcid = 9; return S_OK; }

	STDMETHOD(GetDocVersionString)(BSTR *pbstrVersion) 
	{ *pbstrVersion = SysAllocString(L""); return S_OK; }

	STDMETHOD(OnScriptTerminate)(const VARIANT *pvr, const EXCEPINFO *pei)
	{ return S_OK; }

	STDMETHOD(OnStateChange)(SCRIPTSTATE ssScriptState)
	{ return S_OK; }

	STDMETHOD(OnEnterScript)(void)
	{ return S_OK; }

	STDMETHODIMP OnLeaveScript(void) 
	{ return S_OK; }
};

class IActiveScriptSiteWindowImpl : public IActiveScriptSiteWindow
{
	STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject) = 0;
	_ATL_DEBUG_ADDREF_RELEASE_IMPL(IActiveScriptSiteImpl)

	STDMETHODIMP GetWindow(HWND *phwnd)
	{ *phwnd = GetDesktopWindow(); return S_OK; }

	STDMETHODIMP EnableModeless(BOOL)
	{ return S_OK; }
};

class IActiveScriptSiteDebugImpl : public IActiveScriptSiteDebug
{
	STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject) = 0;
	_ATL_DEBUG_ADDREF_RELEASE_IMPL(IActiveScriptSiteDebugImpl)

	STDMETHODIMP GetDocumentContextFromPosition( 
        /* [in] */ DWORD dwSourceContext,
        /* [in] */ ULONG uCharacterOffset,
        /* [in] */ ULONG uNumChars,
        /* [out] */ IDebugDocumentContext __RPC_FAR *__RPC_FAR *ppsc)
	{
		return S_OK;
	}
    
    STDMETHODIMP GetApplication(/* [out] */ IDebugApplication __RPC_FAR *__RPC_FAR *ppda)
	{
		ATLASSERT(FALSE);
		return E_NOTIMPL;
	}
    
    STDMETHODIMP GetRootApplicationNode(/* [out] */ IDebugApplicationNode __RPC_FAR *__RPC_FAR *ppdanRoot)
	{
		*ppdanRoot = NULL;		// Top-Level
		return S_OK;
	}
    
    STDMETHODIMP OnScriptErrorDebug( 
        /* [in] */ IActiveScriptErrorDebug __RPC_FAR *pErrorDebug,
        /* [out] */ BOOL __RPC_FAR *pfEnterDebugger,
        /* [out] */ BOOL __RPC_FAR *pfCallOnScriptErrorWhenContinuing)
	{ 
		ATLASSERT(FALSE);
		return E_NOTIMPL;
	}
};

#endif //__SITEIMPL_H__

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
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions