using System;
using System.Runtime.CompilerServices;
using Vanara.Extensions;
namespace Vanara.PInvoke
{
/// Helper methods for structures.
public static class StructHelper
{
/// Gets the address of a reference.
/// The unmanaged value type to convert.
/// The target value.
/// The address of .
/// This address will only remain valid if the target value or its encapsulating type are pinned.
public static IntPtr DangerousAddressOf(ref T target) where T : unmanaged
{
unsafe
{
fixed (T* p = &target)
return (IntPtr)p;
}
}
/// Converts a field reference to array. Used when a structure defines an ANYSIZE array as the last field.
/// The unmanaged type to convert.
/// A reference to the field value.
/// The number of items in the array.
/// The offset from the field value at which to start extracting the array.
/// An item array of values of .
///
/// Properties defined with this can only be safely used when the structure has been marshaled via a dynamic memory block and not
/// when marshaled directly.
///
#if !(NET20 || NET35 || NET40)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public unsafe static T[] FieldToArray(ref T fieldReference, int count, int offset = 0) where T : unmanaged
{
if (count == 0) return new T[0];
return DangerousAddressOf(ref fieldReference).ToArray(count, offset);
}
}
}