Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2008

Tip: Looking for answers? Try searching our database.

Unmanaged C Structure-embedded arrays, structure pointers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ToyTrains - 10 Mar 2008 14:20 GMT
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.
The data structure is allocated/deallocated during various calls to functions
in the C DLL. The data structure contains pointers to other structures as
well as arrays (both arrays of ints, floats, etc and arrays of pointers to
other structurers).

I have not found a way to setup a pointer to this data structure in managed
C#, pass it into the C DLL routines, and then both reference data from the
data structure and set data in the data structure from the managed C#.

If all of the data structure elements are blittable then I can setup the
pointers to other data structures and reference them from C#. But if the data
structure contains management elements (like arrays of pointers to
structures) then I get a compilation error when I try to have a pointer in
the data structure (you can't have pointers to managed structures).

Btw: The interface to the C DLL cannot be modified.

The only way I can see doing this would be to write an intermediate C DLL
which contained many small routines that would individually set/get data from
the C structures and then wrap these routines in the managed C# routines.

I am using VS2005 .Net v2.0
Any assistance would be appreciated.
Thanks!
Signature

Dan

adamroot@microsoft.com - 28 Mar 2008 01:28 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.