Solved

Getting process name from processid

Posted on 2002-07-16
4
522 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
ID: 7157149
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
ID: 7160888
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
ID: 7160909
weicco,

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

Accepted Solution

by:
weicco earned 50 total points
ID: 7161679
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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 article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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