Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Hold Reference to Value Type in Native Type

Use gcroot on the boxed type to hold a reference to a value type in a native type.

// reference_to_value_in_native.cpp
// compile with: /clr
#using <mscorlib.dll>
#include <vcclr.h> 

using namespace System; 

public value struct V {
   String ^str;

class Native {
   gcroot< V^ > v_handle;

int main() {
   Native native;
   V v;
   native.v_handle = v;
   native.v_handle->str = "Hello";
   Console::WriteLine("String in V: {0}", native.v_handle->str);
String in V: Hello

Community Additions

© 2015 Microsoft