How can I pass a C++ struct to a C# DLL method using COM interop
See the question and my original answer on StackOverflowIt depends on the struct definition and types (it must describable by a type library), but yes, you can create a struct like this in C#
[ComVisible(true)]
[StructLayout(LayoutKind.Sequential)]
public struct Test
{
public int Value1;
public int Value2;
etc...
}
Use it like this in the interface (and implement it in the class):
[ComVisible(true)]
public interface ICommonTest
{
void Test(ref Test test);
}
The Visual Studio #import
directive will generate code like this in the .tlh file:
struct __declspec(uuid("d80d27a3-7643-39bf-bba8-c8cc0ab10e7e"))
Test
{
long Value1;
long Value2;
etc...
};
And you can use like this in C++:
ICommonTestPtr pICommonTest(__uuidof(CommonTestManaged));
Test t = {};
t.Value2 = 123456789;
pICommonTest->Test(&t);