It is possible to get an IntPtr from an int[] array?

后端 未结 2 460
孤城傲影
孤城傲影 2021-01-17 17:20

Greetings.

In C#: If I have an int[] array declared like this

int[] array = new array[size];

there is an way to get the IntPtr from

相关标签:
2条回答
  • 2021-01-17 18:03

    You should be able to do this without unsafe code using GCHandle. Here is a sample:

    GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
    try
    {
        IntPtr pointer = handle.AddrOfPinnedObject();
    }
    finally
    {
        if (handle.IsAllocated)
        {
            handle.Free();
        }
    }
    
    0 讨论(0)
  • 2021-01-17 18:08

    Use unsafe code, like this:

    unsafe
    {
      fixed (int* pArray = array)
      {
        IntPtr intPtr = new IntPtr((void *) pArray);
      }
    }
    
    0 讨论(0)
提交回复
热议问题