Add marshalling support for CriticalHandle
We need to port CriticalHandle and HandleRef to CoreLib in order to have P/Invoke support for them.
Do you know if any progress is made or planned in this issue?
Both classes are already ported to CoreRT, but the marshalling code generator in the CoreRT compiler doesn't handle them.
I think we can repurpose this issue to track implementing this in the CoreRT compiler. (Cc @shrah)
@farkasmark Are you asking about support for this in the CoreRT compiler?
@MichalStrehovsky Yes, I am asking about the CoreRT compiler support. I can build the solution successfully but I get a runtime exception from the natively compiled application.
@farkasmark I should be able to work on the marshalling support for CriticalHandle and HandleRef around mid July if that works for you.
@shrah That would be perfect! Let me know if I can assist you in that! (in testing for example)
@shrah May I ask if you could have made any progress on this matter? Thanks a lot!
Does anyone have any idea how to get started with this? Thanks!
@farkasmark I apologize for not getting to you sooner. With shipping .NET Native and parental leave I couldn't get to it in time.
If you want to work on it, you can start by looking into the corresponding CoreCLR implementation here https://github.com/dotnet/coreclr/blob/52a816d3011f4d03b80b5940dc036b40d701f52d/src/vm/ilmarshalers.cpp#L3002
@shrah Congratulations! :) No problem, I try to make it work based on the corresponding CoreCLR implementation you have given! Thanks for the help and I get back to you if I stuck.
This should not be hard - marking up-for-grabs.
-
For CriticalHandle:
- Start by creating clone of SafeHandleMarshaller as CriticalHandleMarshaller in https://github.com/dotnet/corert/blob/master/src/Common/src/TypeSystem/Interop/IL/Marshaller.cs. Alternatively, add a bool argument to SafeHandleMarshaller that says whether it is marshalling safehandle or critical handle.
- Delete code that deals with DangerousAddRef/DangerousRelease in CriticalHandleMarshaller
- Add a test to https://github.com/dotnet/corert/tree/master/tests/src/Simple/PInvoke to verify that it works
-
For HandleRef:
- Start by creating clone of DelegateMarshaller as HandleRefMarshaller in https://github.com/dotnet/corert/blob/master/src/Common/src/TypeSystem/Interop/IL/Marshaller.cs.
- Change AllocAndTransformManagedToNative to fetch _handle field from HandleRef, change EmitCleanupManaged to call KeepAlive on the _object field, and change TransformNativeToManaged to throw since native->managed direction is not supported for HandleRefs.
- Add a test to - https://github.com/dotnet/corert/tree/master/tests/src/Simple/PInvoke to verify that it works
HandleRef is done in #6395. What's left is CriticalHandle. It shouldn't be hard to implement based on the information in this issue, so that's still up for grabs.