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

  • Home
  • SEARCH
  • 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 provide me with the pointers to source code for linux commands such
Could anyone please let me know if there is a way to programatically determine
Could anyone recommend me a good open source .Net based wiki project? Better with
Could anyone guide me in created a simple app in JS? What the app
Could anyone explain to me in noob way what the difference is betweeen ImageIcon
Could anyone give me some help with this please? The content needs to appear
could anyone please clarify the meaning of line generalizes the tag object's storage of
could anyone assist me with this. I need to compare two beefy xml files
could anyone tell me the difference between Terminal and non-terminal symbol in the case
Could anyone please tell me why the following line about filter init method invocation

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.