title: How to send a struct by ref containing a string from C# to unmanaged C++ library
body: <p>I need to send a struct from C# managed code to a C library.  The C library will populate the values in the struct.  I have been trying to pass the struct as a reference so that the C# code will get the updated data values.</p>
  
  <p>This is an example C function in libshlib.so:</p>
  
  <pre><code>void sharedStruct(struct data* d)
  {
     d-&gt;number = calcSomething();
     d-&gt;message = doSomething();
  }
  </code></pre>
  
  <p>I can send individual parameters (int and StringBuilder) and the library code runs and returns new values to the C# code that called it.</p>
  
  <p>But how can I create a struct in C# that contains both int and string and send it to the unmanaged code (C library) which will populate the values to be used back in the C# code? </p>
  
  <p>The C struct might be like this:</p>
  
  <pre><code>struct data
  {
    int number;
    char* message;
  };
  </code></pre>
  
  <p>Right now I'm trying to establish the best way to manipulate data in the C library for use in C#.  I am writing both pieces of code so I am flexible but right now I haven't been able to get it working.</p>