?
Solved

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

Posted on 2000-03-15
6
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 22

Accepted Solution

by:
nietod earned 120 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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…

770 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