Solved

Translating from thread id to module name

Posted on 2004-10-05
11
1,091 Views
Last Modified: 2013-12-03
What is the easiest way to translate a thread id (lparam in WM_ACTIVATEAPP) into a module (.exe) name?
0
Comment
Question by:rj2
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 150 total points
ID: 12228340
There is no way to do that. But, you cann always call

char acExeName [ MAX_PATH];
GetModuleFileName ( NULL, acExeName, MAX_PATH);

to retrieve the current executable's name. That will also work in a hook, since it is executed in the context of the process that receives that message.
0
 
LVL 8

Assisted Solution

by:mxjijo
mxjijo earned 150 total points
ID: 12228791

Take a look at
http://www.codeproject.com/system/ntenumthreads.asp

On Win 03 there is even easier method (GetProcessIdOfThread()) available to find out the associated process.

0
 
LVL 86

Expert Comment

by:jkr
ID: 12228907
And why would you do that if the module name is available via a single API call?
0
 
LVL 8

Expert Comment

by:mxjijo
ID: 12228995

because it is generic, able to get the exe file name from *any* thread id in the system.
0
 
LVL 10

Author Comment

by:rj2
ID: 12230232
jkr, I'm not sure if I understand, GetModuleFileName ( NULL ... wil only get the current executable's name, that is my executable?
When I get the WM_ACTIVATEAPP I want to find the name of the other application beeing activated or deactivated.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:jkr
ID: 12230241
>>wil only get the current executable's name, that is my executable?

No. The executable that receives that message.
0
 
LVL 10

Author Comment

by:rj2
ID: 12230999
Ok, you mean using GetModuleFileName(NULL,... in a global hook?
0
 
LVL 8

Expert Comment

by:mxjijo
ID: 12231599

When you use global hook, you (your DLL) are apparently inside other processes.
0
 
LVL 4

Accepted Solution

by:
Milind00 earned 200 total points
ID: 12237358
Follow following steps ....

1. Using (lparam in WM_ACTIVATEAPP) call OpenThread which will return you the thread handle
2. Then with the handle call GetProcessIdOfThread
3. Then use following code to get the process name....

#include <windows.h>
#include <stdio.h>
#include "psapi.h"

void PrintProcessNameAndID( DWORD processID )
{
    char szProcessName[MAX_PATH] = "unknown";

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );

    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName,
                               sizeof(szProcessName) );
        }
        else return;
    }
    else return;

    // Print the process name and identifier.

    printf( "%s (Process ID: %u)\n", szProcessName, processID );

    CloseHandle( hProcess );
}

0
 
LVL 8

Expert Comment

by:mxjijo
ID: 12239319

Milind00,
 MSDN says GetProcessIdOfThread() needs 2003 server. Take a look at :
 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocessidofthread.asp

 Also, you may take a look at your  kernel32.dll to see if  GetProcessIdOfThread is available. I have win2k Pro and I don't see that function in the dll.



0
 
LVL 10

Author Comment

by:rj2
ID: 12245491
Too bad that Microsoft did not include GetProcessIdOfThread in earlier versions of Win32.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

17 Experts available now in Live!

Get 1:1 Help Now