Whats the best way to pass raw datablocks between C# objects

G

Guest

Hi,

In our system we get image buffers through a native dll as void*. We want to
manage several of this blocks by C# objects an pass them wen needed to a
native image processing library.

What is the best way to do this? Can I just declare a unsafe pointer as
member of a class?

Best regards

Thomas
 
V

Vadym Stetsyak

You can also create managed buffers that will contain the same data as
unmanaged ones, and after processing synchronize managed with unmanaged

IMO another way is to create buffers in the managed side, pin them and then
pass to unmanaged lib.
 
N

Nicholas Paldino [.NET/C# MVP]

Thomas,

You could create an unsafe pointer, but that would require most of your
code to be marked as unsafe (at least, anywhere that uses it).

A better idea would be to have a managed wrapper that exposes a Handle
property (or whatever you want to call it), which returns an IntPtr which
you can pass back to any unmanaged functions you need to.

Hope this helps.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Vadym Stetsyak said:
You can also create managed buffers that will contain the same data as
unmanaged ones, and after processing synchronize managed with unmanaged

IMO another way is to create buffers in the managed side, pin them and
then pass to unmanaged lib.

I think Nicholas suggestion is better, you keep the data in the unmanaged
side, not need to copy it and in managed side you just keep a pointer ( an
IntPtr ) to the bufferr
rr
 
G

Guest

Thank you very much, I think I now understand it better. So I only have to
mark the the region unsafe that assigns the pointer to the IntPtr. The
IntPtr Variable can be used e.g. assigned to another IntPtr without using
unsafe.

Thomas


Nicholas Paldino said:
Thomas,

You could create an unsafe pointer, but that would require most of your
code to be marked as unsafe (at least, anywhere that uses it).

A better idea would be to have a managed wrapper that exposes a Handle
property (or whatever you want to call it), which returns an IntPtr which
you can pass back to any unmanaged functions you need to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

In our system we get image buffers through a native dll as void*. We want
to manage several of this blocks by C# objects an pass them wen needed to
a native image processing library.

What is the best way to do this? Can I just declare a unsafe pointer as
member of a class?

Best regards

Thomas
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Thank you very much, I think I now understand it better. So I only have to
mark the the region unsafe that assigns the pointer to the IntPtr. The
IntPtr Variable can be used e.g. assigned to another IntPtr without using
unsafe.

No really, the IntPtr will be assigned when you call the external method
using P/Invoke, from that moment all you have to do is carry it around.

You will have to release the memory after you are done with it though
 

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