Data from unmanaged to managed

G

Guest

I'm trying to specify the requirement from unmanaged DLL component that will
be used by a managed application written in C#.
The unmanaged DLL is implementing some kind of algorithm for defect
detection that will generate results. Each defect result should be a
structure containing the data of the found defect. The number of defect is
known only at the end of the algorithm at the unmanaged side.
The managed application should get the defect results and display them in a
DataGridView.

My question is:
How will the defects results data will be transferred from the unmanaged DLL
to the managed application without doing any memory copying?
I do not want to copy the memory (containing the defects data) for that. Is
there a way get the results without doing the memory copy?

I can allocate a big (the largest ) chunk of memory at the managed
application side, and letting the unmanaged component fill it with the
results. But this solution is ugly and the amount of memory to allocate may
be a problem.
 
G

Guest

Hi,
There is a special class called Marshal that can do all possible interop
with unmanaged code. In some cases it can be not so simple to use, but when
you have no choice - it's a good choice. It can take any unmanaged object,
allocate/deallocate it, and move data between managed and unmanaged
objects.Search for "marshaling data structures marshal.PtrTostructure c# in
google and you will get further insights into it
 
G

Guest

Hi, thanks for you help.
I looked at marshal.PtrTostructure and noticed that it also allocate a new
managed memory and copy into it the data from the unmanaged memory (From the
MSDN: "Marshals data from an unmanaged block of memory to a newly allocated
managed object of the specified type").

I wish to avoid the memory copying.

I think the way it should be done is like that:
(a) For every new defect found, the unmanaged code will call (somehow **) a
managed function asking for a new allocated structure.
(b) The managed code will allocate a managed structure and pass it to the
unmanaged code as fixed unsafe pointer.
(c) The unmanaged code will then fill the structure with data and notify the
unmanaged code when done.



** The trouble is how the unmanaged code will invoke a managed function (
like in (a) and (c) )?
 
G

Guest

Hi,
Yes you can call managed functions from unmanaged code using CCW(Com
Callable Wrapper).Search for "CCW Com Callable Wrapper " on google and you
will get further insights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top