Untitled
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.IO; // Required for Path.GetFileName() using tai; using UnityEngine; public class Test : MonoBehaviour { [DllImport("psapi.dll")] private static extern bool EnumProcesses([Out] uint[] processIds, int size, out int needed); [DllImport("kernel32.dll")] private static extern IntPtr OpenProcess(uint access, bool inherit, uint processId); [DllImport("psapi.dll", CharSet = CharSet.Auto)] private static extern int GetProcessImageFileName(IntPtr hProcess, StringBuilder lpImageFileName, int nSize); private const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000; private const int MAX_PROCESSES = 1024; void Start() { try { uint[] processIds = new uint[MAX_PROCESSES]; int bytesReturned; if (EnumProcesses(processIds, processIds.Length * sizeof(uint), out bytesReturned)) { int numProcesses = bytesReturned / sizeof(uint); for (int i = 0; i < numProcesses; i++) { IntPtr hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processIds[i]); if (hProcess != IntPtr.Zero) { StringBuilder buffer = new StringBuilder(1024); int length = GetProcessImageFileName(hProcess, buffer, buffer.Capacity); if (length > 0) // Only log valid processes { string exeName = Path.GetFileName(buffer.ToString()); // Extracts only the EXE name Log.Info($"Process: {exeName}"); } } } Log.Info($"Total Processes: {numProcesses}"); } } catch (Exception ex) { Log.Error($"Failed to retrieve process list: {ex.Message}"); } } }
Leave a Comment