Solved

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

Posted on 2000-03-15
6
301 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 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