How to pass char* in struct from c# to c++
<p>It feels like my problem is similar to <a href="https://stackoverflow.com/questions/36079751/how-to-pass-struct-with-unsigned-char-from-c-sharp-to-c">this</a>.</p>

<p>So far, I have a struct defined in C++ like this:</p>

<pre><code>typedef struct struct_type1
{
    uint64  nameLen;
    char *  name;  
} STATUSSTRUCT;
</code></pre>

<p>and a function defined as:</p>

<pre><code>extern int _stdcall getStatus(STATUSSTRUCT * status);
</code></pre>

<p>and presumably the function like this:</p>

<pre><code>int _stdcall getStatus(STATUSSTRUCT * status)
{
    status-&gt;nameLen = someLength;
    status-&gt;name = someName;
    return 1;
}
</code></pre>

<p>Note that I can't actually change the C++ code (for various reasons) nor the header file.</p>

<p>My C# code looks like this:</p>

<pre><code>public struct STATUSSTRUCT
{
    public UInt64 nameLen;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4128)]
    public byte[] name;
}
STATUSSTRUCT status;

[DllImport("test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int getStatus(ref STATUSSTRUCT status);

public void refreshStatus() {
    status = new STATUSSTRUCT();
    status.nameLen = 4128;
    status.name = new byte[4128];
    getStatus(ref status);
}
</code></pre>

<p>However, calling refreshStatus is giving me System.AccessViolationException.</p>

<p>Can someone help me figure out how I can call this function in C++ from C#?</p>