Could anyone answer on these questions?
1) There is a Microsoft’s class: SafeHandle.cs I looked into the source and there are such methods:
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern void DangerousAddRef(ref bool success);
or
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern void DangerousRelease();
Where are the definitions of these method? Where Can I find them?
2) There is a method definitions which execute a method from the system library.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), SuppressUnmanagedCodeSecurity, DllImport("kernel32.dll", EntryPoint="WaitForSingleObject", SetLastError=true, ExactSpelling=true)]
private static extern int WaitForSingleObjectDontCallThis(SafeWaitHandle handle, int timeout);
Normally method: WaitForSingleObject accepts (HANDLE and DWORD).
How .net know how to obtain handle from the SafeWaitHandle class and how he does it?
Here the code that I found in safechandle.cpp file:
But I’m not sure is it usefull or not.
And now about second question.
During marshalling from managed code to native code object marshallel converts any
SafeHandletoIntPtrby callingDangerousGetHandlemethod.During unmarshalling from native code to managed code there is opposed conversion: any
IntPtrreturns asSafeHandle.When you’re calling any unmanaged code that takes
DWORDorPVOIDwe could passSafeHandleor one of its descendants.For example if we have a couple of external functions in our umanaged DLL:
We could call them in following way:
Where
MySafeHandleis subclass ofSafeHandleclass that knows how to deal with that particular resource.