2

For a few days now I have been trying to Marshal a complex struct from C++ to C#, basically I have managed to get most of what I am trying to achieve done but now I'm stuck trying to marshal what I believe is a list.

In example I will include what I do get working and where I am stuck.

public: void __thiscall TransactionModule_t::GetTransaction(class Identity_t const &)const 

Conformed as follwoing:

// public: void __thiscall TransactionModule_t::GetTransaction(class Identity_t const &)const     
[DllImport("Transaction.dll", EntryPoint = "?GetTransaction@TransactionModule_t@@Identity_t@@@Z", CallingConvention = CallingConvention.ThisCall)]
public static extern void GetTransaction(IntPtr iPtr,[Out, MarshalAs(UnmanagedType.LPStruct)] Identity transaction);


[StructLayout(LayoutKind.Sequential)]
[Serializable]
public class Identity
{
    public uint Id;
    public uint Type;

    public Identity(uint id = 0, uint type = 0)
    {
        this.Id = id;
        this.Type = type;
    }
}

This is working just fine.

However I want to call a method which gives me the list.

public: void __thiscall TransactionModule_t::GetTransactions(class std::vector<class Identity_t,class std::allocator<class Identity_t> > &)const 

And where i am getting stuck:

// public: void __thiscall TransactionModule_t::GetTransactions(class std::vector<class Identity_t,class std::allocator<class Identity_t> > &)const 
[DllImport("Transaction.dll", EntryPoint = "long mangled entry point", CallingConvention = CallingConvention.ThisCall)]
public static extern void GetTransactions(IntPtr iPtr,[Out] Transactions transactions);

I tried making a class that fits in between the two.

[StructLayout(LayoutKind.Sequential)]
[Serializable]
public class Transactions
{
    public Identity Identity;
    public Identity[] List;
}

Is it even possible to call this method, am I missing something here?

Joachim
  • 360
  • 2
  • 12
  • Marshalling works when both parties can understand the structure and access mechanism of data which is passed back and forth. C# won't understand structure and access mechanism of vectors. – sameerkn Jul 24 '15 at 06:38

1 Answers1

0

Is it even possible to call this method?

No it is not. You cannot supply a std::vector from C# code.

Realistically you are going to need a C++/CLI wrapper.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • This was what I was afraid of, its time for me to start study some C++ and try grasp what is going on. So far as from what I have read a vector is a undefined array that can change in size and allocator is a pre defined sized array. – Joachim Jul 24 '15 at 11:08
  • The point is that interop requires binary compatibility, and that's not possible for complex types like C++ classes. C++/CLI makes life easy. – David Heffernan Jul 24 '15 at 11:19