none
Problem in starting MS Outlook from C# application RRS feed

  • Question

  • Hi.

    I implemented code to process some emails in the Outlook inbox. In my development machine, everything required are working fine. But in some other machines, the code for starting the Outlook application instance is causing problem. The code I used to start Outlook application is as below:

    System.Diagnostics.Process[] outlookInstances = System.Diagnostics.Process.GetProcessesByName("OUTLOOK");
    
                    wasOutlookRunningBeforeProcess = (outlookInstances.Length > 0);
    
                    if (outlookApp == null)
                    {
                        if (wasOutlookRunningBeforeProcess)
                        {
    
                            outlookApp = (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
                            System.Windows.Forms.MessageBox.Show("Section 1 ");
                        }
                        else
                        {
                            outlookApp = new Microsoft.Office.Interop.Outlook.Application();
                            System.Windows.Forms.MessageBox.Show("Section 2");
                        }
                    }

    The extra code starts a new instance of Outlook only if Outlook is not running. If an instance of Outlook is already running, the running instance is used. It is also. After processing emails, the program performs some admin tasks. So the application will always be run with Admin Privileges.

    The problem raises here when the user has already launched Outlook with normal privileges and our application tries to use the available instance of outlook. The conflict between the two is our application runs with admin privileges and the existing instance of Outlook is running in Normal previleges. In this situation, our application throws an exception and terminates. The StackTrace of the thrown exception is:

    System.Runtime.InteropServices.COMException (0x800401E3): Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))    at System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object& ppunk)    at System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)    at CustomApplication.StartOutlookApplication()

    This is really urgent to get a solution for this. Can anyone help to get rid of it? Any help will greatly be appreciated.


    :RSMANU:

    Tuesday, February 14, 2012 7:13 AM

Answers

  • You need to run the process that starts or hooks into Outlook with the same permissions level as Outlook. It just won't work otherwise. You probably should just start or grab the Outlook process at asInvoker level and then spawn the admin process from there, requesting admin permissions in the manifest for the admin app. You'd be prompted for permissions, but it won't work with Outlook otherwise.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ananthan Unni" <=?utf-8?B?QW5hbnRoYW4gVW5uaQ==?=> wrote in message news:bea39228-68a3-435d-a787-20f014b3a21b...

    Hi.

     The error that i mentioned above is showing up in all machines when the exe is run with Administrator rights. Further digging down the problem I could see that the error is because the both applications is run by different users. [ my application as admin and outlook as normal user]. The error vanishes when the exe is run normally or if the outlook is kept closed.

    But the problem here is that my application is a long running app and should run behind the scene silently. Most of the features requires admin privliages too. With all these constraints, I dont have the options to compromise on above mentioned solutions. 

     any help is highly appreciated.


    :RSMANU:



    Ken Slovak MVP - Outlook
    Tuesday, February 14, 2012 2:00 PM
    Moderator

All replies

  • Detect if process is not evelated (even just by catchig this specific exception if you will) and restart your own process as not elevated (asInvoker level).
    Tuesday, February 14, 2012 8:18 AM
  • Hi..

    Thank you for your reply. But my application needs to be run elevated as it has some admin tasks to perform. I want my application to be able to connect to an instance of Outlook either it has admin privileges or not. Any way to do that?


    :RSMANU:

    Tuesday, February 14, 2012 8:46 AM
  • Span child process with asInvoker and let it start outlook. You should be able to communicate with your child process using for example remoting or pipes and your child process should automate outlook. 
    Tuesday, February 14, 2012 8:56 AM
  • Hi.

     The error that i mentioned above is showing up in all machines when the exe is run with Administrator rights. Further digging down the problem I could see that the error is because the both applications is run by different users. [ my application as admin and outlook as normal user]. The error vanishes when the exe is run normally or if the outlook is kept closed.

    But the problem here is that my application is a long running app and should run behind the scene silently. Most of the features requires admin privliages too. With all these constraints, I dont have the options to compromise on above mentioned solutions. 

     any help is highly appreciated.


    :RSMANU:


    • Edited by Ananthan Unni Tuesday, February 14, 2012 9:22 AM Add more information
    Tuesday, February 14, 2012 9:17 AM
  • Spanning child process is not an option?

    Tuesday, February 14, 2012 9:25 AM
  • I'm sorry I didn't get what you meant by 'Spanning child processes'. Can you please make it clearer? If possible, any sample code please.....

    :RSMANU:

    Tuesday, February 14, 2012 9:53 AM
  • Create new process by starting your own exe (other one or the same passing argument to detect that this is child process) that will run with normal user level and let it communicate with outlook and your master process will communicate with your child process (by sharing a pipe for example).
    Tuesday, February 14, 2012 10:07 AM
  • You need to run the process that starts or hooks into Outlook with the same permissions level as Outlook. It just won't work otherwise. You probably should just start or grab the Outlook process at asInvoker level and then spawn the admin process from there, requesting admin permissions in the manifest for the admin app. You'd be prompted for permissions, but it won't work with Outlook otherwise.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ananthan Unni" <=?utf-8?B?QW5hbnRoYW4gVW5uaQ==?=> wrote in message news:bea39228-68a3-435d-a787-20f014b3a21b...

    Hi.

     The error that i mentioned above is showing up in all machines when the exe is run with Administrator rights. Further digging down the problem I could see that the error is because the both applications is run by different users. [ my application as admin and outlook as normal user]. The error vanishes when the exe is run normally or if the outlook is kept closed.

    But the problem here is that my application is a long running app and should run behind the scene silently. Most of the features requires admin privliages too. With all these constraints, I dont have the options to compromise on above mentioned solutions. 

     any help is highly appreciated.


    :RSMANU:



    Ken Slovak MVP - Outlook
    Tuesday, February 14, 2012 2:00 PM
    Moderator