Solved

Translating from thread id to module name

Posted on 2004-10-05
11
1,125 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DB Mail Export to Excel some row wrap to new row 30 143
How to update List item Managed Metadata Column with JSOM 2 292
Trouble with References... 5 67
VS2015 Redefinition errors 4 102
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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