I have been given a C++ Library with a function that takes a parameter of list< pair< tstring, tstring>>. I need to create a C# shim for other developers to be able to use this library.
I understand that marshaling generic types is not allowed, so I wanted to know if I can pass in an array of structs which mimics the list of pairs in C++.
In order to test if this works, I made a simple C++ DLL which mimics the DLL I was given. I created the following function in my C++ Test DLL:
//In my C++ Test DLL
int MyFunction(list<pair<tstring, tstring>> &listParams)
In C#, I created the following Structure to mimic the pair of tstrings:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
struct myPair
{
[MarshalAs(UnmanagedType.LPTStr)]
public string Key;
[MarshalAs(UnmanagedType.LPTStr)]
public string Value;
public myPair(string key, string val)
{
Key = key;
Value = val;
}
}
Here is also the PInvoke definition to my C++ function:
[System.Runtime.InteropServices.DllImport("MyTestLib.dll",
CharSet = CharSet.Unicode, EntryPoint = "MyFunction")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern Int32 MyFunction(
[param: MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
ref myPair[] listParams);
Here is the code in my C# Test Shim:
public Int32 MyFunctionTest(Dictionary<string, string> testData)
{
Int32 retCode = 0;
try
{
List<myPair> transfer = new List<myPair>();
foreach (var entry in testData)
{
transfer.Add(new myPair(entry.Key, entry.Value));
}
myPair[] transferArray = transfer.ToArray();
retCode = NativeMethods.MyFunction(ref transferArray);
}
catch (Exception ex)
{
}
return retCode;
}
Although the call is successful (does not crash), in my C++ method, the data is garbled and invalid.
Does anyone know if this sort of mapping is even possible?