title: Passing a struct pointer as a parameter in C#
body: <p>I have a function in C++ that I exported to a DLL.  I contains a struct pointer as one of the parameters.  I need to use this function in C#, so I used DLLImport for the function and recreated the struct in C# using StructLayout.  I've tried passing in the parameter using ref as well as tried Marshaling it in using MarshalAs(UnmangedType.Struct) and Marshal.PtrToStructure.  The parameter still isn't passing correctly.  </p>
  
  <p>Example:</p>
  
  <pre><code>[DllImport("testdll.dll")]
  public static extern int getProduct(int num1, int num2, [MarshalAs(UnmanagedType.Struct)] ref test_packet tester);
  </code></pre>
  
  <p>One more tidbit of info, the struct contains a byte* var, which I think may be causing the problem in terms of passing the param as ref.  Any ideas? Am I on the right track?  Thanks for the help.</p>
  
  <p>Thanks nobugz for the response. Here's a sample of the struct def: </p>
  
  <pre><code>//C# DEFINITION 
  
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
  
  public struct test_packet 
  { 
  
       public UInt32 var_alloc_size; 
  
      public byte* var; 
  
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_TAG_LENGTH)] 
      public byte[] tag; 
  
  } 
  
  //ORIGINAL UNMANAGED STRUCT
  
  typedef struct test_packet_tag 
  {
  
      unsigned int var_alloc_size;
  
      unsigned char *var;
  
      unsigned char tag[MAX_TAG_LENGTH];
  } test_packet;
  </code></pre>