Link to home
Start Free TrialLog in
Avatar of ibferoz
ibferoz

asked on

Getting process name from processid

How to get process name (.exe) if I have process ID?
Avatar of jkr
jkr
Flag of Germany image

This depends on whether you are using Win9x or NT/W2k - the latter one has 'GetModuleBaseName()'. See the sample code at http://support.microsoft.com/default.aspx?scid=kb;EN-US;q175030 ("HOWTO: Enumerate Applications Using Win32 APIs (Q175030)") on how to do that.
Avatar of weicco
weicco

This should find exe file's name by process id. I didn't test it so I don't know does it actually work.

void GetProcessNameById(DWORD Id, char **buffer) {
  PROCESSENTRY32 processEntry = { 0 };

  HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

  *buffer = NULL;
  processEntry.dwSize = sizeof(PROCESSENTRY32);

  if (Process32First(hSnapShot, &processEntry)) {
    do {
      if (processEntry.th32ProcessID == Id) {
        *buffer = (char *)malloc(strlen(processEntry.szExeFile) + 1);
        strcpy(buffer, processEntry.szExeFile);
        break;
      }
    } while (Process32Next(hSnapShot, &processEntry));
  }
  CloseHandle(hSnapShot);
}
weicco,

that works on win9x only and is also mentioned in the article :o)
ASKER CERTIFIED SOLUTION
Avatar of weicco
weicco

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