Solved

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

Posted on 2000-03-15
6
296 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
Comment Utility
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
Comment Utility
Adjusted points from 20 to 40
0
 

Author Comment

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

How would you draw it?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 22

Accepted Solution

by:
nietod earned 40 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

14 Experts available now in Live!

Get 1:1 Help Now