tags:

views:

157

answers:

1

Can the behaviour of pin_ptr be achieved directly in C++/CLI? For example, is it possible to write CLR code directly, something like asm for native apps?

An example of what I would like to do is a wrapper for a pin_ptr (impossible because of the restrictions on pin_ptrs).

class WrappedPtr
{
public:
    explicit WrappedPtr(String^ s)
    {
        pin = PtrToStringChars(s);
        // I want to pin s for the lifetime of this object (only used on the stack)
    }
};
+1  A: 

Use a GCHandle.Alloc. with GCHandleType.Pinned. You can then use GCHandle.AddrOfPinnedObject to get the address of the .NET object.

hjb417
Thanks, I'll give that a go when I get a chance.
James Hopkin