Hi,
I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem. This is what I have...
C++ side
void populateRequiredIndexNames(char** names);
C# side
[DllImport("QRMAPIDummyImpl.dll", CharSet = CharSet.Auto)]
private static extern void populateFieldCategories(out IntPtr categories);
public string[] FieldCategories
{
get
{
int num = NumberOfFieldCategories;
string[] categories = new string[num];
IntPtr ptr;
populateFieldCategories(out ptr); <---- FAILS HERE
for (int i = 0; i < num; i++ )
{
categories[i] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(ptr, i*IntPtr.Size));
}
return categories;
}
}
It gives me an System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The C++ team is trying to pass an array of C-strings, but I don't seem to be able to get that. Other methods work fine (ones returning primitive types or char*) but the method with char** doesn't want to work.
Is it ok for them to pass a char** in the C++ function or should they pass a char*** if they want to give me an array of C-strings?
Any help would be greatly appreaciated.
Thx