Skip to content

Instantly share code, notes, and snippets.

@khchen
Last active November 9, 2021 18:27
Show Gist options
  • Select an option

  • Save khchen/5cd03eab742517fa20ccc688e6b1a1a6 to your computer and use it in GitHub Desktop.

Select an option

Save khchen/5cd03eab742517fa20ccc688e6b1a1a6 to your computer and use it in GitHub Desktop.
#[
Author: Ward
Example of EnumProcesses.
References:
https://docs.microsoft.com/en-us/windows/win32/psapi/enumerating-all-processes
]#
import winim/lean
import winim/inc/psapi
import strformat
# This converter is really convenient.
converter nimIntTocint(x: int): cint = cint x
var
aProcesses: array[1024, DWORD]
cbNeeded: DWORD
if EnumProcesses(&aProcesses[0], sizeof(aProcesses), &cbNeeded) != 0:
for i in 0 ..< cbNeeded div sizeof(DWORD):
var hProcess = OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, aProcesses[i])
defer: CloseHandle(hProcess)
if hProcess != 0:
var
hMod: HANDLE
cbNeeded: DWORD
szProcessName = T(MAX_PATH) # generate wstring or mstring buffer depend on conditional symbol
if EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded) != 0:
GetModuleBaseName(hProcess, hMod, &szProcessName, szProcessName.len)
echo szProcessName.nullTerminated, fmt" (PID: {aProcesses[i]})"
else:
echo fmt"<unknown> (PID: {aProcesses[i]})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment