如何在MFC类中声明一个.Net FrameWork变量--gcroot(from msdn)
来源:互联网 发布:p2p数据分析思路 编辑:程序博客网 时间:2024/05/04 01:33
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.
// 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
This sample shows how to create a gcroot object on the native heap.
// 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
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.
// 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)
- 如何在MFC类中声明一个.Net FrameWork变量--gcroot(from msdn)
- 如何将.NET中定义的一个Window Form控件作为一个MFC对话框调用?(From Msdn)
- 如何将.NET中编写控件作为MFC的视图调用?(From MSDN)
- 在 ASP.NET 中执行 URL 重写(ZZ FROM MSDN)
- 在一个类的函数中声明static变量是个啥
- 如何在MFC的对话框调用CLR控件(From Msdn)
- C++---如何在类中声明一个常量?
- 如何:在 .NET Compact Framework 中使用 DateTimePicker 类
- 如何在PowerShell中使用.NET Framework
- 在 .NET Framework 中表示 XML 的最佳做法! (不错,摘自MSDN)
- .NET Framework 类库 [msdn]
- .NET Framework 类库 [msdn]
- 在objective-C类中声明一个数组型成员变量的property
- 在objective-C类中声明一个数组型成员变量的property
- js中如何声明变量
- MFC数据类型(from MSDN)
- 在select 中声明变量
- SMS脚本节选五:为SMS对象设置安全权限
- 如何在MFC的对话框调用CLR控件(From Msdn)
- COM 组件设计与应用(一)
- SMS脚本节选六:如何使用站点控制文件
- dauctor
- 如何在MFC类中声明一个.Net FrameWork变量--gcroot(from msdn)
- 了解计算机系统启动过程
- 用协议分析工具学习TCP/IP
- 2006年1月5日
- Asp.Net 学习资源列表
- 笑傲HUB.路由器.交换机之教学篇
- 共享文件夹的卷影副本:Q&A
- 大概弄懂了Java Mail的使用方法
- 一个从MS站点转载的各个不同Windows Server 2003版本的特性对照表