title: Trouble converting function from vmm.dll (C) to vmmsharp.dll (C#). (DMA) (PCILEECH) (C#)
body: <p>I am attempting to bring over a function from <a href="https://github.com/ufrisk/MemProcFS/blob/master/vmm/vmmdll.h" rel="nofollow noreferrer">vmmdll.h</a> to <a href="https://github.com/ufrisk/MemProcFS/blob/master/vmmsharp/vmmsharp.cs" rel="nofollow noreferrer">vmmsharp.cs</a>. In this case, I am trying to bring over Map_GetPool.</p>
  <p>You can see here my implementation to C#:</p>
  <pre><code>//Please view the images:
  </code></pre>
  <p><a href="https://i.stack.imgur.com/pjN2r.png" rel="nofollow noreferrer">Bringing function over</a></p>
  <pre><code>    internal static uint VMMDLL_POOLMAP_FLAG_ALL = 0;
      internal static uint VMMDLL_POOLMAP_FLAG_BIG = 1;
  
      [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
      internal unsafe struct VMMDLL_MAP_POOLENTRYTAG
      {
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] internal char[] szTag;
          //szTag struct 
          /*internal uint dwTag;
          internal uint _Filler;
          internal uint cEntry;
          internal uint iTag2Map;*/
      }
  
      [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
      internal unsafe struct VMMDLL_MAP_POOLENTRY
      {
          internal ulong va;
  
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] internal char[] szTag;
          //szTag struct 
          /*internal uint dwTag;
          internal byte _ReservedZero;
          internal byte fAlloc;
          internal byte tpPool;
          internal byte tpSS;*/
  
          internal uint cb;
          internal uint _Filler;
      }
  
      [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
      internal unsafe struct VMMDLL_MAP_POOL
      {
          internal uint dwVersion;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] internal uint[] _Reserved1;
          internal uint cbTotal;
          internal uint* piTag2Map;
          internal VMMDLL_MAP_POOLENTRYTAG* pTag;
          internal uint cTag;
          internal uint cMap;
          internal VMMDLL_MAP_POOLENTRY[] pMap;
      }
  
      [DllImport(&quot;vmm.dll&quot;, EntryPoint = &quot;VMMDLL_Map_GetPoolEx&quot;)]
      internal static extern unsafe bool VMMDLL_Map_GetPoolEx(
          byte* pNetMap,
          ref uint pcbNetMap);
  </code></pre>
  <p><a href="https://i.stack.imgur.com/crIxq.png" rel="nofollow noreferrer">Implementing it into c#</a></p>
  <pre><code>//My map pool
      public unsafe struct MAP_POOL
      {
          public uint dwVersion;
          public uint[] _Reserved1;
          //public unsafe fixed uint _Reserved1[6];
          public uint cbTotal;
          public uint[] piTag2Map;
          public MAP_POOLENTRYTAG* pTag;
          public uint cTag;
          public uint cMap;
          public MAP_POOLENTRY[] pMap;
      }
  
  
      //szTag[5] -&gt; POOLENTRY 4th tpPool MAP_POOL_TYPE 5th tpSS MAP_POOL_TYPE_SUBSEGMENT
      public enum MAP_POOL_TYPE
      {
          MAP_POOL_TYPE_Unknown = 0,
          MAP_POOL_TYPE_NonPagedPool = 1,
          MAP_POOL_TYPE_NonPagedPoolNx = 2,
          MAP_POOL_TYPE_PagedPool = 3
      }
  
      public enum MAP_POOL_TYPE_SUBSEGMENT
      {
          VMM_MAP_POOL_TYPE_SUBSEGMENT_UNKNOWN = 0,
          VMM_MAP_POOL_TYPE_SUBSEGMENT_NA = 1,
          VMM_MAP_POOL_TYPE_SUBSEGMENT_BIG = 2,
          VMM_MAP_POOL_TYPE_SUBSEGMENT_LARGE = 3,
          VMM_MAP_POOL_TYPE_SUBSEGMENT_VS = 4,
          VMM_MAP_POOL_TYPE_SUBSEGMENT_LFH = 5
      }
  
      public unsafe struct MAP_POOLENTRYTAG
      {
          //union
          public char[] szTag;
          /*
           * struct {
          DWORD dwTag;
          DWORD _Filler;
          DWORD cEntry;
          DWORD iTag2Map;
           };
           */
      }
  
      public unsafe struct MAP_POOLENTRY
      {
          public ulong va;
          public char[] szTag;
          //public unsafe fixed char szTag[5];            
          public uint cb;
          public uint _Filler;
  
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/YaEeN.png" rel="nofollow noreferrer">Function</a></p>
  <pre><code>        public static unsafe MAP_POOLENTRY[] Map_GetPoolEx()
      {
          bool result;
          uint cb = 0;
          int cbMAP = System.Runtime.InteropServices.Marshal.SizeOf(typeof(vmmi.VMMDLL_MAP_POOL));
          int cbENTRY = System.Runtime.InteropServices.Marshal.SizeOf(typeof(vmmi.VMMDLL_MAP_POOLENTRY));
          result = vmmi.VMMDLL_Map_GetPoolEx(null, ref cb);
          if (!result || (cb == 0)) { return new MAP_POOLENTRY[0]; }
          fixed (byte* pb = new byte[cb])
          {
              result = vmmi.VMMDLL_Map_GetPoolEx(pb, ref cb);
              if (!result) { return new MAP_POOLENTRY[0]; }
              vmmi.VMMDLL_MAP_POOL pm = Marshal.PtrToStructure&lt;vmmi.VMMDLL_MAP_POOL&gt;((System.IntPtr)pb);
              if (pm.dwVersion != vmmi.VMMDLL_MAP_PHYSMEM_VERSION) { return new MAP_POOLENTRY[0]; }
              MAP_POOLENTRY[] m = new MAP_POOLENTRY[pm.cMap];
              for (int i = 0; i &lt; pm.cMap; i++)
              {
                  vmmi.VMMDLL_MAP_POOLENTRY n = Marshal.PtrToStructure&lt;vmmi.VMMDLL_MAP_POOLENTRY&gt;((System.IntPtr)(pb + cbMAP + i * cbENTRY));
                  MAP_POOLENTRY e;
                  e.va = n.va; 
                  e.cb = n.cb;
                  e.szTag = n.szTag; 
                  //m[i] = e;
              }
              return m;
          }
  
      }
  </code></pre>
  <p>As you can see, the pointer PVMMDLL -&gt; VMMDLL* throws an error stating it can't do that.
  For the function, for some reason, <strong>e</strong> is undefined even though it is indeed defined. Also, is my overall setup correct?</p>
  <p>Thanks, I appreciate any help.</p>
  <p>Here are the links again to the implementations if needed (This doesn't have my implementation in it, but it has other implementations for similar functions):</p>
  <pre><code>vmmdll: https://github.com/ufrisk/MemProcFS/blob/master/vmm/vmmdll.h#L1506
  
  vmmsharp: https://github.com/ufrisk/MemProcFS/blob/master/vmmsharp/vmmsharp.cs
  </code></pre>