use serde_json::{Value, json};
use windows::Win32::Foundation::CloseHandle;
use windows::Win32::System::Diagnostics::ToolHelp::*;
pub fn list_processes() -> anyhow::Result<Vec<Value>> {
unsafe { proc_inner() }
}
unsafe fn proc_inner() -> anyhow::Result<Vec<Value>> {
let snap = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }?;
let mut entry = PROCESSENTRY32W::default();
entry.dwSize = std::mem::size_of::<PROCESSENTRY32W>() as u32;
let mut procs = Vec::new();
if unsafe { Process32FirstW(snap, &mut entry) }.is_ok() {
loop {
let pos = entry
.szExeFile
.iter()
.position(|&c| c == 0)
.unwrap_or(entry.szExeFile.len());
let name = String::from_utf16_lossy(&entry.szExeFile[..pos]);
procs.push(json!({
"pid": entry.th32ProcessID,
"ppid": entry.th32ParentProcessID,
"name": name,
"threads": entry.cntThreads,
}));
if unsafe { Process32NextW(snap, &mut entry) }.is_err() {
break;
}
}
}
let _ = unsafe { CloseHandle(snap) };
Ok(procs)
}