如何在MFC类中声明一个.Net FrameWork变量--gcroot(from msdn)
2012-10-15 11:35
543 查看
转载:http://blog.csdn.net/sfcyyc/article/details/571588
You cannot declare a handle type in a native type. vcclr.h provides the type-safe wrapper template
gcroot, to refer to a CLR object from the C++ heap. This template lets you embed a virtual handle in a native type and treat it as if it were the underlying type.
The gcroot template is implemented using the facilities of the value class System::Runtime::InteropServices::GCHandle, which provides "handles" into the garbage-collected heap. Note that the handles themselves are not garbage collected and are freed when
no longer in use by the destructor in the gcroot class (this destructor cannot be called manually). If you instantiate a gcroot object on the native heap, you must call delete on that resource.
The runtime will maintain an association between the handle and the CLR object, which it references. When the CLR object moves with the garbage-collected heap, the handle will return the new address of the object. A variable does not have to be pinned before
it is assigned to a gcroot template.
This sample shows how to create a gcroot object on the native heap.
This sample shows how to gcroot to hold references to value types (not reference types) in a native type by using gcroot on the boxed type.
You cannot declare a handle type in a native type. vcclr.h provides the type-safe wrapper template
gcroot, to refer to a CLR object from the C++ heap. This template lets you embed a virtual handle in a native type and treat it as if it were the underlying type.
The gcroot template is implemented using the facilities of the value class System::Runtime::InteropServices::GCHandle, which provides "handles" into the garbage-collected heap. Note that the handles themselves are not garbage collected and are freed when
no longer in use by the destructor in the gcroot class (this destructor cannot be called manually). If you instantiate a gcroot object on the native heap, you must call delete on that resource.
The runtime will maintain an association between the handle and the CLR object, which it references. When the CLR object moves with the garbage-collected heap, the handle will return the new address of the object. A variable does not have to be pinned before
it is assigned to a gcroot template.
Example
This sample shows how to create a gcroot object on the native stack.Copy Code | |
---|---|
// mcpp_gcroot.cpp // compile with: /clr #include <vcclr.h> using namespace System; class CppClass { public: gcroot<String^> str; // can use str as if it were String^ CppClass() {} }; int main() { CppClass c; c.str = gcnew String("hello"); Console::WriteLine( c.str ); // no cast required } |
Output
hello |
Copy Code | |
---|---|
// mcpp_gcroot_2.cpp // compile with: /clr // compile with: /clr #include <vcclr.h> using namespace System; struct CppClass { gcroot<String ^> * str; CppClass() : str(new gcroot<String ^>) {} ~CppClass() { delete str; } }; int main() { CppClass c; *c.str = gcnew String("hello"); Console::WriteLine( *c.str ); } |
Output
hello |
Copy Code | |
---|---|
// mcpp_gcroot_3.cpp // compile with: /clr #include < vcclr.h > using namespace System; public value struct V { String^ str; }; class Native { public: 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); } |
Output
String in V: Hello |
相关文章推荐
- 如何在MFC类中声明一个.Net FrameWork变量--gcroot(from msdn)
- 如何辨认一个复杂声明表达式子中变量的类型?
- 关于如何使用反射声明一个泛型类型变量(泛型参数是动态的)的问题
- SQL SERVER 如何声明一个变量
- 如何将.NET中定义的一个Window Form控件作为一个MFC对话框调用?(From Msdn)
- 如何声明一个全局变量,可以在应用的任何单元引用
- 【Android 如何置底一个View(附 前置声明layout布局文件中的资源ID)】
- C++ 声明一个变量 和 New 一个空间的区别
- Vue中如何定义一个全局变量(Trick)
- 一个讨论引发关于js中函数声明,函数表达式,形参与变量声明赋值引发的一些事
- 在MFC下如何定义全局变量和全局函数VC++
- 在一个类的函数中声明static变量是个啥
- 在MFC下如何定义全局变量和全局函数(ZZ)
- asp.net 同一个页面中如何保持变量的值在多个事件中变换???
- php 如何获取一个变量的名字
- 如何通过一个结构体成员变量的地址找到该结构体的首地址?
- MFC 如何为控件关联变量
- duilib篇 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)
- MFC字符串分割提取给一个变量
- c语言题有一个浮点型变量x,如何判断x是否等于0