I'm currently writing a C dynamic library in MacOSX, the functions are then used in Excel (2011) using the VBA, e.g.:
Declare Function TestF Lib "path_to_lib:mylib.dylib" Alias "test" (ByRef res As String) As String
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:
char* test(char *res)
{
res = "test";
return res;
}
but calling the function TestF in VBA as
Dim res As String
res = TestF(res)
crashes Excel... If I use a function that is provided by the MathLink (Mathematica) library, e.g.
#include "mathlink.h"
char* test(MLINK link, char *res)
{
MLGetString(link, &res);
return res;
}
this function successfully assigns a string to "res" that I can use in VBA. The information on MLGetString can be found here: http://reference.wolfram.com/mathematica/ref/c/MLGetString.html.
So apparently, this function is able to create a string and pass it to res
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.?