title: How to read heap using PYKD
body: <p>In <a href="https://stackoverflow.com/questions/47651846/how-to-avoid-the-dbgcommand-command-being-written-to-the-logfile">this question</a>, I wanted to replace <code>DbgCommand("dt ...")</code> by an API call, and the <code>PYKD</code> command <code>typedVar()</code> came to the rescue.</p>
  
  <p>As a result, my heap_stat script (extended with <code>m_nSize</code> and <code>m_nCount</code> information) is running three times faster now.</p>
  
  <p>For your information, I've done this replacement for calculating the amount of members in an STL collection:</p>
  
  <pre><code>Replace: collection_Size = dbgCommand(("dt 0x" + pointer_format + " %s m_nSize") % (ptr,type_name)).split(' : ').[-1].split('\n')[0]
  By:      collection_Size = typedVar(type_name, ptr).m_nSize
  </code></pre>
  
  <p>As a result of this success, I'd like to replace other <code>DbgCommand</code> requests by API calls.</p>
  
  <p>For the case of <code>dbgCommand('!heap -h 0')</code>, this seems not to be that simple (some examples):</p>
  
  <pre><code>&gt;&gt;&gt; for t in targetHeapIterator():
  ...   print t
  ... 
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  RuntimeError: This class cannot be instantiated from Python
  
  &gt;&gt;&gt; for t in targetHeap().entries:
  ...   print t
  ... 
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  RuntimeError: This class cannot be instantiated from Python
  
  &gt;&gt;&gt; for t in targetProcess().getManagedHeap().entries:
  ...   print t
  ... 
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  TypeError: 'instancemethod' object is not iterable
  </code></pre>
  
  <p>How can I iterate over the heap of my process (replacing <code>!heap -h 0</code>)?</p>
  
  <p>P.S. Even if <code>targetHeap()</code> can't be used as a replacement for <code>!heap -h 0</code>, I'd still like to know how to use it, for investigation purposes.</p>