Marshal.FreeHGlobal 方法 (IntPtr)

简介: 释放以前从进程的非托管内存中分配的内存。 命名空间:   System.Runtime.InteropServices程序集:  mscorlib(位于 mscorlib.dll)     下面的示例演示如何将托管的内容转换 String 类写入非托管内存,并因而释放非托管内存完成。

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

命名空间:   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

相关文章
|
4月前
|
缓存 前端开发 JavaScript
componentWillMount()方法有什么用
componentWillMount() 是 React 组件生命周期中的一个方法,在组件首次渲染之前调用。可以用来进行初始化操作,如设置状态或加载数据,但不建议在此方法中执行复杂的异步操作。注意,此方法在 React 16.3 版本后已被标记为不安全,建议使用替代方法。
|
6月前
|
机器学习/深度学习 自然语言处理 API
10-22|处理脏话其他方法
10-22|处理脏话其他方法
|
C语言 C++
求公因数的方法(C/C++)
求公因数的方法(C/C++)
347 0
求公因数的方法(C/C++)
|
存储 JavaScript 前端开发
JavaScript继承的几种方法
JavaScript继承的几种方法
157 0
JavaScript继承的几种方法
|
安全 Java
浅析ThreadList的runcheckpoint方法
浅析ThreadList的runcheckpoint方法
131 0
Egiht(八种方法)
Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it.
1349 0
|
测试技术 C#
分享几个实用的方法
  今天主要和大家分享的是本人总结的分页执行方法,也可以说就是分批执行;该篇采用java8新增的表达式来操作,希望能给各位带来好的帮助和在日常工作中提供便利;同样的操作流程和逻辑之前用C#代码写过一次,有需要的朋友可以看以前的博文; 分页方式拆分List为多个子集List方法 执行统一方法-无...
1229 0
|
程序员 架构师
|
C# 编译器 索引