Solved

Translating from thread id to module name

Posted on 2004-10-05
11
1,101 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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