How can I pass a string from a C# DLL into an unmanaged language (e.g. C++)? <p>I've had quite a lot of luck interoperating between Fortran (don't ask! :-) and C# using Robert Giesecke's wonderful "<a href="https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports" rel="nofollow">Unmanaged Exports</a>" library. However, one of the things I haven't been able to achieve yet is sending a string from C# to Fortran. The worst of it is that I get no indication of what's wrong. My Fortran application just crashes. I've posted this problem to the most popular Fortran forum I know (<a href="https://software.intel.com/en-us/comment/1888588" rel="nofollow">here</a>), and so far it has generated a lot of discussion but I'm no closer to solving the problem. I'm thinking now that if I can solve it in C/C++, perhaps I can take that solution over to Fortran more easily.</p> <p>From all the research I've done, it would seem to me that something like the following should work:</p> <pre><code>using RGiesecke.DllExport; using System.Runtime.InteropServices; namespace CSharpDll { public class Structures { [StructLayout(LayoutKind.Sequential)] public struct MyStruct { [MarshalAs(UnmanagedType.LPStr)] public string Title; public int SourceNode; public int EndNode; } [DllExport("GetStruct", CallingConvention = CallingConvention.Cdecl)] public static MyStruct GetStruct() { var result = new MyStruct(); result.Title = "This is the title"; result.SourceNode = 123; result.EndNode = 321; return result; } } } </code></pre> <p>Unfortunately, my C/C++ is even rustier than my Fortan. I have no idea how to make this work on that side. Can someone help me write an extremely simple console app in C/C++ that will call the above "GetStruct" method and get the structure including the string?</p> <p>Note 1: I've tried pretty much every permutation of "UnmanagedType" as well as both "string" and "char[]" with no luck, so whereas I may not have that right in this example, please don't suggest one of those unless you can show that it works. :-)</p> <p>Note 2: The vast majority of threads I've found around this topic send strings the other way (i.e. from an unmanaged DLL to a managed application rather than from an unmanaged application to a managed DLL). I'm not interested in those, since I have that working like a charm. :-)</p>