Solved

Getting process name from processid

Posted on 2002-07-16
4
534 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error email attachment VB2010 5 54
Issue testing the Class Library 4 65
C# Performance testing with Visual Studio Profession 2015 2 39
WPF issue with Trigger 2 130
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 …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

726 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