Solved

Translating from thread id to module name

Posted on 2004-10-05
11
1,094 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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 …
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

18 Experts available now in Live!

Get 1:1 Help Now