UsingMCppGCRoot Sample: Demonstrates Embedding a __gc Pointer in an Unmanaged Class
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
- In the Visual Studio IDE, load the solution file UsingMCppGCRoot.sln.
- In Solution Explorer, right-click the UsingMCppGCRoot solution.
- On the shortcut menu, click Build.
- On the Debug menu, click Start.
This sample demonstrates the following keywords:
gcroot; __gc; Marshal::StringToHGlobalAnsi; Marshal::FreeHGlobal; System::Runtime::InteropServices