title: How to allocate memory for an array of structure in C#
body: <p><strong>Structure 1:</strong></p>
  
  <pre><code>typedef struct _wfs_cdm_cu_info
  {
      USHORT usTellerID;
      USHORT usCount;
      LPWFSCDMCASHUNIT * lppList;
  } WFSCDMCUINFO, * LPWFSCDMCUINFO; 
  </code></pre>
  
  <p><strong>Structure 2:</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>typedef struct _wfs_cdm_cashunit
  {
      USHORT usNumber;
      USHORT usType;
      LPSTR lpszCashUnitName;
      CHAR cUnitID[5];
      CHAR cCurrencyID[3];
      ULONG ulValues;
      ULONG ulInitialCount;
      ULONG ulCount;
      ULONG ulRejectCount;
      ULONG ulMinimum;
      ULONG ulMaximum;
      BOOL bAppLock;
      USHORT usStatus;
      USHORT usNumPhysicalCUs;
      LPWFSCDMPHCU * lppPhysical;
  } WFSCDMCASHUNIT, * LPWFSCDMCASHUNIT;
  </code></pre>
  
  <p><strong>Structure 3:</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>typedef struct _wfs_cdm_physicalcu
  {
      LPSTR lpPhysicalPositionName;
      CHAR cUnitID[5];
      ULONG ulInitialCount;
      ULONG ulCount;
      ULONG ulRejectCount;
      ULONG ulMaximum;
      USHORT usPStatus;
      BOOL bHardwareSensor;
  } WFSCDMPHCU, * LPWFSCDMPHCU; 
  </code></pre>
  
  <hr>
  
  <p><strong>C# structure:-</strong></p>
  
  <pre><code>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)]
  public struct WFSCDMPHCU {     [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
   public string lpPhysicalPositionName;[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=5)]
   public string cUnitID;
   public uint ulInitialCount;
   public uint ulCount;
   public uint ulRejectCount;
   public uint ulMaximum;
   public ushort usPStatus;
   public int bHardwareSensor;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)]
      public struct WFSCDMCASHUNIT {
       public ushort usNumber;
       public ushort usType;         [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
       public string lpszCashUnitName;[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=5)]
       public string cUnitID;        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=3)]
       public string cCurrencyID;
       public uint ulValues;
       public uint ulInitialCount;
       public uint ulCount;
       public uint ulRejectCount;
       public uint ulMinimum;
       public uint ulMaximum;
       public int bAppLock;
       public ushort usStatus;
       public ushort usNumPhysicalCUs;
       public System.IntPtr lppPhysical;
      }
  </code></pre>
  
  <hr>
  
  <pre><code>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)]
  public struct WFSCDMCUINFO {    
   public ushort usTellerID;    
   public ushort usCount;      
   public System.IntPtr lppList;
  }
  </code></pre>
  
  <p><strong>DLLImport</strong></p>
  
  <pre><code>[DllImport(@"Dispenser.dll")]
  public static extern int CDM_SetCashUnit(out WFSCDMCUINFO cuinfo);
  </code></pre>
  
  <p>1)My main problem is How should I marshall or allocate memory for this structure to send data from C# to C++   ,the second and third structure being array of structure???</p>
  
  <p>2)If I use pointer how efficient it would be.</p>
  
  <p>3)If C++/CLI wrapper be used then how I could access it through C#.</p>
  
  <p>It is been a long time I'm working and I m yet to figure out how should I fill the array of structures in C# .</p>
  
  <p>Follwing code is what i try to figure out...</p>
  
  <p><strong>Marshal Code:</strong></p>
  
  <p>Facing an error of "to define an extension non generic static class"</p>
  
  <pre><code> public static IntPtr GetIntPtr(this object obj) {
              try {
                  var handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
                  var thread = new Thread(() =&gt; {
                      Thread.Sleep(20000);
                      handle.Free();
                  });
                  thread.Start();
  
                  return handle.AddrOfPinnedObject();
              } catch (ArgumentException) {
                  var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj));
  
                  Marshal.StructureToPtr(obj, ptr, false);
  
                  return ptr;
              }
          }
  
          public static T FromIntPtr&lt;T&gt;(this IntPtr ptr) {
              if (ptr == IntPtr.Zero)
                  return default(T);
  
              return (T) Marshal.PtrToStructure(ptr, typeof (T));
          }
  </code></pre>
  
  <p>A link of C++ code, of how I have called the function is given.<a href="https://stackoverflow.com/questions/30184208/how-to-dynamically-fill-the-structure-which-is-a-pointer-to-pointer-of-arrays-in/30195771?noredirect=1#comment48798363_30195771">link</a></p>