On Mar 10, 6:20 am, ToyTrains <ToyTra...@discussions.microsoft.com>
wrote:
> I am trying to write a wrapper around a C DLL that makes use of a very large
> complex C data structure for all data exchange between the caller and DLL.
[quoted text clipped - 24 lines]
> --
> Dan
Dan,
There are numerous ways to go about doing this, and it is all
dependent on the details of what you are trying to achieve.
One way is to maintain an IntPtr that points to the unmanaged data.
This prevents marshalling large amounts of data between managed and
unmanaged code. From managed code, you can access the unmanaged data
using the Marshal class, or by other means. Access it when you need
it, modify directly in unmanaged memory, and then let your unmanaged
code use it as normal.
Another way is to marshal the data for every call. For you, this seems
cumbersome, since your structures are deep and complex. This is
effectively like passing it by reference between managed and unmanaged
code with the performance of passing by value. You can likely utilize
the MarshalAsAttribute to help the marshaler do its job, or you can
create a custom marshaler by implemented ICustomMarshaler.
I'm sure I could think of other ways, but to me, from the information
you provided, the first way, maintaining an IntPtr to unmanaged data,
sounds like the best way. You can also work out a hybrid, where you
marshal parts of the data, and the parts you don't want to marshal,
you can pass as IntPtr.
adam