question about pinning pointers

J

J

I understand how to pin pointers as they are declared, but for some
reason, I can'f figure out the syntax for predaclaring a pointer and
then pinning in afterward like you would if you wanted to embed an
unmanaged object: Assumingthat pAudioU has alread been defined
as the appriate type of pointer,

Instead of
pAudioU = new __pin AudioDriver_U; // syntax error

it seems necessary to take the extra step of creating an intermediate
pointer and then assigning that:

AudioDriver_U __pin *U = new AudioDriver_U;
pAudioU = U;

Am I overlooking some syntactical subtlety?
 
M

Mattias Sjögren

Am I overlooking some syntactical subtlety?

In metadata, the pinning nature of the pointer is recorded as a
modifier on the local variable type. You can see this if you view the
code with ILDASM. So basicly you need a __pin local variable for
pinning.



Mattias
 
I

Ioannis Vranos

J said:
I understand how to pin pointers as they are declared, but for some
reason, I can'f figure out the syntax for predaclaring a pointer and
then pinning in afterward like you would if you wanted to embed an
unmanaged object: Assumingthat pAudioU has alread been defined
as the appriate type of pointer,

Instead of
pAudioU = new __pin AudioDriver_U; // syntax error

it seems necessary to take the extra step of creating an intermediate
pointer and then assigning that:

AudioDriver_U __pin *U = new AudioDriver_U;
pAudioU = U;

Am I overlooking some syntactical subtlety?


If I understood the question, you can do:


SomeClass *p= __gc new SomeClass;

// ...

SomeClass __pin *pinp= p;

// ... Do stuff with pinned object


//Unpin object
pinp=0;
 
J

J

In metadata, the pinning nature of the pointer is recorded as a
modifier on the local variable type. You can see this if you view the
code with ILDASM. So basicly you need a __pin local variable for
pinning.

so in regard to this:

pAudioU = new __pin AudioDriver_U; // syntax error

OK, that just won't compile...

vs this:
AudioDriver_U __pin *U = new AudioDriver_U;
pAudioU = U;

Did you mean that the latter is a necessessity... or it won't function
as planned? Just want to be clear about it.

The counterintuitive thing: It would seem that you can't easily keep
a permanent pinned pointer to a permanently embedded unmanaged object.
Yet that is exactly what you want to do for embedded unmanaged DLLs
and such, right?

How on earth did you figure this out? The MSDN never seems quite
clear on this stuff. I'd like to avoid P-Invoke if possible. Is
there a definitive reference on how to manage C++ wrappers?
 

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