Add WindowsRuntimeMarshal.TryGetDataUnsafe and use it in various places.#1556
Conversation
9dadf5a to
bdb090a
Compare
bdb090a to
ef7a2c3
Compare
4ce4fb2 to
db6c137
Compare
dec5ec2 to
c3bb701
Compare
|
D:\a_work\1\s\src\Projections\Windows\Generated Files\net8.0\CsWinRT\Windows.Storage.Streams.cs(8700,37): error CS0234: The type or namespace name 'IBufferByteAccessMethods' does not exist in the namespace 'ABI.Windows.Storage.Streams' (are you missing an assembly reference?) [D:\a_work\1\s\src\Projections\Windows\Windows.csproj::TargetFramework=net8.0] |
9683419 to
16f72ab
Compare
| if (ComWrappersSupport.TryUnwrapObject(buffer, out var unwrapped) && | ||
| unwrapped.TryAs(global::WinRT.Interop.IID.IID_IBufferByteAccess, out IntPtr ThisPtr) >= 0) | ||
| { | ||
| try |
There was a problem hiding this comment.
We could optimize this to not need this try/finally:
IntPtr __retval;
int hresult = (*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)ThisPtr)[3](ThisPtr, &__retval);
dataPtr = __retval;
Marshal.Release(ThisPtr);
global::WinRT.ExceptionHelpers.ThrowExceptionForHR(hresult);
return true;But also, why are we throwing in the first place here, given the method is named "Try"?
There was a problem hiding this comment.
Try doesn't mean we never throw. Try here means we are not throwing if the type doesn't implement IBufferByteAccess. If it does implement it but the implementation is bad we still throw.
There was a problem hiding this comment.
I'm not entirely sure that's intuitive, but I suppose it's fine. Can we drop the try/finally though?
There was a problem hiding this comment.
I don't think it's an optimization though.
try/finally doesn't have performance impact for fast path (no exception being thrown) and it can make sure the finally block being executed once there's an exception. You can check the codegen, unless you don't have catch blocks in a try, it shouldn't make any meaningful impact on the performance.
Allows unsafe access to the underlying buffer data.