Link to home
Start Free TrialLog in
Avatar of shavit
shavit

asked on

how to get the process name from the PID? (winapi C++ visual C++ 6.0)

how to get the process name from the PID? (winapi C++ visual C++ 6.0)

I need to translate process id (PID) to process name (such as "notepad.exe")

any idea?

ps

execution speed is also important
Avatar of jkr
jkr
Flag of Germany image

You can use

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

void PrintProcessNameAndID( DWORD processID )
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<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)/sizeof(TCHAR) );
        }
    }

    // Print the process name and identifier.

    _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

    CloseHandle( hProcess );
}

See also the complete code at http://msdn2.microsoft.com/en-us/library/ms682623.aspx ("Enumerating All Processes")
Avatar of shavit
shavit

ASKER

jkr, i have the pid and need to extract to process name. your solution gives the process handle...

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

should i now use this handle to find the exe name?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shavit

ASKER

Thanks
Why not simply use GetProcessImageFileName?
Like:
         
 
HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID);
 
    if (NULL != hProcess )
        ::GetProcessImageFileName(hProcess, szProcessName, MAX_PATH);

Open in new window

Avatar of shavit

ASKER

I used GetModuleBaseName which is very similar to GetProcessImageFileName.
I know these two methods (GetModuleBaseName & GetProcessImageFileName) are not "that" similar. I was trying to use the first approach to find the process name of notepad.exe only from its pid. But on my Windows 7 x64 machine, the 'EnumProcessModules' returned 0 somehow, with the error code ERROR_PARTIAL_COPY(Only part of a ReadProcessMemory or WriteProcessMemory request was completed.) But the GetProcessImageFileName works fine :) .. Since I don't have time to find out why is that partial error coming, I'm going with the latter one.

Hope this helps someone out there.