title: Passing a Struct pointer (with char pointers in body) in C# via P/Ivoke
body: <p>I have a .DLL file in C. The primary structure required by all functions in that DLL is of the following form. </p>
  
  <pre><code>typedef struct
  {
      char *snsAccessID;      
      char *snsSecretKey;     
      char *snsPath;          
      char *snsTopicName;     
      char *snsTopicAmazonResourceName; 
      char *snsDisplayName;   
      char *snsOwnerId;       
  } snsTopic, *HSNS;
  </code></pre>
  
  <p>One of the functions just for example is as follows: </p>
  
  <pre><code>BOOL SnsOpenTopic(char *accessID, char *secretKey, char *ownerId, char *path, char *topicName, char *displayName, HSNS *snsTopicHandle);
  </code></pre>
  
  <p>All char pointers above are Input parameters.</p>
  
  <p>I am using C# with .NET CF 3.5 on a WinCE6/7 device. </p>
  
  <p>I have tried using a class and then passing the pointer to the structure required by the C function as follows:</p>
  
  <pre><code>public class HSNS
  {
      public string snsAccessID;      
      public string snsSecretKey;     
      public string snsPath;          
      public string snsTopicName;     
      public string snsTopicAmazonResourceName; 
      public string snsDisplayName;   
      public string snsOwnerId;       
  }
  
  [DllImport("Cloud.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern Boolean SnsOpenTopic(string accessID, string secretKey, string ownerId, string path, string topicName, string displayName, ref HSNS snsTopicHandle);
  </code></pre>
  
  <p>Using the C# snippet above results in NotSupportedException being thrown. I can't figure out what's wrong with the above C# code?</p>
  
  <p>Another thing i tried is using unmanaged code with C#. </p>
  
  <pre><code>unsafe public struct HSNS
  {
      public char *snsAccessID;      
      public char *snsSecretKey;     
      public char *snsPath;          
      public char *snsTopicName;     
      public char *snsTopicAmazonResourceName; 
      public char *snsDisplayName;   
      public char *snsOwnerId;       
  }
  
  [DllImport("Cloud.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern Boolean SnsOpenTopic(string accessID, string secretKey, string ownerId, string path, string topicName, string displayName, HSNS *snsTopicHandle);
  
  fixed (HSNS *snsAcsTopicHandle = &amp;snsAcsTopic)
  {
      if (SnsOpenTopic(AWS_ACCOUNT_ACCESS_ID, AWS_ACCOUNT_SECRET_KEY, AWS_ACCOUNT_OWNER_ID, AWS_SNS_SINGAPORE_REGION, topicName, displayName, snsAcsTopicHandle))
      {
      }
  }
  </code></pre>
  
  <p>In the above case, in debug i can check the pointers inside the structure are not being populated and in the debug view i can see Invalid Reference. Cannot dereference pointer message. The rest of the functions fail because of this.</p>
  
  <p>What is the correct way to use Platform Invoke and Marshalling for the above scenario. I have tried searching on Google and Stack overflow. Didn't find a use case similar to mine. </p>