EnumProcesses 

www.madshi.net

You can easily enumerate processes in most OSs by using the well known toolhelp functions. Unfortunately the toolhelp functions don't work in NT4. So here's a function which enumerates processes and works well in all OSs.

type
  TDAProcess = array of record
    id      : dword;    // process id
    exeFile : string;   // exe file (9x = full path; nt = name only)
    session : dword;    // session id
    sid     : string;   // user sid
  end;

function EnumProcesses : TDAProcess;

// Example:
function GetProcessSessionId(processId: dword) : dword;
var i1 : integer;
    pl : TDAProcess;
begin
  result := 0;
  pl := EnumProcesses;
  for i1 := 0 to high(pl) do
    if pl[i1].id = processId then begin
      result := pl[i1].session;
      break;
    end;
end;