With Delphi 10 Seattle, I use this code to get the handle of a process:
uses
Winapi.Windows;
var
hp: THandle;
begin
hp := OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID);
This works well with all processes, both 32 bit and 64 bit.
Unfortunately, it does not work with ELEVATED PROCESSES, where it gives back 0.
So how can I get the process handle of an elevated process?
The simple answer is that you cannot do this. The system said no. When you call GetLastError
the value returned will be ERROR_ACCESS_DENIED
. Your process does not have sufficient rights to gain PROCESS_QUERY_INFORMATION
to a process that is elevated.
You will succeed if you make the call to OpenProcess
when your process is elevated.
Alternatively, it is plausible that you might be able to get by with lower rights than PROCESS_QUERY_INFORMATION
. That's hard to judge since we don't know what you mean to do with this process handle. According to your comments, you will pass the handle to GetProcessImageFileName
, which is documented as requiring either PROCESS_QUERY_INFORMATION
or PROCESS_QUERY_LIMITED_INFORMATION
.