How to find Current Process Name
-
lunedì 16 aprile 2012 11:34
I want to find the name of my current process .I have the currentprocessid .
please tell me how to find the name.
Tutte le risposte
-
lunedì 16 aprile 2012 12:06
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.
- Proposto come risposta Helen ZhaoModerator martedì 17 aprile 2012 03:36
- Contrassegnato come risposta Helen ZhaoModerator martedì 24 aprile 2012 07:26
-
lunedì 16 aprile 2012 15:14
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.th32ProcessID,pe32.szExeFile); } while(Process32Next(hSnapshot,&pe32)); } CloseHandle(hSnapshot); } }ThanksRupesh Shukla
- Proposto come risposta Helen ZhaoModerator martedì 17 aprile 2012 03:36
- Contrassegnato come risposta Helen ZhaoModerator martedì 24 aprile 2012 07:26
-
lunedì 16 aprile 2012 16:30
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.- Proposto come risposta Helen ZhaoModerator martedì 17 aprile 2012 03:36
- Contrassegnato come risposta Helen ZhaoModerator martedì 24 aprile 2012 07:26

