Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Translating from thread id to module name

Posted on 2004-10-05
11
Medium Priority
?
1,158 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 600 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 600 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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 …
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…
Integration Management Part 2

972 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