How to find the ProcessID for a Process.

I want to find the PID (Processor ID) for a given process as in the Task Manger. How to find, what is the API for finding the process,

say I want to find the PID for Cmd.exe.
kid99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akalmaniCommented:
Hope this helps out ..
  First use CreateProces() function to execute any exe that creates a process and then use the GetProcessID() to get the process ID of that process.
 Try this out i have not tried it...
0
akalmaniCommented:
I mistaked in writing it is CreateProcess() with some parameters u wish to pass. There is also Spy++ with which u can get the process ID..
0
kid99Author Commented:
Thank's for your reply Mr.akalmani, but i dont want to use the CreateprocesId and getprocessid,because it will furthere increase the code. Is there any direct or indirect api's for this.
using spi++, how can we do this.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

kid99Author Commented:
Thank's for your reply Mr.akalmani, but i dont want to use the CreateprocesId and getprocessid,because it will furthere increase the code. Is there any direct or indirect api's for this.
using spi++, how can we do this.
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

Use the function _getpid() to get the process ID as

int pID = _getpid();
CString str;
str.Format("%X", pID);
AfxMessageBox(str);


U will get process ID in pID. For this U have to include
#include <process.h>

Hope it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kid99Author Commented:
Hi
_getpid gets the current  process id number and this value gets
changed every time i execute  the program.  This value is not same as that of the TASK MANAGER pid.  I want to find the Pid as it is displayed in the TaskManager for each process.
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

It is the same ID!!!. Here I am displaying it in HEX, but the TASKMGR displays it in decimal. Is that right?. For me it is giving correct values.

Try it out.
0
stefanrCommented:
I think the answer to the question is different depending if you use Windows 9X or Windows NT. Windows 9X uses the ToolHelp32 group of functions to enumerate processes, while Windows NT uses Performance Data to do the same. I use Windows NT, so I have used Performance Data directly, though there nowadays exists a DLL in the Platform SDK that takes care of some of the ugly details unpacking the Performance Data information (PSAPI.DLL).
0
kid99Author Commented:
Hi vinexpert,
I tried to print using decimal format also, but the pid i'm getting is not same as that of the Task manager.  Consider i have written a console based program using getpid, now i want to find the pid for " MSDEV.EXE". How will i execute and find out.please help it out.
0
Vinayak KumbarSr Program ManagerCommented:
Hi kid99,

If this question is still a question to U, give me Ur email ID, I have a sample program which behaves as Task Manager, It may be useful to U.

Cheers,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.