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 107809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:48:45+00:00 2026-05-11T01:48:45+00:00

When working with window handles, is it good enough to use the plain IntPtr

  • 0

When working with window handles, is it good enough to use the plain IntPtr or should I subclass SafeHandle?

Are there any significant pros/cons?

Thanks.

  • 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. 2026-05-11T01:48:45+00:00Added an answer on May 11, 2026 at 1:48 am

    I wouldn’t think that SafeHandle would add any value. MSDN provides the following remarks on the usage of SafeHandles (emphasis mine):

    The SafeHandle class provides protection for handle recycling security attacks; it also provides critical finalization of handle resources. This class allows you to pass to unmanaged code a handle to an unmanaged resource (such as an operating system handle) wrapped in a derived class instance. It provides protection against security attacks by recycling handles. An example of handle recycling is an untrusted user of your handle attempting to queue operations against the resource on one thread while closing the handle on another. The untrusted user would do this in the hope that the handle is reused immediately by some unrelated thread in the process, and the in-process operation returns or alters data that is normally inaccessible to the caller. SafeHandle also provides critical finalization: the ReleaseHandle method is run even when the AppDomain is unloaded by the host, or if normal finalizers block or take too long to clean up.

    Neither of these two goals seems applicable to a window handle returned from a Windows Forms Control object. Handle recycling seems moot given typical usages of a Control‘s Handle property, and the framework and the operating system take any finalization issues out of your hands with window handles.

    Another way to think of it is this: to use SafeHandle, you’d have to provide an implementation for the IsInvalid property and the ReleaseHandle method. The Control.Handle property will never, to my knowledge, return an invalid value, and the window handle will be and should only be ‘released’ when the control is disposed.

    That said, the framework itself does wrap window handles in a HandleRef object when it uses them, which simply protects the control from the garbage collector while the handle is being used by unmanaged code. This is something you’re unlikely to need in the typical scenarios you’d need a window handle, but it takes almost no work to use it, if there’s a possibility you might need it.

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

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You mean like with pragma? #pragma warning( disable : 2400… May 11, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer That's a function of the browser to interpret the link… May 11, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer Don't focus on the number of queries. This is not… May 11, 2026 at 6:18 pm

Related Questions

Doing some work on controlling printing via the System.Printing framework (main classes are PrintQueue,
I'm currently working on cross-platform applications and was just curious as to how other
I'm trying to resize an embedded object. The issue is that when the mouse
My scenario: Windows Forms Application with a base master (mdi) form. An Interface that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.