Solved

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

Posted on 2000-03-15
6
300 Views
Last Modified: 2013-11-25
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
Comment
Question by:perizi
  • 3
  • 3
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2619980
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
 

Author Comment

by:perizi
ID: 2620291
Adjusted points from 20 to 40
0
 

Author Comment

by:perizi
ID: 2620292
A class which helps preventing leaks from handles that are never closed sounds really useful.

How would you draw it?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Accepted Solution

by:
nietod earned 40 total points
ID: 2620403
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
 

Author Comment

by:perizi
ID: 2635181
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
 
LVL 22

Expert Comment

by:nietod
ID: 2635842
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now