Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6134393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:20:59+00:00 2026-05-23T17:20:59+00:00

Could anyone answer on these questions? 1) There is a Microsoft’s class: SafeHandle.cs I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:21:00+00:00Added an answer on May 23, 2026 at 5:21 pm
    1. This method implemented as unmanaged function internally. You can download Shared Source Common Language and take a look at that method implementation.

    Here the code that I found in safechandle.cpp file:

    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->AddRef();
        *pfSuccess = TRUE;
    
        HELPER_METHOD_FRAME_END();
    }
    FCIMPLEND
    

    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 SafeHandle to IntPtr by calling DangerousGetHandle method.

    During unmarshalling from native code to managed code there is opposed conversion: any IntPtr returns as SafeHandle.

    When you’re calling any unmanaged code that takes DWORD or PVOID we could pass SafeHandle or one of its descendants.

    For example if we have a couple of external functions in our umanaged DLL:

    PVOID CreateCustomHandle();
    void ReleaseCustomHandle(PVOID handle);
    

    We could call them in following way:

    [DllImport("kernel32")]
    public static extern MySafeHandle CreateCustomHandle();
    
    [DLLImport("kernel32")]
    public static extern void ReleaseCustomHandle(MySafeHandle handle);
    

    Where MySafeHandle is subclass of SafeHandle class that knows how to deal with that particular resource.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone suggest a good packet sniffer class for c++? Looking for a easy
Could anyone explain with some examples when it is better to call functions by
Could anyone could point me to some code/give me ideas on how to create
Could anyone recommend a good BAML Decompiler / Viewer besides BAML Viewer plugin for
Could anyone explain me why: function doAjax() { var xmlHttpReq = false; try {
Could anyone can tell me where I can find full ASP.NET MVC beta documentation?
Could anyone point me to a resource that explains WCF with pictures and simple
C#: Could anyone give me good example on how anchoring controls at runtime is
ASP.NET: Could anyone provide some C# paging codes in Repeater or ListView controls for
I need to get substed drive letter in Perl. Could anyone kindly help me?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.