allocating “unmanaged” memory in c#

前端 未结 3 1761
暖寄归人
暖寄归人 2020-12-05 14:10

I\'m writting a program in c# that uses a C++ library, and for some reason I need to allocate an unmanaged buffer to pass it to the lib. Is there a way to do this in c# ? Ba

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 14:30

    Try something like this:

    using System;
    using System.Runtime.InteropServices;
    
    class Example
    {
        static void Main()
        {
            IntPtr pointer = Marshal.AllocHGlobal(1024);
        }
    }
    

    This uses the Marshal.AllocHGlobal method:

    Allocates memory from the unmanaged memory of the process by using the specified number of bytes.

提交回复
热议问题