By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

Data from unmanaged to managed

P: n/a
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.
--
Thanks
Sharon
Jun 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Sharon" wrote:
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.
--
Thanks
Sharon
Jun 11 '07 #2

P: n/a
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) )?
--
Thanks
Sharon
Jun 11 '07 #3

P: n/a
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.
--
Hope this answers youe question
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Sharon" wrote:
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) )?
--
Thanks
Sharon
Jun 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.