Skip to content

Consider adding unmanaged pointer overloads for Marshal APIs #75630

@buyaa-n

Description

@buyaa-n

I am wondering whether it would make sense to introduce overloads for PtrToStringUTF8 and similar APIs that take void* to avoid nint casts in these situations.

Originally posted by @jkotas in #75557 (comment)

APIs using IntPtr not included:

  • Marked with EditorBrowsableAttribute(EditorBrowsableState.Never) or Obsolete.
  • APIs where the IntPtr exists only in the return type.
  • Related APIs that would make arguments and/or return types inconsistent (for example, IntPtr ReAllocCoTaskMem(IntPtr pv, int cb) and IntPtr AllocCoTaskMem(int cb)).
namespace System.Runtime.InteropServices
{
    public static partial class Marshal
    {
+        public static unsafe int AddRef(void* pUnk);
+        public static unsafe void Copy(byte[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(char[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(double[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(short[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(int[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(long[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(void* source, byte[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, char[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, double[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, short[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, int[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, long[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, void*[] destination, int startIndex, int length);
+        public static unsafe void Copy(void* source, float[] destination, int startIndex, int length);
+        public static unsafe void Copy(void*[] source, int startIndex, void* destination, int length);
+        public static unsafe void Copy(float[] source, int startIndex, void* destination, int length);
+        [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
+        public static unsafe void* CreateAggregatedObject<T>(void* pOuter, T o) where T : notnull;
+        public static unsafe void DestroyStructure<T>(void* ptr);
+        public static unsafe TDelegate GetDelegateForFunctionPointer<TDelegate>(void* ptr);
+        public static unsafe System.Exception? GetExceptionForHR(int errorCode, void* errorInfo);
+        [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
+        public static unsafe object GetObjectForIUnknown(void* pUnk);
+        [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
+        public static unsafe object GetTypedObjectForIUnknown(void* pUnk, System.Type t);
+        [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
+        public static unsafe object GetUniqueObjectForIUnknown(void* unknown);
+        public static unsafe void InitHandle(SafeHandle safeHandle, IntPtr handle);
+        public static unsafe string? PtrToStringAnsi(void* ptr);
+        public static unsafe string PtrToStringAnsi(void* ptr, int len);
+        public static unsafe string? PtrToStringAuto(void* ptr);
+        public static unsafe string? PtrToStringAuto(void* ptr, int len);
+        public static unsafe string PtrToStringBSTR(void* ptr);
+        public static unsafe string? PtrToStringUni(void* ptr);
+        public static unsafe string PtrToStringUni(void* ptr, int len);
+        public static unsafe string? PtrToStringUTF8(void* ptr);
+        public static unsafe string PtrToStringUTF8(void* ptr, int byteLen);
+        public static unsafe T? PtrToStructure<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]T>(void* ptr);
+        public static unsafe void PtrToStructure<T>(void* ptr, [System.Diagnostics.CodeAnalysis.DisallowNullAttribute] T structure);
+        public static unsafe int QueryInterface(void* pUnk, ref System.Guid iid, out void* ppv);
+        public static unsafe byte ReadByte(void* ptr);
+        public static unsafe byte ReadByte(void* ptr, int ofs);
+        public static unsafe short ReadInt16(void* ptr);
+        public static unsafe short ReadInt16(void* ptr, int ofs);
+        public static unsafe int ReadInt32(void* ptr);
+        public static unsafe int ReadInt32(void* ptr, int ofs);
+        public static unsafe long ReadInt64(void* ptr);
+        public static unsafe long ReadInt64(void* ptr, int ofs);
+        public static unsafe void* ReadIntPtr(void* ptr);
+        public static unsafe void* ReadIntPtr(void* ptr, int ofs);
+        public static unsafe int Release(void* pUnk);
+        public static unsafe void StructureToPtr<T>([System.Diagnostics.CodeAnalysis.DisallowNullAttribute] T structure, void* ptr, bool fDeleteOld);
+        public static unsafe void ThrowExceptionForHR(int errorCode, void* errorInfo);
+        public static unsafe void WriteByte(void* ptr, byte val);
+        public static unsafe void WriteByte(void* ptr, int ofs, byte val);
+        public static unsafe void WriteInt16(void* ptr, char val);
+        public static unsafe void WriteInt16(void* ptr, short val);
+        public static unsafe void WriteInt16(void* ptr, int ofs, char val);
+        public static unsafe void WriteInt16(void* ptr, int ofs, short val);
+        public static unsafe void WriteInt32(void* ptr, int val);
+        public static unsafe void WriteInt32(void* ptr, int ofs, int val);
+        public static unsafe void WriteInt64(void* ptr, int ofs, long val);
+        public static unsafe void WriteInt64(void* ptr, long val);
+        public static unsafe void WriteIntPtr(void* ptr, int ofs, void* val);
+        public static unsafe void WriteIntPtr(void* ptr, void* val);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions