Hi,
I've tried to use pid_name from psutil using the corrections proposed in https://github.com/johnscillieri/psutil-nim/issues/13 But even if it compiles I get an empty string each time whereas I am sure of the process ID.
Anybody know if there another way to get the name of a running process knowing it's PID?
Thanks
When you got a problems with windows API, reading microsoft's documents about it would help you. Microsoft windows is created by microsoft and their document is the first source of their API. Open following site and search for APIs you are using and read related documents. https://learn.microsoft.com/en-us/docs/
These documents are written for C programmers. If you don't know much about C programming language, learning it helps understanding the documents.
Many windows API functions has pointer parameters. You need to know about pointers, memory address and how static storage, stack, or heap memory works to use these functions correctly. Nim's memory safety cannot cover C functions. This site explains about the Nim memory model: http://zevv.nl/nim-memory/ Also good C lang books explains about memory.
Uncle chatGPT answered me with this:
import os, strutils, sequtils
import winim
# This procedure gets the executable name from a process ID (PID) on a Windows system.
proc getExeNameFromPid(pid: DWORD): string =
# Define the required access rights for the OpenProcess function.
const PROCESS_QUERY_INFORMATION = 0x0400
const PROCESS_VM_READ = 0x0010
var
# Declare variables to store the process handle and the executable path.
processHandle: HANDLE
exePath: array[MAX_PATH, TCHAR]
# Open the process with the specified PID and access rights.
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
false, pid)
# Check if the process handle is valid. If not, raise an exception.
if processHandle == 0:
raise newException(Exception, "Failed to open process with PID: " & $pid)
# Ensure the process handle is closed after its usage.
defer: CloseHandle(processHandle)
# Get the process image file name using the process handle.
let len = GetProcessImageFileName(processHandle, addr exePath[0], MAX_PATH)
# Check if the process image file name was retrieved successfully. If not, raise an exception.
if len == 0:
raise newException(Exception, "Failed to get process image file name for PID: " & $pid)
let path = exePath.mapIt(it.chr).join()
let (_, exeName) = splitPath(path)
# Return the process image file name as a string.
return exeName
# This procedure is the main entry point of the program.
proc main() =
# Check if a command-line argument was provided.
if paramCount() < 1:
echo "Usage: ", getAppFilename(), " <pid>"
return
# Convert the first command-line argument to an integer (PID).
let pid = paramStr(1).parseInt
# Get the executable name for the specified PID.
let exeName = getExeNameFromPid(DWORD(pid))
# Print the executable name.
echo "Executable name for PID ", pid, " is: ", exeName
# The main entry point of the program.
when isMainModule:
main()