title: Importing C struct to C# program
body: <h2>The Sctruct</h2>
  
  <p>I am not much of a C# or C programmer and have not found much guidance on specifying pointers and structs within a struct. I am attempting to import the following from a C dll into a C# program:</p>
  
  <pre><code>#define MAXFILENAME     259 
  
  struct IDentry {
      char* IDname;
      int length;
  };
  typedef struct IDentry idEntry;
  
  
  struct SMOutputAPI {
      char name[MAXFILENAME + 1];     
      FILE* file;                     
  
      struct IDentry *elementNames;   
  
      long Nperiods;                  
      int FlowUnits;                  
  
      int Nsubcatch;                  
      int Nnodes;                     
      int Nlinks;                     
      int Npolluts;                   
  
      int SubcatchVars;               
      int NodeVars;                   
      int LinkVars;                   
      int SysVars;                    
  
      double StartDate;               
      int    ReportStep;              
  
      __int64 IDPos;                  
      __int64 ObjPropPos;             
      __int64 ResultsPos;               
      __int64 BytesPerPeriod;           
  };
  </code></pre>
  
  <p>I am not sure how to handle the <code>*elementsNames</code>, <code>file</code> or <code>name</code> properties. What I have so far in C# is:</p>
  
  <pre><code>int MAXFILENAME = 259 
  
  [StructLayout(LayoutKind.Sequential)]
  public struct SMOutputAPI
  {
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAXFILENAME+1)]
      public string name;
  
      IntPtr file;
  
      IntPtr elementNames;
  
      public long Nperiods;   
      public int FlowUnits;   
  
      public int Nsubcatch;   
      public int Nnodes;      
      public int Nlinks;      
      public int Npolluts;    
  
      public int SubcatchVars;
      public int NodeVars;    
      public int LinkVars;    
      public int SysVars;     
  
      public double StartDate;
      public int ReportStep;  
  
      public int IDPos;       
      public int ObjPropPos;              
      public int ResultsPos;              
      public int BytesPerPeriod;          
  };
  </code></pre>
  
  <p>The C# application builds fine, but when I call the C initialization function that should return a new SMOutputAPI struct I get an error:</p>
  
  <pre><code>System.Runtime.InteropServices.MarshalDirectiveException 
  Method's type signature is not PInvoke compatible. 
  </code></pre>
  
  <p>Any thoughts on how to properly specify this struct in C# would be much appreciated. Thanks!</p>
  
  <h2>Initializing the Struct</h2>
  
  <p>The struct is initialized in the c-code with:</p>
  
  <pre><code>SMOutputAPI* DLLEXPORT SMO_init(void)
  //
  //  Purpose: Returns an initialized pointer for the opaque SMOutputAPI
  //    structure.
  //
  {
      SMOutputAPI *smoapi = malloc(sizeof(struct SMOutputAPI));
      smoapi-&gt;elementNames = NULL;
  
      return smoapi;
  }
  </code></pre>
  
  <p>The corresponding c# code is:</p>
  
  <pre><code>[DllImport("swmm-output.dll")]
  static extern SMOutputAPI SMO_init();
  
  
  static void Main(string[] args)
  {
      Console.Write("Hello World!");
  
      SMOutputAPI SMO = SMO_init();
  }
  </code></pre>