How to call C# delegate to pass array of strings from native C simplest way?

Viewed 2134

I know this can be done by mallocing in C, passing malloced pointer to delegate with parameter type IntPtr, marshalling to string[] and then freeing malloced memory with separate, exported C-function from managed code.

My question is: Can this be done simpler way? E.g. :

  • C# delegate parameter is of type string[]?
  • no separate free function to call from managed code

EDIT: I tried with delegate signature:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
MyManagedDelegate(string[] values, int valueCount)

and fucntion in C:

void NativeCallDelegate(char *pStringValues[], int nValues)
{
    if (gSetStringValuesCB)
        gSetStringValuesCB(pStringValues, nValues);
}

calling it in C:

char *Values[]= {"One", "Two", "Three"};
NativeCallDelegate(Values, 3);

This results in that i can use only 1st string in array.

2 Answers
Related