Return C++ char to C#
<p>I have a C++ project in which I have to return some variables from C++ to C#.</p>

<p>These char variables are in the main program: </p>

<p>char test1[MAX_Q_LEN], test2[MAX_Q_LEN], test3[MAX_Q_LEN];</p>

<p>After I finish doing something with these variables in my C program, I have to return the values of these variables in a C# program.</p>

<p><strong>ReturnChar.h</strong></p>

<pre><code>extern "C" RETURNCHAR_API TCHAR* __cdecl testString();
</code></pre>

<p><strong>ReturnChar.cpp</strong></p>

<pre><code>extern "C" RETURNCHAR_API TCHAR* testString()
{
return ;
}
</code></pre>

<p><strong>TestImport C#</strong></p>

<pre><code>static class TestImport
{
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr testString(); 
}


public partial class MainWindow : Window
{
public MainWindow()
{
try
{
InitializeComponent();
textBox1.Text = ReturnSomething()
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private static string ReturnSomething()
{
IntPtr t = TestImport.testString();
String result = Marshal.PtrToStringAuto(t);
}
</code></pre>

<p>I tried with the above approach but I am not able to find out how to return the above char values. Also, this should not be an independent function because the values shoud be fetched only after executing the main which will give the right values in these variables.</p>

<p>Any suggestions?</p>
