@@ -377,8 +377,33 @@ std::optional<std::wstring> GetUserNameFromProcess(DWORD id)
377377// Permalink: https://stackoverflow.com/a/73242956
378378// Thanks!
379379
380+ std::string GetProcessNameFromPid (DWORD pid) {
381+ HANDLE snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS , 0 );
382+ if (snapshot == INVALID_HANDLE_VALUE ) {
383+ return " " ; // vroken
384+ }
385+
386+ PROCESSENTRY32 pe{};
387+ pe.dwSize = sizeof (PROCESSENTRY32 );
388+
389+ if (Process32First (snapshot, &pe)) {
390+ do {
391+ if (pe.th32ProcessID == pid) {
392+ CloseHandle (snapshot);
393+ return WideToString (pe.szExeFile );
394+ }
395+ } while (Process32Next (snapshot, &pe));
396+ }
397+
398+ CloseHandle (snapshot);
399+ return " " ;
400+ }
380401
381402void PrintAncestry (DWORD pid) {
403+ // now we're geting the name
404+ // we're making it slower by adding a bunch of snapshots
405+ // but again, we'll optimize and refactor later, i need this to work first
406+
382407
383408/*
384409~~~~~~~~~~~~~TODO: This tree is flipped. The output should be like this, as shown in the original witr:
@@ -558,6 +583,25 @@ CloseHandle(hSnapshot); // we're only closing the handle until we finish messing
558583
559584
560585void PIDinspect (DWORD pid) { // ooh guys look i'm in the void
586+ std::string procName = GetProcessNameFromPid (pid);
587+ if (IsVirtualTerminalModeEnabled ()) {
588+ if (procName == " " ){
589+ std::cout << " \033 [34mTarget:\033 [0m N/A\n\033 [34mProcess:\033 [0m N/A\n " ;
590+ } else {
591+ std::cout << " \033 [34mTarget:\033 [0m " << procName << " \033 [0m" << std::endl;
592+ std::cout << " \033 [34mProcess:\033 [0m " << procName << " \033 [90m (pid " << std::to_string (pid) << " )\033 [0m" << std::endl;
593+ }
594+ } else {
595+ if (procName == " " ){
596+ std::cout << " Target: N/A\n Process: N/A\n " ;
597+ } else {
598+ std::cout << " Target: " << procName << std::endl;
599+ std::cout << " Process: " << procName << " (pid " << std::to_string (pid) << " )" << std::endl;
600+ }
601+ }
602+
603+
604+
561605 HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ , FALSE , pid);
562606 // The above little handle opener is currently a somwehat "agressive" flag, since it
563607 // Requests read access directly to the process' actual memory. This can get us rejected if called
@@ -575,6 +619,7 @@ void PIDinspect(DWORD pid) { // ooh guys look i'm in the void
575619 bool queryError = false ;
576620 if (!hProcess) {
577621 errorCode = GetLastError ();
622+
578623
579624 if (IsVirtualTerminalModeEnabled ()) {
580625
@@ -816,7 +861,7 @@ int main(int argc, char* argv[]) {
816861 }
817862
818863
819- std::cout << " PID specified: " << pid << std::endl;
864+
820865 PIDinspect (static_cast <DWORD >(pid));
821866 } else {
822867 if (IsVirtualTerminalModeEnabled ()) { // ugh i have to do this EVERY SINGLE TIME
@@ -837,7 +882,7 @@ int main(int argc, char* argv[]) {
837882 std::string procName = arg;
838883 int pid = findMyProc (procName.c_str ());
839884 if (pid != 0 ) {
840- std::cout << " Process Name specified: " << procName << " (PID " << pid << " ) " << std::endl;
885+
841886 PIDinspect (static_cast <DWORD >(pid));
842887 } else {
843888 if (IsVirtualTerminalModeEnabled ()) {
0 commit comments