How to launch a process in a Terminal Session #2

Command Prompt IconA little while ago I wrote an article on launching a process in another Terminal Session (

The article didn’t have a demo app yet so I’ve attached it here.

How do you use it?

It’s a commandline tool and it’s called RunInSession. You need to specify at least the SessionId in which you want to launch the process and which process you want to launch. Optional is servername if you want to launch on a remote server. If you run it without parameters a dialog with possible parameters is shown:

Run In Session Screenshot

Currently supported OS versions are Windows XP, 2003, Vista and 2008.

How does it work?

The program needs to run in the context of the Localsystem user, therefore it temporarily installs itselfs as service and start itsself. With the WTSQueryUserToken it obtains the Primary User token of the requested Terminal Session. Finally the process is launched with CreateProcessAsUser and the service deletes itsself. (1917)

Leave a Reply

  1. Pingback:   Launch a process in another Session. by JEDI Windows API

  2. Pingback: Automatically Accept Shadow Request | Remko Weijnen's Blog (Remko's Blog)

  3. Hi Remko,

    Hope you are well, as always you’re the first port of call for anything Delphi\RDS related!

    Just wondered if you ever came up against the 32\64bit bug in XP\Server 2003 where a 32-bit service\application cant call WTSQueryUserToken on a 64-bit version of the OS?

    I’ve build a TS\RDP recorder app but falls over when making this call, annoying as Microsofts answer is simply to rebuild your app with a 64-bit compiler!


    Not sure if you were able to get around this at all. If not it’s worth noting, although I understand Server 2003 is proper old school!!

    Hope you’re well, take care Remko…


  4. Ha ha, that’s absolutely amazing daNIL, talk about the EXACT answer to the question I was asking!! Man you guys are awesome.

    I am going to check this out now and see if I can get it going.

    Guys keep up the good work, there is so much here that has got myself and many other Delphi guys out of trouble. It’s blogs like this that keep Delphi going, let’s hope with a 64-bit compiler it’ll breathe new life into the language.

    Thanks again!!!!


  5. Hi! I’m receiving this message could not obtain primary user token. What should I do ? I’m using PCExpanion ThinClient and the others sessions is running on them. I want this to open a webpage in all sessions. So it will be easy to open a website in all stations when some teacher wants it to. Sorry for the bad english.

  6. Hi, thanks for tool. I wonder why it runs only programs from system32 folder? Eg it runs notepad.exe but it does not run my program, even if I specify the full PATH. If I copy my program into system32, it runs it, but I want to run it in a special folder.
    Any hints? Thanks.

  7. To bad it doesn’t work 🙁

    From windows 7 machine i tried to control a session on a windows 2008 r2 machine.

    The RuninService service could not be started?!?

    Do i need to install a service somewhere?