I have a library I'm dependent on that receives a struct with char**
as one of the function's arguments.
I want to have a wrapper that receives a List<string>
(or any string[]
for that matter) and creates a char**
to put in that struct. The compiler is OK with my code, but I'm really not sure I got the fixed
and unsafe
concepts right, and I'm afraid of unexpected consequences:
InternalAttributeKeys attrKeys; // has { (char**) .keys , (int) .count }
unsafe
{
char*[] chars = new char*[keys.Count];
for (var i = 0; i < keys.Count; ++i)
{
fixed (char* c = keys[i].ToCharArray())
{
chars[i] = c;
}
}
fixed (char** keysPtr = chars)
{
attrKeys.keys = keysPtr;
}
}
attrKeys.count = (uint) keys.Count;
return attrKeys;
Did I get it right?
Is there generally a better way to send a char**
(for instance, can I send a char*[]
instead and expect C# to do the conversion behind the scenes)?
Found this, seems to address my question from a different perspective (and it looks more robust than whatever I came up with):
How can I copy a array of strings into an unmanaged char double pointer?
I will use the Marshal.AllocHGlobal
and create an IDisposable
object to manage this memory.