Solved

Getting process name from processid

Posted on 2002-07-16
4
544 Views
Last Modified: 2013-12-03
How to get process name (.exe) if I have process ID?
0
Comment
Question by:ibferoz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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