Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>This method implemented as unmanaged function internally. You can download <a href="http://www.microsoft.com/download/en/details.aspx?id=4917" rel="nofollow">Shared Source Common Language</a> and take a look at that method implementation.</li> </ol> <p>Here the code that I found in safechandle.cpp file:</p> <pre><code>FCIMPL2(void, SafeHandle::DangerousAddRef, SafeHandle* refThisUNSAFE, CLR_BOOL *pfSuccess) { CONTRACTL { THROWS; MODE_COOPERATIVE; DISABLED(GC_TRIGGERS); SO_TOLERANT; } CONTRACTL_END; SAFEHANDLEREF sh(refThisUNSAFE); HELPER_METHOD_FRAME_BEGIN_1(sh); if (pfSuccess == NULL) COMPlusThrow(kNullReferenceException); sh-&gt;AddRef(); *pfSuccess = TRUE; HELPER_METHOD_FRAME_END(); } FCIMPLEND </code></pre> <p>But I'm not sure is it usefull or not.</p> <hr> <p>And now about second question.</p> <p>During marshalling from managed code to native code object marshallel converts any <code>SafeHandle</code> to <code>IntPtr</code> by calling <code>DangerousGetHandle</code> method.</p> <p>During unmarshalling from native code to managed code there is opposed conversion: any <code>IntPtr</code> returns as <code>SafeHandle</code>.</p> <p>When you're calling any unmanaged code that takes <code>DWORD</code> or <code>PVOID</code> we could pass <code>SafeHandle</code> or one of its descendants.</p> <p>For example if we have a couple of external functions in our umanaged DLL:</p> <pre><code>PVOID CreateCustomHandle(); void ReleaseCustomHandle(PVOID handle); </code></pre> <p>We could call them in following way:</p> <pre><code>[DllImport("kernel32")] public static extern MySafeHandle CreateCustomHandle(); [DLLImport("kernel32")] public static extern void ReleaseCustomHandle(MySafeHandle handle); </code></pre> <p>Where <code>MySafeHandle</code> is subclass of <code>SafeHandle</code> class that knows how to deal with that particular resource.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload