[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Obiwan Jacobi

5/2/2007 11:14:00 AM

Hi,

I'm trying to decide what is the best way to pass a data structure to
a P/Invoked Win32 function.

As far as I can tell there are two options:
1) Use the Marshal class to allocate native memory an copy the data
into it.
2) Use the GCHandle to pin a managed object that has the exact same
layout.

I know that pinning will hinder the GC's efficiency. Allocating native
memory will increase the 'memory pressure' for the GC but not
interfere with its collection strategies and it requires additional
copy instructions to move the data.

So whats the best choice when both performance and memory management
efficiency are important?

Thanx
Marc Jacobi

5 Answers

Obiwan Jacobi

5/2/2007 11:25:00 AM

0

PS: I forgot to mention that the 'data structure' is used by the Win32
function for an extended period of time (for callback). It is not a
case of simple parameter passing. The 'data structure' has to remain
alive.

G Himangi

5/5/2007 10:35:00 AM

0

I believe that depends on the number of times the win32 API is being called.
If it is called just a couple of times, then it wont matter at all what
method is used. If it is however used in a loop or otherwise called a
ignorant number of times, then the choice will matter. It also depends on
the size of the data that is to be passed.

---------
- G Himangi, Sky Software http://www....
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------



"obiwanjacobi" <obiwanjacobi@hotmail.com> wrote in message
news:1178104412.537061.252340@c35g2000hsg.googlegroups.com...
> Hi,
>
> I'm trying to decide what is the best way to pass a data structure to
> a P/Invoked Win32 function.
>
> As far as I can tell there are two options:
> 1) Use the Marshal class to allocate native memory an copy the data
> into it.
> 2) Use the GCHandle to pin a managed object that has the exact same
> layout.
>
> I know that pinning will hinder the GC's efficiency. Allocating native
> memory will increase the 'memory pressure' for the GC but not
> interfere with its collection strategies and it requires additional
> copy instructions to move the data.
>
> So whats the best choice when both performance and memory management
> efficiency are important?
>
> Thanx
> Marc Jacobi
>


Obiwan Jacobi

5/9/2007 9:22:00 AM

0

On May 5, 12:34 pm, "G Himangi" <i...@ssware.com> wrote:
> I believe that depends on the number of times the win32 API is being called.
> If it is called just a couple of times, then it wont matter at all what
> method is used. If it is however used in a loop or otherwise called a
> ignorant number of times, then the choice will matter. It also depends on
> the size of the data that is to be passed.

My specific scenario concerns the midi (multi media) Win32 API. This
API requires user allocated buffers to be registered for receiving
Midi (midiInAddBuffers). Multiple buffers are registered. When a
buffer has filled up with received midi data it is returned via a
callback passed during the midiInOpen function. A midi buffer consists
of a MIDIHDR structure (about 36 bytes) that contains information on
the buffer (how many byte were recorded). It also points to an empty
memory block that receives the midi data. I have a managed
representation of the MIDIHDR.

The question is: should I copy my managed MidiHeader into an unmanaged
memory block to build the MIDIHDR structure or should I pin my managed
MidiHeader and pass its address directly?

Christian Fröschlin

5/9/2007 9:30:00 AM

0

> The question is: should I copy my managed MidiHeader into an unmanaged
> memory block to build the MIDIHDR structure or should I pin my managed
> MidiHeader and pass its address directly?

I would prefer using an unmanaged memory block for this, because
it makes you independent of the way the GC handles pinning.

Obiwan Jacobi

5/10/2007 9:57:00 AM

0

On May 9, 11:29 am, Christian Fröschlin <froesch...@mvtec.com> wrote:
> > The question is: should I copy my managed MidiHeader into an unmanaged
> > memory block to build the MIDIHDR structure or should I pin my managed
> > MidiHeader and pass its address directly?
>
> I would prefer using an unmanaged memory block for this, because
> it makes you independent of the way the GC handles pinning.

That would be my hunch too, but I'm not completely up-to-date on how
the GC handles pinning. But the thought of a fixed memory block the GC
has to work around suggests that its efficiency can be severly
impacted when multiple pinned objects exist (possible spread over the
entire memory space)... I don't know, seems better to take the
overhead of copying data.

My current implementation uses an unmanaged memory block (allocate
through the Marshal class) and copies in the MIDIHDR data. I think
I'll stick to that...

Thanx,
--Marc