1 " Psapi.h " 2 #pragma Comment (lib, "Psapi.lib")
Need to include header file Oh!
1 /*function: Gets the full path of the executable (EXE) file corresponding to the specified process2 * Parameter: hprocess-process handle. Must have process_query_information or3 process_query_limited_information Permissions4 * Sfilepath-the path of the executable file corresponding to the process handle hprocess5 * return value:6 */7 voidGetprocessfilepath (in HANDLE hprocess, out cstring&Sfilepath)8 {9Sfilepath = _t ("");TenTCHAR Tsfiledospath[max_path +1]; OneZeroMemory (Tsfiledospath,sizeof(TCHAR) * (MAX_PATH +1)); A if(0= = Getprocessimagefilename (hprocess, Tsfiledospath, MAX_PATH +1)) - { - return; the } - - //get logic Drive string length -UINT Uilen = GetLogicalDriveStrings (0, NULL); + if(0==Uilen) - { + return; A } at -Ptstr plogicdrivestring =NewTchar[uilen +1]; -ZeroMemory (plogicdrivestring, Uilen +1); -Uilen =getlogicaldrivestrings (Uilen, plogicdrivestring); - if(0==Uilen) - { in Delete[]plogicdrivestring; - return; to } + -TCHAR szdrive[3] = TEXT (" :"); thePtstr Pdosdrivename =NewTchar[max_path]; *Ptstr Plogicindex =plogicdrivestring; $ Panax Notoginseng Do - { theszdrive[0] = *Plogicindex; +Uilen =Querydosdevice (szdrive, Pdosdrivename, MAX_PATH); A if(0==Uilen) the { + if(Error_insufficient_buffer! =GetLastError ()) - { $ Break; $ } - - Delete[]pdosdrivename; thePdosdrivename =NewTchar[uilen +1]; -Uilen = Querydosdevice (szdrive, pdosdrivename, Uilen +1);Wuyi if(0==Uilen) the { - Break; Wu } - } About $Uilen =_tcslen (pdosdrivename); - if(0==_tcsnicmp (Tsfiledospath, Pdosdrivename, Uilen)) - { -Sfilepath.format (_t ("%s%s"), szdrive, Tsfiledospath +Uilen); A Break; + } the - while(*plogicindex++); $} while(*plogicindex); the the Delete[]plogicdrivestring; the Delete[]pdosdrivename; the}
Gets the full path (code) of the executable (EXE) file corresponding to the specified process