[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Process handles enumeration and nature (VC++, NT no MFC)

How can a thread easily know what handles its process has and what kind of handles they are (thread handle, sempahore handle, file handle..)?

Is the use of PDH (Performance Data Helper) calls the most simple way?

I'm working in Windows NT, programming a component with VC++, using ATL, and no MFC.
0
perizi
Asked:
perizi
  • 3
  • 3
1 Solution
 
nietodCommented:
There is no way provided by the windows API to track your handle usage.

You need do so yourself.  You can write classes to help you in this.  These classes may also good at preventing leaks from handles that are never closed.
0
 
periziAuthor Commented:
Adjusted points from 20 to 40
0
 
periziAuthor Commented:
A class which helps preventing leaks from handles that are never closed sounds really useful.

How would you draw it?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
nietodCommented:
I don't have a single class that works for all types of handles.  (in fact you can't as different handles have different ways to be destroyed).  but I've developed some classes for dealing with different types of handles.

The design I often use uses reference counting to allow the handle to be passed around in the program and closes the handle when no parts fo the program have references to it any longer.  For this have a reference coutned class like

class RefCntCls
{
   HANDLE Hnd;
   int UseCnt; // reference count.
   RefCntCls(const RefCntCls &); // Prevent copying.
   operator = (const RefCntCls &); // Prevent assignment.
public:
   RefCntCls(HANDLE h) : Hnd(h),UseCnt(1) {};
    // This needs to be changed for handles that don't
    // get closed  in this way
    // like GDI object handles need DeleteObkect()
   ~RefCntCls() {  CloseHandle(Hnd); };
    void IncUseCnt() { ++UseCnt; };
    // Deletes the object and closes the handle when use count
   // returns to 0.
    void DecUseCnt() { if (UseCnt-- <= 0) delete this; };
};

then you need a class that manipulates the reference counted objects like

class HndCls
{
   RefCntCls *HndPtr;
public:
   ~HndPtr() { HndPtr->DecUseCnt(); };
   HndCls(HANDLE Hnd) : HndPtr(new RefCntCls(Hnd)) {};
   HndCls(const HndCls &Src) : HndPtr(Src.HndPtr) { HndPtr->IncUseCnt(); };
   HndCls &operator =(const HndCls &Src)
   {
      Src.HndPtr->IncUseCnt();
      HndPtr->DecUseCnt();
      HndPtr = Src.HndPtr;
      return *this;
   };
   HANDLE operator *() const { return HndPtr->Hnd; };
};

This design  is the sort of skeleton of the skeleton of what you woudl want.  it has all the important properties.  As long as a HndCls object exists the handle it matains stays open.  When the last HndCls object that uses a particular handle is destroyed, the handle is closed.   It can use imbellishments, like cosntructors that take parameters that create the handle and other features like that.
0
 
periziAuthor Commented:
Thanks NIETOD for the useful code.
However, it sounds strange to me why there are no API functions to get one's used handles.
It would be so useful at debbug time!

Thanks again.
0
 
nietodCommented:
Not really.  it would be just as useful as function that returned your dynamically allocted pointers.  What would you do with those pointers?  You don't know what they point to.  The only useful thing you could use it for is to count the handles to see if there are leaks.  use the above approach and avoid the memory leaks in the first place.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now