<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual C++ Tips &#187; virtual table under the hood</title>
	<atom:link href="http://weseetips.com/tag/virtual-table-under-the-hood/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to Parse Virtual Table?</title>
		<link>http://weseetips.com/2009/05/07/how-to-parse-virtual-table/</link>
		<comments>http://weseetips.com/2009/05/07/how-to-parse-virtual-table/#comments</comments>
		<pubDate>Thu, 07 May 2009 16:47:06 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++ virtual functions]]></category>
		<category><![CDATA[C++ virtual table]]></category>
		<category><![CDATA[parse virtual table]]></category>
		<category><![CDATA[virtual function]]></category>
		<category><![CDATA[virtual table]]></category>
		<category><![CDATA[virtual table parsing]]></category>
		<category><![CDATA[virtual table under the hood]]></category>
		<category><![CDATA[vtable]]></category>
		<category><![CDATA[vtable under the hood]]></category>
		<category><![CDATA[vtbl]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=1146</guid>
		<description><![CDATA[Virtual Table is one of the most fascinating stuff for C++ programmer. Well, did you ever peek into virtual table, which is the real engine of virtual functions? The first 4 bytes of an objects points to another pointer which points to virtual table. Casting it to DWORD*, we can parse all virtual functions. Once [...]]]></description>
			<content:encoded><![CDATA[<p>Virtual Table is one of the most fascinating stuff for C++ programmer. Well, did you ever  peek into virtual table, which is the <span style="color: #0000ff;"><strong>real engine </strong></span>of virtual functions?</p>
<p><img class="alignnone size-full wp-image-1147" title="virtualfunction" src="http://weseetips.wordpress.com/files/2009/05/virtualfunction.jpg" alt="virtualfunction" width="510" height="351" /></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
The <span style="color: #0000ff;">first 4 bytes of an objects points to another pointer which points to virtual table.</span> Casting it to DWORD*, we can parse all virtual functions. Once you get function address, you can get the function name by calling &#8211; <span style="color: #0000ff;">SymFromAddr()</span>. Have a look at code snippet.</p>
<p><img class="alignnone size-full wp-image-1148" title="virtualfunction2" src="http://weseetips.wordpress.com/files/2009/05/virtualfunction2.jpg" alt="virtualfunction2" width="493" height="290" /></p>
<pre>#include &lt;ImageHlp.h&gt;
...
// Get list of virtual functions.
void CRabbitDlg::ParseVtable()
{
    // Initialize symbols.
    InitializeSymbols();

    // We are going to parse vtable of CWinApp object.
    DWORD* pBase = (DWORD*)(AfxGetApp());
    DWORD* pVptr = (DWORD*)*pBase;

    // Iterate through VirtualTable.
    DWORD Index = 0;
    DWORD FnAddr = pVptr[Index];
    while( FnAddr )
    {
        // Translate FunctionAddress to FunctionName.
        CString FunctionName;
        GetSymbolNameFromAddr( FnAddr, FunctionName );

        // Format and add to list.
        CString Final;
        Final.Format( _T("%0x - %s"), FnAddr, FunctionName.operator LPCTSTR());
        m_List.AddString( Final );

        // Next function pointer.
        FnAddr = pVptr[++Index];
    }
}

// Initialize Symbol engine.
void CRabbitDlg::InitializeSymbols()
{
    DWORD Options = SymGetOptions();
    Options |= SYMOPT_DEBUG;
    Options |= SYMOPT_UNDNAME; 

    ::SymSetOptions( Options ); 

    // Initialize symbols.
    ::SymInitialize ( GetCurrentProcess(),
                      NULL,
                      TRUE );
}

// Get symbol name from address.
void CRabbitDlg::GetSymbolNameFromAddr( DWORD SymbolAddress, CString&amp; csSymbolName )
{
    DWORD64 Displacement = 0;
    SYMBOL_INFO_PACKAGE SymbolInfo = {0};
    SymbolInfo.si.SizeOfStruct  = sizeof( SYMBOL_INFO );
    SymbolInfo.si.MaxNameLen = sizeof(SymbolInfo.name);

    // Get symbol from address.
    ::SymFromAddr( GetCurrentProcess(),
                   SymbolAddress,
                   &amp;Displacement,
                   &amp;SymbolInfo.si );

    csSymbolName = SymbolInfo.si.Name;
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Don&#8217;t forget to include <span style="color: #0000ff;">ImageHlp.lib </span>to project settings.</p>
<p><img class="alignnone size-medium wp-image-53" title="intermediateseries" src="http://weseetips.wordpress.com/files/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audiance &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2009/05/07/how-to-parse-virtual-table/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

