-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding unmanaged pointer overloads for Marshal
APIs
#75630
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/interop-contrib Issue DetailsI am wondering whether it would make sense to introduce overloads for PtrToStringUTF8 and similar APIs that take Originally posted by @jkotas in #75557 (comment)
|
Marshal
APIs
namespace System.Runtime.InteropServices;
public partial class Marshal
{
public static string PtrToStringAnsi(void* str);
public static string PtrToStringAnsi(void* str, int count);
public static string PtrToStringAuto(void* str);
public static string PtrToStringAuto(void* str, int count);
public static string PtrToStringBSTR(void* str);
public static string PtrToStringUni(void* str);
public static string PtrToStringUni(void* str, int count);
public static string PtrToStringUTF8(void* str);
public static string PtrToStringUTF8(void* str, int byteLength);
} |
We have better equivalents elsewhere (e.g. direct unsafe pointer operations,
Should the argument type be APIs that I find missing in the list:
|
That is going to be part of the API review. I simply copied all APIs on
Yep. This was intentionally skipped. I updated the issue with those details. APIs that can't be updated to match a pair weren't. For example, |
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 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);
}
} |
Shouldn't |
I am wondering whether it would make sense to introduce overloads for PtrToStringUTF8 and similar APIs that take
void*
to avoidnint
casts in these situations.Originally posted by @jkotas in #75557 (comment)
APIs using
IntPtr
not included:EditorBrowsableAttribute(EditorBrowsableState.Never)
orObsolete
.IntPtr
exists only in the return type.IntPtr ReAllocCoTaskMem(IntPtr pv, int cb)
andIntPtr AllocCoTaskMem(int cb)
).The text was updated successfully, but these errors were encountered: