Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Question about Marshalling of data structures across C++/C# #8365

Closed
darderik opened this issue Oct 13, 2020 · 2 comments
Closed

Question about Marshalling of data structures across C++/C# #8365

darderik opened this issue Oct 13, 2020 · 2 comments
Labels

Comments

@darderik
Copy link
Contributor

Hello, following the solution provided to the issue #6984 , I tried playing around with the return of data structures from C++ to C#, as i couldn't find an issue related to this kind of operation, I thought about opening one in order to receive a few heads-up on the topic.

C++ Code

struct myStruct {
    int ok;
    string testStr;
} ;
extern "C" myStruct entry() {
    myStruct myStu;
    myStu.ok = 1;
    myStu.testStr = "hello";
    return myStu;
}

C# Code

namespace CppInterop
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic ok = entry();
        }
        [DllImport("*")]
        public extern static dynamic entry();
    }
}

As expected, i get this error

Unhandled Exception: System.Exception: Method '[CppInterop]CppInterop.Program.entry()' requires marshalling that is not yet supported by this compiler.
   at CppInterop.Program.entry() + 0x30
   at CppInterop!<BaseAddress>+0x1e9b2d
Aborted

I was wondering if there was another way (as I know that dynamic isn't aot friendly at all) to pass data structures (or objects) back from cpp , in the particular case that i know exactly how is the passed object structured.
Thank you for the help 👍

@darderik darderik changed the title Question about Marshalling of data structure across C++/C# Question about Marshalling of data structures across C++/C# Oct 13, 2020
@MichalStrehovsky
Copy link
Member

extern "C" myStruct entry()

You can't pass a myStruct with a std::string to anything, really. AFAIK the memory layout of std::string is not even stable across different versions of the C++ standard libraries (a DLL compiled with e.g. Visual C++ 6.0 would have a hard time consuming this mystruct coming from an EXE compiled with Visual Studio 2019).

You have to use C types such as char*.

public extern static dynamic entry();

This means COM marshalling - the C++ side would have to return the value as a COM interface for this to work. CoreRT doesn't support COM.

I suggest reading up on p/invoke in general. This question is not really CoreRT related.

@darderik
Copy link
Contributor Author

Clear enough. I read the error so I thought that there was something in the work in order to 'marshal' complex data structures(in this case COM Interfaces). thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants