mirror of https://github.com/dahall/Vanara.git
Added string handling to all Convert methods
parent
dc83d5d0b4
commit
178701d505
|
@ -14,23 +14,33 @@ namespace Vanara.InteropServices
|
||||||
/// <typeparam name="T">The destination type.</typeparam>
|
/// <typeparam name="T">The destination type.</typeparam>
|
||||||
/// <param name="ptr">The pointer to a block of memory.</param>
|
/// <param name="ptr">The pointer to a block of memory.</param>
|
||||||
/// <param name="sz">The size of the allocated memory block.</param>
|
/// <param name="sz">The size of the allocated memory block.</param>
|
||||||
|
/// <param name="charSet">The character set.</param>
|
||||||
/// <returns>A value of the type specified.</returns>
|
/// <returns>A value of the type specified.</returns>
|
||||||
public static T Convert<T>(this IntPtr ptr, uint sz) => (T)Convert(ptr, sz, typeof(T));
|
public static T Convert<T>(this IntPtr ptr, uint sz, CharSet charSet = CharSet.Auto) => (T)Convert(ptr, sz, typeof(T), charSet);
|
||||||
|
|
||||||
/// <summary>Converts the specified pointer to type specified in <paramref name="destType" />.</summary>
|
/// <summary>Converts the specified pointer to type specified in <paramref name="destType" />.</summary>
|
||||||
/// <param name="ptr">The pointer to a block of memory.</param>
|
/// <param name="ptr">The pointer to a block of memory.</param>
|
||||||
/// <param name="sz">The size of the allocated memory block.</param>
|
/// <param name="sz">The size of the allocated memory block.</param>
|
||||||
/// <param name="destType">The destination type.</param>
|
/// <param name="destType">The destination type.</param>
|
||||||
|
/// <param name="charSet">The character set.</param>
|
||||||
/// <returns>A value of the type specified.</returns>
|
/// <returns>A value of the type specified.</returns>
|
||||||
|
/// <exception cref="ArgumentException">
|
||||||
|
/// Cannot convert a null pointer. - ptr
|
||||||
|
/// or
|
||||||
|
/// Cannot convert a pointer with no Size. - sz
|
||||||
|
/// </exception>
|
||||||
/// <exception cref="NotSupportedException">Thrown if type cannot be converted from memory.</exception>
|
/// <exception cref="NotSupportedException">Thrown if type cannot be converted from memory.</exception>
|
||||||
public static object Convert(this IntPtr ptr, uint sz, Type destType)
|
/// <exception cref="OutOfMemoryException"></exception>
|
||||||
|
public static object Convert(this IntPtr ptr, uint sz, Type destType, CharSet charSet = CharSet.Auto)
|
||||||
{
|
{
|
||||||
if (ptr == IntPtr.Zero) throw new ArgumentException("Cannot convert a null pointer.", nameof(ptr));
|
if (ptr == IntPtr.Zero) throw new ArgumentException("Cannot convert a null pointer.", nameof(ptr));
|
||||||
if (sz == 0) throw new ArgumentException("Cannot convert a pointer with no Size.", nameof(sz));
|
if (sz == 0) throw new ArgumentException("Cannot convert a pointer with no Size.", nameof(sz));
|
||||||
|
|
||||||
// Handle byte array as special case
|
// Handle byte array and pointer as special cases
|
||||||
if (destType.IsArray && destType.GetElementType() == typeof(byte))
|
if (destType.IsArray && destType.GetElementType() == typeof(byte))
|
||||||
return ptr.ToArray<byte>((int)sz);
|
return ptr.ToArray<byte>((int)sz);
|
||||||
|
if (destType == typeof(IntPtr))
|
||||||
|
return Marshal.ReadIntPtr(ptr);
|
||||||
|
|
||||||
var typeCode = Type.GetTypeCode(destType);
|
var typeCode = Type.GetTypeCode(destType);
|
||||||
switch (typeCode)
|
switch (typeCode)
|
||||||
|
@ -86,6 +96,9 @@ namespace Vanara.InteropServices
|
||||||
case TypeCode.DateTime:
|
case TypeCode.DateTime:
|
||||||
return DateTime.FromBinary((long)GetBlittable(typeof(long)));
|
return DateTime.FromBinary((long)GetBlittable(typeof(long)));
|
||||||
|
|
||||||
|
case TypeCode.String:
|
||||||
|
return StringHelper.GetString(ptr, charSet, sz);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new NotSupportedException("Unsupported type parameter.");
|
throw new NotSupportedException("Unsupported type parameter.");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue