Records passed to unmanaged code!

A

Atmapuri

Hi!

When records are passed by reference to unmanaged code
do they need to be pinned down explicitely or not?

I must have gone over this a dozen of times, but I am still
not sure about it. I know that records are also just
objects, but maybe CLR treats them differently.

Thanks!
Atmapuri.
 
N

Nicholas Paldino [.NET/C# MVP]

Atmapuri,

When you make calls to unmanaged code, passing a structure structures by
reference, or a class that is marshaled as a structure, the P/Invoke layer
and COM interop will pin down those objects for the lifetime of the call.
If the COM method or the unmanaged function stores the pointer, then this is
an issue, because you will have to store that structure in unmanaged memory,
and adjust it there (so that the pointer in memory is never moved).

Hope this helps.
 
A

Atmapuri

Hi!

Thanks. Perfect.
Atmapuri.

Nicholas Paldino said:
Atmapuri,

When you make calls to unmanaged code, passing a structure structures by
reference, or a class that is marshaled as a structure, the P/Invoke layer
and COM interop will pin down those objects for the lifetime of the call.
If the COM method or the unmanaged function stores the pointer, then this is
an issue, because you will have to store that structure in unmanaged memory,
and adjust it there (so that the pointer in memory is never moved).

Hope this helps.

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


Atmapuri said:
Hi!

When records are passed by reference to unmanaged code
do they need to be pinned down explicitely or not?

I must have gone over this a dozen of times, but I am still
not sure about it. I know that records are also just
objects, but maybe CLR treats them differently.

Thanks!
Atmapuri.
 

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