Excel 互操作:使用 Task.Run 创建实例会导致异常 System.EntryPointNotFoundException

Excel Interop: Creating instance with Task.Run results in exception System.EntryPointNotFoundException

这是我产生问题的最小示例:

using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;

class Program
{
    static void Main(string[] args)
    {
        Task.Run(() =>
        {
            Excel.Application app = new Excel.Application();

            if (app != null)
            {
                app.Quit();
                Marshal.FinalReleaseComObject(app);
                app = null;
            }
        });
    }
}

这会导致以下异常:

日语的最后一部分说"EventSetInformation" of DLL advapi32.dll entry point cannot be found。 我很难理解发生了什么。基本上为什么抛出这个异常以及它试图告诉我什么?

我示例中的异常是在 Windows 7 上使用 VS 2013 生成的。 在这种情况下,无法解析对 EventSetInformation 的调用,因为在 advapi32.dll 中找不到该函数。 然后,我用 Visual Studio 2015 CTP 测试了相同的代码,它 运行 完成,没有任何异常。这让我相信这是一个版本冲突。

此外,根据 here and msdnEventSetInformation 在 Windows 8 中 添加了 到 advapi32.dll。这就是为什么当我 运行 使用 VS 2013 的代码时找不到。因此,对于 运行 我的代码片段,我需要 advapi32.dll 的较新版本,它包含在后来的 Visual Studio 中版本(或 Windows 8)。

更新

根据https://github.com/dotnet/coreclr/issues/974

Note that the OS group has assured us that Win7 will be patched to include this API soon (within months), so that even the exception will not happen at that point

因此,很可能 advapi32.dll Windows 7 会在将来的某个时间更新以包含 EventSetInformation