Solved

Getting process name from processid

Posted on 2002-07-16
4
497 Views
Last Modified: 2013-12-03
How to get process name (.exe) if I have process ID?
0
Comment
Question by:ibferoz
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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.
0
 

Expert Comment

by:weicco
Comment Utility
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);
}
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
weicco,

that works on win9x only and is also mentioned in the article :o)
0
 

Accepted Solution

by:
weicco earned 50 total points
Comment Utility
Well.. It worked fine on XP, I just tested it.

I called it like this:

char *buffer;
GetProcessNameById(GetCurrentProcessId(), &buffer);
puts(buffer);

And it outputted test.exe which was correct.



Btw. There's a little bug, change line:

strcpy(buffer, processEntry.szExeFile);

to:

strcpy(*buffer, processEntry.szExeFile);

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
This video discusses moving either the default database or any database to a new volume.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now