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

UsingMCppGCRoot Sample: Demonstrates Embedding a __gc Pointer in an Unmanaged Class

Visual Studio .NET 2003

The UsingMCppGCRoot sample illustrates how to embed a __gc pointer in an unmanaged class. In particular, it shows how to access a __gc pointer (to a string allocated in the managed heap) from an object allocated on the unmanaged heap.

Embedding a __gc pointer mainly involves creating a "handle." The sample demonstrates a way to wrap a handle such that there is no need to create or delete it. The wrapping is done with the gcroot template from vcclr.h. This file can be found in the \Microsoft Visual Studio .NET 2003\Vc7\include directory.

For more information on using the gcroot template from vcclr.h, see 16.3 __gc Pointers in Unmanaged Classes.

Building and Running the Sample

To build and run UsingMCppGCRoot using Visual Studio

  1. In the Visual Studio IDE, load the solution file UsingMCppGCRoot.sln.
  2. In Solution Explorer, right-click the UsingMCppGCRoot solution.
  3. On the shortcut menu, click Build.
  4. On the Debug menu, click Start.


This sample demonstrates the following keywords:

gcroot; __gc; Marshal::StringToHGlobalAnsi; Marshal::FreeHGlobal; System::Runtime::InteropServices

See Also

Managed Extensions for C++ Samples

© 2015 Microsoft