c++ - GetModuleFileNameEx - Split output -
i trying process name process id, , i've use getmodulefilenameex , write function.
char* processname(ulong_ptr processid) { char szbuffer[max_path+1]; handle hprocess = openprocess(process_query_information | process_vm_read | process_terminate, false, processid); if(getmodulefilenameex(hprocess, null, szbuffer, max_path) == 0) sprintf(szbuffer, "null"); closehandle(hprocess); return szbuffer; }
the output full-path&process-name, , want split can process-name without full-path.
is there way this, or other function can use process name process id?
first: you're returning pointer local memory , going end in tears
char* processname(ulong_ptr processid) { char szbuffer[max_path+1]; ... return szbuffer; }
aside that, can use _splitpath_s() or filename path, or pathfindfilename function available on windows platforms shell api
#include "windows.h" #include "psapi.h" #include "shlwapi.h" #include <string> #include <iostream> #pragma comment(lib, "shlwapi.lib") std::string processname(dword processid) { std::string name; handle handle = openprocess(process_query_information | process_vm_read, false, processid); if (handle) { tchar buffer[max_path]; if (getmodulefilenameex(handle, 0, buffer, max_path)) { name = std::string(pathfindfilename(buffer)); } else { // time call getlasterror() } closehandle(handle); } return name; } int main() { std::cout << processname(getcurrentprocessid()); return 0; }
you can test code on recent msvc compiler here
Comments
Post a Comment