Search code examples
asp.netwcfcomitunescom-interop

Local IIS - Com interop iTunes


Small question, was anyone able to control iTunes through any local webdeployment? Most preferable through a hosted IIS WCF service?

I've tried on my Windos 7 with IIS7.5 and when I set the Process Model - Identity to 'Local System' on the IIS apppool containing the WCF service, I see the iTunes.exe popping up in the Task Manager. But iTunes doesn't come 'alive' - no visual shell and even though the iTunesApp object is initialized in my WCF Service code, perfomring any actions on it won't work.

Side not, running the same service through Visual Studio 2010 debug mode, everything works just great!

  • UPDATE * I was trying to communicate to iTunes through my windows phone and I thought of going round that with a website/service... but with the new Mango update we can do TCP sockets native on the phone! SO I'll try that route.

Solution

  • The reason service does not show any UI windows is becuase of Session Isolation. Your service (IIS application pool process) is running in session 0. Your desktop is attached to session 1 (or some other number if more than one user is logged on on this machine).

    There are couple of workarounds to allow services to show UI to user:
    1. You can mark service with option "Allow service to interact with desktop". This only works for services that are running as local system. This option is deprecated, should only be used for compatibility with legacy services.
    2. Service can launch an intermediate process in user session and communicate to it.

    If you don't want to interact with iTunes, and only want to lanch it in user session, what you need to do is:

    • Obtain name of windows station the user is running. You can use windows terminal services API for that. You will have to be creative to figure out which user session is currently active (if there is more than one). You probably also want to query user security token, so that process is run as a user, and not as a local system.
    • Call CreateProcessAsUser and pass STARTUPINFO structure. Set lpDesktop field in STARTUPINFO to point to window station you identified.