From C#.NET I'm trying to PInvoke a method from a C++ assembly (no access to source) with this signature
result_code get_values(values_report_type *values_report)
Where values_report is a pointer to a struct of values_report_type containing the values I am querying which are returned by the method. The struct looks like this:
typedef struct{
int countOfValues;
values_type* values;
const char* name;
} values_report_type;
Where values is an array of struct values_type.
Here's what I would assume to be the problem: the C++ assembly I am working against gives me no information about the content of the values_type struct other than the definition
typedef struct values_type_struct *values_type
According to documentation this privacy is intentional.
So, my C# PInvoke looks like this right now:
[DllImport("MyLibrary.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern result_code get_values(out values_report_type values_report);
internal struct values_report_type{
int countOfValues;
IntPtr values;
string name;
}
That works fine and gives me a pointer to the values struct, but what I need is to access the array of values_type structs and get a pointer to each of the items in the array (a pointer to each item is all I need since I don't have a definition for the struct contents). But I can't think of a way to achieve this, especially because the library has limited me in what it says about that struct (not even giving length). In C++ it would just be something like values_report.values[0] and so on for each item as defined by countOfValues, but I don't know of a way to make it work when marshalling it to .NET.
Is there a way to solve this in C# via marshalling?
valuesis not an array ofstruct. It is an array of pointers tostruct values_type_struct. What are you meant to do with the elements invalues? Readingvalues[i]is easy enough (IntPtr Value = Marshal.ReadIntPtr(values_report.values, i*IntPtr.Size)), it's what happens next that is not clear.values_type_structelements to perform calculations. So basically, I can't view the contents of the struct but I can send a pointer to it to another method for calculations and transformation.values_typeobjects by converting it to a byte array usingMarshal.Copythen feeding it into aBinarySerializer. The only problem is that in order to do the marshalling, you would need to know the size of the amount of memory you want to get, so you will need to know the size of the array (presumably given bycountOfValues) as well as the size of avalues_typeobject (less obvious).