public static IReadOnlyList <string> UnpackListString(PackedValue packedValue) { List <string> result = new List <string>(packedValue.Size); if (packedValue.Type == (PackedValueType.SimpleArray & PackedValueType.NarrowString)) { for (IntPtr cursor = packedValue.Pointer; cursor != packedValue.Pointer + (packedValue.Size * IntPtrSize); cursor += IntPtrSize) { IntPtr handle = Marshal.ReadIntPtr(cursor); result.Add(Marshal.PtrToStringAnsi(packedValue.Pointer)); } } else if (packedValue.Type == (PackedValueType.SimpleArray & PackedValueType.WideString)) { for (IntPtr cursor = packedValue.Pointer; cursor != packedValue.Pointer + (packedValue.Size * IntPtrSize); cursor += IntPtrSize) { IntPtr handle = Marshal.ReadIntPtr(cursor); result.Add(Marshal.PtrToStringUni(packedValue.Pointer)); } } else { throw new ArgumentException("UnpackListString expects a 'NarrowString' or 'WideString' type."); } return(result); }
public static IReadOnlyList <T> UnpackList <T>(PackedValue packedValue, Func <IntPtr, T> factory) { Debug.Assert(packedValue.Type == (PackedValueType.SimpleArray & PackedValueType.Pointer)); List <T> result = new List <T>(packedValue.Size); for (IntPtr cursor = packedValue.Pointer; cursor != packedValue.Pointer + (packedValue.Size * IntPtrSize); cursor += IntPtrSize) { IntPtr handle = Marshal.ReadIntPtr(cursor); result.Add(factory(handle)); } return(result); }
public static string UnpackString(PackedValue packedValue) { switch (packedValue.Type) { case PackedValueType.NarrowString: return(Marshal.PtrToStringAnsi(packedValue.Pointer, packedValue.Size)); case PackedValueType.WideString: return(Marshal.PtrToStringUni(packedValue.Pointer, packedValue.Size)); default: throw new ArgumentException("UnpackString expects a 'NarrowString' or 'WideString' type."); } }