Question

I have a windows service that needs to access the Windows Media Player libraries of all currently logged on users. Since its a service, I must impersonate the logged on user so that calls to the WMP COM will return information in the impersonated user's context (i.e. return the impersonated user's library rather than the system's library). I need to do this for each logged on user.

I am able to successfully impersonate a user in a thread via ImpersonateLoggedOnUser, get the user's library, then perform a RevertToSelf. The problem arises when I try to do this for the next logged on user. It appears that any subsequent calls to the WMP COM always return the first impersonated user's library rather than the current user being impersonated.

Here is a general gist of what I'm doing:

CoInitialize(NULL);
IWMPCore* Core = NULL;
CoCreateInstance(__uuidof(WindowsMediaPlayer), 0, CLSCTX_INPROC_SERVER, __uuidof(IWMPCore), (void **)&Core);
IWMPMediaCollection* Collection = NULL;
Core->QueryInterface(__uuidof(WindowsMediaPlayer), (void **)&Collection);
...
//Do work
...
CoUninitialize();
Was it helpful?

Solution

Are you sure you are releasing all the interfaces?

Have you tried calling CoFreeUnusedLibrariesEx and checking in Process Explorer if the WMP dll's are unloaded (Only dlls with a DllCanUnloadNow export will be unloaded though)?

If it still fails then maybe it is impossible for WMP to be unloaded completely and then you might have to use a helper/host process for each user and get the data back into the main process with some form of IPC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top