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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:02:17+00:00 2026-05-12T17:02:17+00:00

I’ve recently posted a general question about RAII at SO . However, I still

  • 0

I’ve recently posted a general question about RAII at SO.
However, I still have some implementation issues with my HANDLE example.

A HANDLE is typedeffed to void * in windows.h. Therefore, the correct shared_ptr definition needs to be

std::tr1::shared_ptr<void> myHandle (INVALID_HANDLE_VALUE, CloseHandle);

Example 1 CreateToolhelp32Snapshot: returns HANDLE and works.

const std::tr1::shared_ptr<void> h
    (CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL), CloseHandle);

As I use void in the definition (what is the correct way?) problems go on, when I try to call some more winapi commands with this pointer. They functionally work, but are ugly and I am sure that there has to be a better solution.

In the following examples, h is a pointer which was created via the definition at the top.

Example 2 OpenProcessToken: last argument is a PHANDLE. medium ugly with the cast.

OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
    (PHANDLE)&h);

Example 3 Process32First: first argument is a HANDLE. REALLY ugly.

Process32First(*((PHANDLE)&h), &pEntry);

Example 4 simple comparison with a constant HANDLE. REALLY ugly.

if (*((PHANDLE)&h) == INVALID_HANDLE) { /* do something */ }

What is the correct way to create a proper shared_ptr for a HANDLE?

  • 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-12T17:02:18+00:00Added an answer on May 12, 2026 at 5:02 pm

    Example 1 is OK

    Example 2 is wrong. By blindly casting to PHANDLE, the shared_ptr logic is bypassed. It should be something like this instead:

    HANDLE h;
    OpenProcessToken(...., &h);
    shared_ptr<void> safe_h(h, &::CloseHandle);
    

    or, to assign to a pre-exising shared_ptr:

    shared_ptr<void> safe_h = ....
    {
      HANDLE h;
      OpenProcessToken(...., &h);
      safe_h.reset(h, &::CloseHandle);
    }//For extra safety, limit visibility of the naked handle
    

    or, create your own, safe, version of OpenProcessToken that returns a shared handle instead of taking a PHANDLE:

    // Using SharedHandle defined at the end of this post
    SharedHandle OpenProcess(....)
    {
        HANDLE h = INVALID_HANDLE_VALUE;
        ::OpenProcessToken(...., &h);
        return SharedHandle(h);
    }
    

    Example 3: No need to take these detours. This should be ok:

    Process32First(h.get(), ...);
    

    Example 4: Again, no detour:

    if (h.get() == INVALID_HANDLE){...}
    

    To make things nicer, you could typedef something like:

    typedef shared_ptr<void> SharedHandle;
    

    or better yet, if all handles are to be closed with CloseHandle(), create a SharedHandle class wrapping a shared_ptr and automatically providing the right deleter:

    // Warning: Not tested. For illustration purposes only
    class SharedHandle
    {
    public:
      explicit SharedHandle(HANDLE h) : m_Handle(h, &::CloseHandle){};
      HANDLE get()const{return m_Handle.get();}
    
      //Expose other shared_ptr-like methods as needed
      //...
    
    private:
      shared_ptr<void> m_Handle;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.