Dynamically load and use COM object in C#

只谈情不闲聊 提交于 2019-12-06 05:27:35

问题


I have a C# project, where I would like to access MS outlook, if it is installed on a client´s machine. The "access outlook" part has been done by referencing the outlook COM object, and going from there. My problem is now the "if it is installed" part. At the moment, my project doesn´t compile on machines without outlook installed, so I assume that I will have to not reference the outlook component, and instead load and use it dynamically, after detecting that outlook is present, but I haven´t found a way to do this. Am I correct, and does anybody have any hints on how to do this?

Thanks.

Edit: Resolved. Following the advice given by Hans Passant in one of the comments about using the office PIAs, proved to be the path of least resistance. I had a little difficulty getting the PIAs on my office-less machine, but it was overcome using the accepted answer to this question.


回答1:


You definitely won't be able to compile your assembly on a machine without the Outlook COM object being present, but that doesn't mean that your application will completely fail to work on a machine without Outlook - just that attempting to create or use the Outlook COM object will result in a failure / exception being thrown.

According to this question the best way of detecting whether or not a COM object is present is simply attempting to instantiate it and catching the resulting exception in the case where it is not installed.




回答2:


Following Hans Passant's advice, I now answer my own question with details of my solution. I was able to both compile and run my project on a computer without office installed. The primary problem was to let visual studio know, which interface it could expect from the COM object. This was solved by finding a primary interface assembly (PIA) file for outlook. That process is better described in this question, but the short story is that I already had the office PIAs on my computer, in this location:

C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA

I use VS2010 pro, I don't know if the files are included with other versions. It was then a matter of copying the relevant file to my project source folder, including it in my project and source control, and making a reference to it. After that, I was able to use Outlook COM types in my project and compile it without error. I am also able to run my project without office installed. This is achieved by trying to instantiate the relevant COM objects, and catching the exception if office is not installed. The following sample code shows how:

using Outlook = Microsoft.Office.Interop.Outlook;

namespace Foo
{
    public class Bar
    {
        public void Quux()
        {
            try
            {
                // try to instantiate outlook COM object.
                Outlook.Application outlookApp = new Outlook.Application();

                // if it works, fine. Proceed
                ...
            }
            // If we catch a COMException, assume no office installed. Deal accordingly.
            catch (System.Runtime.InteropServices.COMException)
            {
                ...
            }
        }
    }
}


来源:https://stackoverflow.com/questions/5041431/dynamically-load-and-use-com-object-in-c-sharp

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