Skip to content

Instantly share code, notes, and snippets.

@codebeta
Created May 26, 2023 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebeta/86102f2d1256263d46396513141460c7 to your computer and use it in GitHub Desktop.
Save codebeta/86102f2d1256263d46396513141460c7 to your computer and use it in GitHub Desktop.
Display a message box with process details
#include <iostream>
#include <sstream>
#include <windows.h>
#include <tlhelp32.h>
std::string GetProcessName(DWORD pid)
{
std::string processName = "";
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &processEntry))
{
do
{
if (processEntry.th32ProcessID == pid)
{
processName = processEntry.szExeFile;
break;
}
} while (Process32Next(hSnapshot, &processEntry));
}
CloseHandle(hSnapshot);
}
return processName;
}
DWORD getppid()
{
HANDLE hSnapshot;
PROCESSENTRY32 pe32;
DWORD ppid = 0, pid = GetCurrentProcessId();
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
__try{
if( hSnapshot == INVALID_HANDLE_VALUE ) __leave;
ZeroMemory( &pe32, sizeof( pe32 ) );
pe32.dwSize = sizeof( pe32 );
if( !Process32First( hSnapshot, &pe32 ) ) __leave;
do{
if( pe32.th32ProcessID == pid ){
ppid = pe32.th32ParentProcessID;
break;
}
}while( Process32Next( hSnapshot, &pe32 ) );
}
__finally{
if( hSnapshot != INVALID_HANDLE_VALUE ) CloseHandle( hSnapshot );
}
return ppid;
}
std::string GetParentProcessName(DWORD pid)
{
std::string parentProcessName = "";
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &processEntry))
{
do
{
if (processEntry.th32ProcessID == pid)
{
DWORD parentPid = processEntry.th32ParentProcessID;
parentProcessName = GetProcessName(parentPid);
break;
}
} while (Process32Next(hSnapshot, &processEntry));
}
CloseHandle(hSnapshot);
}
return parentProcessName;
}
void ShowProcessInfo()
{
DWORD pid = GetCurrentProcessId();
std::string processName = GetProcessName(pid);
std::string parentProcessName = GetParentProcessName(pid);
// Display the process information in a message box
std::ostringstream oss;
oss << "Process Name: " << processName << "\n"
<< "Process ID: " << pid << "\n"
<< "Parent Process Name: " << parentProcessName << "\n"
<< "Parent Process ID: " << getppid() << std::endl;
MessageBoxA(NULL, oss.str().c_str(), "Process Details", MB_OK);
}
int main()
{
ShowProcessInfo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment