allocating “unmanaged” memory in c#

前端 未结 3 1769
暖寄归人
暖寄归人 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:35

    This is how we need to assign and free unmanaged memory by using specific number of bytes.

    // Demonstrate how to call GlobalAlloc and 
    // GlobalFree using the Marshal class.
    IntPtr hglobal = Marshal.AllocHGlobal(100);
    Marshal.FreeHGlobal(hglobal)
    

提交回复
热议问题