I currently have a C# project which is compiled into a COM interop DLL containing the following:
using System.Runtime.InteropServices;
namespace CommonTest
{
[ComVisible(true)]
public interface ICommonTest
{
[DispId(1)]
int Test(int a, int b);
}
[ComVisible(true)]
public class CommonTestManaged : ICommonTest
{
/// <summary>
/// A method to test the creation of a managed DLL built in C#. It's functionality just adds together two numbers.
/// </summary>
/// <param name="a">Number 1</param>
/// <param name="b">Number 2</param>
/// <returns>The sum of numbers a and b</returns>
public int Test(int a, int b)
{
return a + b;
}
}
}
On the C++ side, this method is successfully called with the following:
void Usage()
{
CoInitialize(nullptr);
ICommonTestPtr pICommonTest(__uuidof(CommonTestManaged));
long lResult = 0;
pICommonTest->Test(5, 10, &lResult);
CoUninitialize();
}
My question is, is there a way to pass a C++ struct as an parameter of Test() so that I can access its contents in C#?
It 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#
Use it like this in the interface (and implement it in the class):
The Visual Studio
#importdirective will generate code like this in the .tlh file:And you can use like this in C++: