When new updates are available, my Delphi application counts processes running from the same directory. If it finds the current process only, then it downloads the updates and restarts itself.
For counting processes by full file name, I'm using OpenProcess
and GetModuleFileNameEx
as shown in this question.
In most cases, everything works but one of my customers uses thin clients and a server in which the application is installed in several directories (Each user has its own installation folder on the server).
When an user starts the application from his thin client, the application finds also the processes that belongs to other users but it can't obtain the executable full file name because OpenProcess
causes an access denied error:
ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.
Here is how I'm calling OpenProcess
and GetModuleFileNameEx
:
const
S_PROCESS_QUERY_LIMITED_INFORMATION = $1000;
function GetFullFileNameFromPID(const PID: Cardinal) : string;
var
ProcessHandle : THandle;
Path : array[0..MAX_PATH - 1] of Char;
begin
ProcessHandle := OpenProcess(S_PROCESS_QUERY_LIMITED_INFORMATION, False, PID);
if(ProcessHandle <> 0) then
begin
if(GetModuleFileNameEx(ProcessHandle, 0, Result, MAX_PATH) = 0)
then RaiseLastOSError
else Result := Path;
CloseHandle(ProcessHandle);
end else
RaiseLastOSError;
end;
Is there a way to get this information from a process for which the application have no permissions?
Update:
The filename is not enough for my purpose, I noticed that I can get the full file name using Task manager with the same user who gets ERROR_ACCESS_DENIED but I don't understand how it is obtaining that information.