How to check if exe is running on terminal session from the server

delphi, service, terminal

Solution

If you check these SO question I believe your question will be answered. They may not be for delphi specifically but the approved answer for the second one provides a link to the MSDN website.

how-to-programmatically-tell-if-the-terminal-server-service-is-running

how-do-i-tell-if-my-application-is-running-in-an-rdp-session

Or another quick search on the web revealed this code snippet. (This is not my code)

function ProcessIdToSessionId(dwProcessId: DWORD; pSessionId: DWORD): BOOL; stdcall; external 'kernel32.dll';

function GetSessionIdfromProccessId(const processId: DWORD; var sessionId: DWORD): boolean;
begin
    result:=ProcessIdToSessionId(processId, DWORD(@sessionId));
end;

function GetCurrentSessionId: DWORD;
begin
    if not GetSessionIdfromProccessId(GetCurrentProcessId,result) then
        result:=0;
end;

It seems as if the result from GetCurrentSessionid <> 0 then your running under TS.

HTH.

Problem

I'm creating a update program that will copy over updated .exe files. It needs to check if any of the .exes are running in a terminal session. If the .exes are running it will kill them. This will be a service that is running on the server with admin rights. The code needs to be in Delphi any one have any thoughts on this?

Original source

Related problems