How to Interop with Outlook when running with elevated privileges?

1

While debugging an issue with Outlook Interop, I noticed that when starting my process as an user that is part of the administrator group, my application works as expected. However running it as the same user under elevated privileges fails.

The issue can be reproduced easily as it arises as soon as we try to do interop.

When trying to create a new instance:

var NewOutlookApp = new Application();
// Crashes if Outlook is already running: System.Runtime.InteropServices.COMException: La récupération de la fabrique de classes COM pour le composant avec le CLSID {0006F03A-0000-0000-C000-000000000046} a échoué en raison de l'erreur suivante : 80080005 Échec de l'exécution du serveur (Exception de HRESULT : 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
// This translates to: retrieving the COM class factory for componant with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error : 80080005 Failed to run server (HRESULT Exception : 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
// If Outlook isn't already running this works as expected.

When trying to retrieve the existing instance:

var ExistingOutlookApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
// Crashes if Outlook is not running (Expected behaviour).
// Crashes even if Outlook is already running: System.Runtime.InteropServices.COMException (0x800401E3): Opération non disponible (Exception de HRESULT : 0x800401E3 (MK_E_UNAVAILABLE))
// This translates to: Operation not available.

Is this the expected behaviour? If so what would be the appropriate way to interop between an application running with elevated privileges and Outlook running without elevation while both processes run as the same user?

Environment's configuration:

  • Windows 10, fully up to date

  • Outlook 2016 click to run

  • Interop library: 14.0.0.0

  • Visual Studio 2017

c#
.net
outlook
office-interop
com-interop
asked on Stack Overflow Jun 20, 2018 by Laurent Gabiot

1 Answer

1

You must run both applications under the same security context. There is no any possible workaround around that problem. The best what you can do is to let users know or ask them to run both applications under the same security context (privileges). For example, you may check out the list of running processes in Windows, so if you see Outlook.exe there and can't get the running Outlook instance it makes sense to notify them.

answered on Stack Overflow Jun 20, 2018 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0