2

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?

  • I'm not sure about the capibilities of Delphi, but it should be as simple as grabbing your running process list, enumerating through it, and doing a literal check agianst the process name. – George Johnston Jan 14 '10 at 20:29

3 Answers3

8

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.

Community
  • 1
  • 1
Vivian Mills
  • 2,552
  • 14
  • 19
3

There are a group of APIs that can enumerate and retrieve sessions and processes in sessions. The two you may need are WTSEnumerateSessions and WTSEnumerateProcesses.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
0

The processes have to be killed anyway, whether they run under terminal session or not. Can you please clarify - why do you need this info?

The best way to kill process is to use "pskill" until from sysexternals. Just use exec from your Delphi code.

Moisei
  • 1,162
  • 13
  • 30