Last active
November 9, 2021 18:27
-
-
Save khchen/5cd03eab742517fa20ccc688e6b1a1a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[ | |
| 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