How to convert between a C# String and C Char pointer
<p>I have a large library written in C that I would like to use as a DLL in a C# program. Most of the C code will be used by the libraries own functions, but I do need one function to be able to be called from the C# project.</p>

<p>So there's an example C function below</p>

<pre><code>__declspec(dllexport) char* test(char* a){
    char* b = "World";
    char* result = malloc(strlen(a) + strlen(b) + 1);

    strcpy(result, a);
    strcpy(result, b);

    return result;
}
</code></pre>

<p>Now in the C# code I have got <code>using System.Running.InteropServices;</code>
and also <code>[DllImport("mydll.dll")]</code> but I'm not sure how to declared the function.</p>

<p><code>public static extern char* test(char* a);</code> obviously doesn't work because C# doesn't support pointers like C does. </p>

<p>So how should I pass a string to this C function and have it return a string as well?</p>