Skip to content

Instantly share code, notes, and snippets.

@asklar
Created March 21, 2025 01:42
Show Gist options
  • Save asklar/aae8d376060a313b48dcefc9021ca1af to your computer and use it in GitHub Desktop.
Save asklar/aae8d376060a313b48dcefc9021ca1af to your computer and use it in GitHub Desktop.
List running processes with package family name
// proclist.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <wil/resource.h>
#include <appmodel.h>
int main()
{
wil::unique_handle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0));
if (snapshot.get() == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to create snapshot: " << GetLastError() << std::endl;
return 1;
}
PROCESSENTRY32W processEntry{ sizeof(PROCESSENTRY32W) };
if (!Process32FirstW(snapshot.get(), &processEntry))
{
std::cerr << "Failed to get first process: " << GetLastError() << std::endl;
return 1;
}
do
{
std::wstring packageFamilyName;
// try to get the package name
wil::unique_process_handle processHandle(OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processEntry.th32ProcessID));
if (processHandle.get())
{
WCHAR packageName[PACKAGE_NAME_MAX_LENGTH];
uint32_t packageNameLength = ARRAYSIZE(packageName);
if (GetPackageFamilyName(processHandle.get(), &packageNameLength, packageName) == S_OK)
{
packageFamilyName = packageName;
}
}
std::wcout << processEntry.szExeFile << L" (PID: " << processEntry.th32ProcessID << L")";
if (!packageFamilyName.empty())
{
std::wcout << L" - Package: " << packageFamilyName;
}
std::wcout << std::endl;
} while (Process32NextW(snapshot.get(), &processEntry));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment