473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Data from unmanaged to managed

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
3 2182
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
3
by: Mark Urish | last post by:
Is there a way to import data from an unmanaged dll? For example, unmanaged.dll: __declspec(dllexport) int unmanagedErrno; managed.cs: class Managed { //this doesn't work:
5
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However,...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.