views:

392

answers:

3

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?

+3  A: 

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.

Ryan J. Mills
+3  A: 

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
A: 

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
I didn't want to call pskill if the process was not running
Jason
pskill won't crash on that. there is no problem to call it on non-existing process.
Moisei