Share via


How to find Current Process Name

Question

Monday, April 16, 2012 11:34 AM

I want to find the name of my current process .I have the currentprocessid .

please tell me how to find the name.

All replies (3)

Monday, April 16, 2012 12:06 PM ✅Answered

Use GetModuleFileName with NULL first argument to get the full path. Then use _tsplitpath with path and fname non-null arguments to extract only the name.


Monday, April 16, 2012 3:14 PM ✅Answered

There are n no of method .Depend on your application type etc  Like you can use command line argument too and PROCESSENTRY32 etc. Second thing Most of the GetModuleName, QueryFullProcessImage name, etc., require a handle and thus won't be of much use. Toolhelp does return process name . For more detail have a look inside MSDN

void showProcessInformation() {     HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);   if(hSnapshot)   {             PROCESSENTRY32 pe32;           if(Process32First(hSnapshot,&pe32))        {                     do           {                              printf("pid %d %s\n",pe32.th42ProcessID,pe32.szExeFile);                     } while(Process32Next(hSnapshot,&pe32));               }               CloseHandle(hSnapshot);        } } 

Thanks

Rupesh Shukla


Monday, April 16, 2012 4:30 PM ✅Answered | 1 vote

For these functions. It is useful to remember GetCurrentProcess. Besides GetModuleFileName which takes a handle to a module so it can only be run inside the process you want to get the information about, and it explicitly documents that if you pass in NULL for the first argument then you will get the path to the executable, the others takes a handle to a process. When a function says that it takes a handle to a process, you can then use GetCurrentProcess to get a handle that refers to the current process.

So for QueryFullProcessImageName it would be

QueryFullProcessImageName(GetCurrentProcess(), 0, exepath, &exepathlen);

This also ends up faster than tool help too.

This is a signature

Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.