Getting The Filename Given A Window Handle
Posted
by Mihai Filimon
on August 6th, 1998
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");
}

Comments
How to get bitmap/icon info of a file?
Posted by Legacy on 10/06/1999 12:00amOriginally posted by: Pabitra Dash
ReplyA simplified version for NT and a better one for Win 9x
Posted by Legacy on 08/24/1999 12:00amOriginally posted by: Mathias Roth
Hi all,
under NT you can make good use of the GetModuleFileNameEx
function in the PSAPI.dll, which does exactly that, what the
old GetModuleFileName did in Win 3.1 .
For Win 9x, it is my experience that the use of
GetModuleFileName gives you not always the path name of
the main executable (why?). AFAIK the only reliable
way is here to loop over all processes (using the ToolHelp functions) until you find the process ID that matches the
one of your window.
For both approaches you can find all the coding you
basically need in the KB Article #Q175030 (which deals
with enumerationg processes, but can be easily adapted).
Here is the link:
http://support.microsoft.com/support/kb/articles/Q175/0/30.asp
Have fun, Mathias
---
Replymathias@thinkred123.com - http://www.thinkred.com
(Remove '123' in my email address to reply)