0

I have a struct that I've defined in both C# and C++ that looks like so:

C#

Public struct Part{
    public int number;
    public string name;
}

C++

struct Part{
    int number;
    std::string name;
}

I have a function that will do many things, but for the sake of simplicity, it will change around the part that was passed in.

C#

[DllImport(@"Part.Interface.dll")]
private static extern void _ChangeAStruct(ref Part part);

public void PassAStruct(){
    Part part = new Part();
    _ChangeAStruct(ref part);
}

C++

extern "C" __declspec(ddlexport) void __cdecl _ChangeAStruct(Part &part) {
    part.number = 42;
    part.name = "I am a struct";
}

On the C# side it successfully changes the number but the name returns as null. I have tried to [MarshalAs(UnmanagedType.LPStr)] on the string but that didn't change anything.

How do I change the structs to pass back "I am a struct"? It has been mentioned that interop cannot handle std::string, but what can it handle?

WWZee
  • 494
  • 2
  • 8
  • 23
  • 2
    I don't think P/Invoke has any support for `std::string`. – SLaks Jan 25 '17 at 18:30
  • @Dan keeping it in the struct was my entire goal, I was unsure if having it in a struct changes how this would be approched – WWZee Jan 25 '17 at 21:06
  • @Dan I actually took out that part about keeping old code. As it turns out we're expanding from here and using the old code as a base but not as a strict usage. So all I need now is the ability to pass back a string equivalent, but not necessarily a string itself. – WWZee Jan 25 '17 at 21:16

0 Answers0