Please help me chase out a SafeArrayTypeMismatchException that I keep getting. I need to pass a struct to unmanaged DLL. One of the struct members is a variable-length array. Unmanaged code will populate it with data, then my C# code will use the data.
My approach is this:
1. Get IntPtr for my structure using StructureToPtr(...), do the memory allocation too, of course.
2. Call unmanaged function, and pass IntPtr as a parameter
3. Get the populated structure using PtrToStructure(...)
If for the purposes of an exercise I call StructureToPtr(...) and PtrToStructure(...) back-to-back there are no exceptions.
PtrToStructure(...) generates SafeArrayTypeMismatchException, if I put a call to unmanaged DLL between StructureToPtr(...) and PtrToStructure(...). The description for SafeArrayTypeMismatchException is "Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata."
Any suggestion, insight or reference is really appreciated!
I can post my code if needed.
Last edited by kender_a; December 27th, 2010 at 05:26 PM.
P.S. The important part of the VB code in the article translates as:
DispatchWrapper dispArray = new DispatchWrapper;
for (int i = 0; i < 9; i++)
// I'll assume that the objs array already exists
dispArray[i] = new DispatchWrapper(objs[i]);
// And then just pass dispArray instead.