title: Return string to VBA in MacOSX
body: <p>I'm currently writing a C dynamic library in MacOSX, the functions are then used in Excel (2011) using the VBA, e.g.:</p>
  
  <pre><code>Declare Function TestF Lib "path_to_lib:mylib.dylib" Alias "test" (ByRef res As String) As String
  </code></pre>
  
  <p>This works fine for functions that return integers etc., but I can't figure out how to pass strings back to VBA. My C function looks like this:</p>
  
  <pre><code>char* test(char *res)
  {
    res = "test";
    return res;
  }
  </code></pre>
  
  <p>but calling the function TestF in VBA as</p>
  
  <pre><code>Dim res As String
  res = TestF(res)
  </code></pre>
  
  <p>crashes Excel...
  If I use a function that is provided by the MathLink (Mathematica) library, e.g.</p>
  
  <pre><code>#include "mathlink.h"
  char* test(MLINK link, char *res)
  {
    MLGetString(link, &amp;res);
    return res;
  }
  </code></pre>
  
  <p>this function successfully assigns a string to "res" that I can use in VBA. The information on MLGetString can be found here: <a href="http://reference.wolfram.com/mathematica/ref/c/MLGetString.html" rel="nofollow">http://reference.wolfram.com/mathematica/ref/c/MLGetString.html</a>.</p>
  
  <p>So apparently, this function is able to create a string and pass it to <code>res</code> and I can use the string in VBA then. So any ideas how this function does that, or how I could pass a string to VBA without having to use OLE etc.? </p>