Calling C unmanaged DLL with with output parameters char **result from C#

In C# library I wanted to call c function from YAZ library


YAZ_EXPORT

int yaz_marc_decode_buf(yaz_marc_t mt, const char *buf, int bsize, char **result, int *rsize);

The problem was that I didn’t know how to specify char **result in managed declaration. I didn’t find similar samplers in Zoom.NET implementation.
The article
Call Unmanaged DLLs from C# recommended to use StringBuilder  for LPSTR (char*) but it wasn’t enough.


After some experimentation I found that adding ref will do the trick(which makes sense).
So the c# declaration is the following:


        [DllImport(“yaz.dll”,


SetLastError = true,


CharSet = CharSet.Ansi,


CallingConvention = CallingConvention.Cdecl,


EntryPoint = “yaz_marc_decode_buf”


)]


        public static extern void yaz_marc_decode_buf(IntPtr yaz_marc_t, String marcBuf, int bsize,


           ref StringBuilder result, ref int rsize);


and the sample code :


StringBuilder result=new StringBuilder() ; /* for result buf */

int result_len = 0; /* for size of result */

YazExt.yaz_marc_decode_buf (_yazMarc, marc_buf, -1, ref result, ref result_len);

string sXml = result.ToString();