释放以前从进程的非托管内存中分配的内存。

命名空间:   System.Runtime.InteropServices
程序集:  mscorlib(位于 mscorlib.dll)

下面的示例演示如何将托管的内容转换 String 类写入非托管内存,并因而释放非托管内存完成。

using System;
using System.Runtime.InteropServices; class MainFunction
{
static void Main()
{
Console.WriteLine("\nStringToGlobalAnsi\n"); // Create a managed string.
String managedString = "I am a managed String";
Console.WriteLine("1) managedString = " + managedString ); // Marshal the managed string to unmanaged memory.
IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(managedString);
Console.WriteLine("2) stringPointer = {0}", stringPointer ); // Get the string back from unmanaged memory
String RetrievedString = Marshal.PtrToStringAnsi( stringPointer);
Console.WriteLine("3) Retrieved from unmanaged memory = " + RetrievedString ); // Always free the unmanaged string.
Marshal.FreeHGlobal(stringPointer); // IntPtr handle value is still the same:
Console.WriteLine("4) stringPointer = " + stringPointer ); // However, it contains no data after being freed:
String RetrievedString2 = Marshal.PtrToStringAnsi( stringPointer);
Console.WriteLine("5) RetrievedString2 = " + RetrievedString2 );
}
}

  

https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal.freehglobal(v=vs.110).aspx

Marshal.FreeHGlobal 方法 (IntPtr)的更多相关文章

  1. C#——Marshal.StructureToPtr方法简介

    目录 MarshalStructureToPtr方法简介 功能及位置 语法 参数说明 异常 备注 举例 本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三 ...

  2. c#中Marshal.Copy()方法的使用

    c#中Marshal.Copy方法的使用 Marshal.copy()方法用来在托管对象(数组)和非托管对象(IntPtr)之间进行内容的复制 函数有很多重载,如下所示: Copy(array< ...

  3. 【转】Graphics.DrawImage 方法 IntPtr 结构 GDI 句柄 知识收集

    Graphics.DrawImage 方法 在指定的位置使用原始物理大小绘制指定的 Image. 命名空间:System.Drawing 程序集:System.Drawing(在 system.dra ...

  4. C#中的Marshal

    Const.MaxLengthOfBufferd的长度固定为0x2000   也就是8192 private bool SendMessage(int messageType, string ip, ...

  5. 自己积累的一些Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)

    using System; using System.Drawing; using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; na ...

  6. OpenCv,EmguCv及.net之间的互动(The Interaction of OpenCv, EmguCv AND .net)

    http://www.cnblogs.com/xrwang/archive/2010/01/26/TheInteractionOfOpenCv-EmguCvANDDotNet.html 前言 在.ne ...

  7. VB.NET 内存指针和非托管内存的应用

    介绍 Visual Basic 从来不像在C或C++里一样灵活的操纵指针和原始内存.然而利用.NET框架中的structures 和 classes,可以做许多类似的事情.它们包括 IntPtr,   ...

  8. 在VS2010上使用C#调用非托管C++生成的DLL文件

    背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用.本篇文章将引导你快速理解这个调用 ...

  9. 在C++中使用C#编写的类2

    在那篇<在C#中使用C++编写的类>中我介绍了如何在C#中使用C++编写的类.可是由于C#在用户界面设计.数据库存储和XML文件读取等方面的优势,有时候也会出现要在C++中使用C#编写的类 ...

随机推荐

  1. L175 Endorestiform Nucleus: Scientist Just Discovered a New Part of the Human Brain

    The newly named Endorestiform Nucleus sits in the inferior cerebellar小脑 peduncle, at the junction be ...

  2. 记录一些js框架用途

    accounting.min.js 货币格式化alertify.min.js 提示信息库amd.loader.js 按需动态加载js文件angular-cookies.js 处理cookieangul ...

  3. ubuntu16切换hosts软件安装

    之前用ubuntu切换hosts以为没有软件可用,直接用cp来替换hosts文件,今天网上搜了一下发现一个软件和window上用的切hosts功能一样,而且可以支持linux,mac,windows. ...

  4. PAT 1007 素数对猜想 C语言

    让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷多对相邻且差为2的素数”. 现给定任意正 ...

  5. Cocos2d-x调用Java 代码

    Java代码: package com.dishu; import com.dishu.org.R; import android.app.Activity; import android.app.A ...

  6. Javascript 中 switch case 等于 (== )还是 恒等于(===)?

    Javascript 中 switch case 等于 (== )还是 恒等于(===)? 可以测试一下以下代码,这个 case 中是 等于(==)还是恒等于(===) <script> ...

  7. Git Flow分支策略

    就像代码需要代码规范一样,代码管理同样需要一个清晰的流程和规范 Vincent Driessen 同学为了解决这个问题提出了 A Successful Git Branching Model 下面是G ...

  8. CentOS 6.6下 BCM4312 802.11b/g无线网卡驱动安装

    1.目前www.broadcom.com网站上最新版本为hybrid-v35,但此版本与2.6.32不匹配,无法识别验证密码,搜索网上说是要求升级内核,后根据http://www.dadclab.co ...

  9. GNU Radio: Overview of the GNU Radio Scheduler

    Scetion 1: The Flowgraph The flowgraph moves data from sources into sinks. 一个流图由多个模块组成,其中一般包括信源(Sour ...

  10. Android中Parcelable与Serializable接口用法

    转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...