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



Comments

  • How to get bitmap/icon info of a file?

    Posted by Legacy on 10/06/1999 12:00am

    Originally posted by: Pabitra Dash

    Hi,
    
    

    I want to know the icon/bitmap information about a file which is attached to it. When we choose properties by right clicking on a file it shows the file icon. Or in Win NT Exploerer for every file a icon is shown left to the file. This icon/bitmap info
    (ICON handle or Icon ID) I want to access in my program using win32 APIs or MFC.

    Please help.

    Regards,
    Pabitra

    Reply
  • A simplified version for NT and a better one for Win 9x

    Posted by Legacy on 08/24/1999 12:00am

    Originally 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

    ---
    mathias@thinkred123.com - http://www.thinkred.com
    (Remove '123' in my email address to reply)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

  • Employees must exchange sensitive emails with customers and partners. These emails might contain protected health information, protected financial information, or corporate information that should not be made public. Globalscape® Mail Express® allows you to encrypt the emails that it manages so that no one but the sender and recipient--not even the administrator--can view the contents of the email. "Secure the Transfer of Sensitive Emails" is the property of GlobalSCAPE

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date