Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) when running through Windows Service?

不问归期 提交于 2019-12-23 17:33:41

问题


i am try to hook to outlook application from windows Service but getting an exception Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) here is my code.

   public void ItemSendEvent()
    {
        try
        {
           if (Process.GetProcessesByName(ApplicationConstants.OUTLOOK_PROCESS_NAME).Count() > 0)
                {
                    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                    outlookApplication = Marshal.GetActiveObject(ApplicationConstants.OUTLOOK_APPLICATION_NAME) as Microsoft.Office.Interop.Outlook.Application;
                    Microsoft.Office.Interop.Outlook.NameSpace nameSpace = outlookApplication.GetNamespace(ApplicationConstants.OUTLOOK_NAME_SPACE);
                    nameSpace.Logon("", "", Missing.Value, Missing.Value);
                    nameSpace = null;
                    outlookApplication.ItemSend += outlookApplication_ItemSend;
                }
                log.Info("Outlook Item Send event registered successfully.");
        }
        catch (System.Exception ex)
        {
            log.Error("Exception occurred while registering Outlook Item Send event. " + ex.Message);
        }
    }

but the same code when i launch it through Windows Form Application its working Fine. i gone through some site's and they were saying that outlook object is not in ROT Table. what will be the solution.


回答1:


Outlook, or any other Office app, cannot run in a Windows service even if your service is running as an interactive user. Only Extended MAPI (C++ or Delphi only) or an Extended MAPI wrapper like Redemption (its RDO family objects) can be used in a service.

In you particular case, it looks like you are trying to trap the Application.ItemSend event. There is absolutely no reason to create a Windows service for that. Create a COM addin - it will be loaded by Outlook and run as long as Outlook itself is running in the same process in the same security context.




回答2:


Two common issues could cause this.

The first would be that you are running Visual Studio in Administrator mode and you are starting your program from within VS, and the Office application is not. To fix that, you need to run your Office application with elevated privileges, in Administrator mode, as well.

The second could be caused by the application not being fully started/loaded when you call Marshal.GetActiveObject(...).




回答3:


you don't need to have your application as a service to get it on the background ...

if your winform work well, just put your winform in background running on the systray for instance



来源:https://stackoverflow.com/questions/37589935/operation-unavailable-exception-from-hresult-0x800401e3-mk-e-unavailable-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!