Getting The Filename Given A Window Handle

I consulted article Article ID: Q119163 from Knowledge Base, and I think
that it’s too complex code. Therefore I designed and implemented three
classes for this: CPerfObject, CPerfInstance and CPerfCounter. Here the
code for solve the subject problem, of course with help of classes
called before. Classes can be improved. Function GetNameProcess can be
easly transform to give all thread from one process by searching object
“Thread” and give all instances for given “ID Process”.

I propose hWnd window for which I must to found the process file name.

CString GetNameProcess(HWND hWnd)
{
DWORD pID = NULL;
GetWindowThreadProcessId(hWnd, &pID);
CPerfObject objects(_T("Process"));
objects.Open();
int i = NULL;
if (CPerfCounter* pCounter = objects.GetCounter(_T("ID Process"), i))
{
 int nInstances = objects.GetNoInstances();
 for (i = 0; i < nInstances; i++)
  if (pCounter->GetValue(objects.GetInstance(i)) == pID)
   return objects.GetInstance(i)->GetName();
 }
 return _T("unknown");
}

Download source – 5KB

More by Author

Must Read