Search code examples
c++winapiwindows-servicesremote-desktoprdp

WIndows service run desktop program in Remote Desktop


I need launch program in remote desktop, this program open a window and need user interact, but my application is a windows service.

I'm using windows 2008. I'm created an application with success but launch program only session connect with mouse, keyboard, but I need launch in RDP.

Now I'm using these functions.

WTSEnumerateSessions (enum all sessions, here I know if RDP)
WTSQueryUserToken (Get user token)
DuplicateTokenEx ( make a primary token)
CreateProcessAsUser (run application with SI.lpDesktop = _T("winsta0\\default"))

But this only work success for run application in console connect to keyboard, mouse, monitor, not RDP.

Anyway can help me ? thanks.


Solution

  • So you're trying to launch a GUI application in a particular RDP session on the machine? You have the right idea, more or less, though you may be missing a few pieces. Take a look at the ProcessHelper class in the source for automated tests of the Cassia library. You can remove a lot of the managed cruft for use in C++. You can also omit the GetTokenInformation( ...TokenInformationClass.TokenLinkedToken... ) nonsense if you don't need to run the process with elevated permissions. This code has been tested on every version of Windows since Windows XP.

    Note also that calling WTSQueryUserToken requires the SE_TCB_NAME privilege (which the LocalSystem account has by default).