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

送分小仙女□ 提交于 2020-01-02 08:31:26

问题


Here is my minimal example producing the problem:

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;
            }
        });
    }
}

This results in the following exception:

The last part in Japanese says the "EventSetInformation" of DLL advapi32.dll entry point cannot be found. I'm having difficulty understanding what is going on. Basically why is this exception being thrown and what it is trying to tell me?


回答1:


The exception in my example was produced with VS 2013 on Windows 7. In this case, the call to EventSetInformation could not be resolved as the function could not be found in advapi32.dll. Then, I tested the same code with Visual Studio 2015 CTP and it ran to completion without any exceptions. This led me to believe it was a version conflict.

Also, according to here and msdn, EventSetInformation was added to advapi32.dll in Windows 8. This is why it could not be found when I ran the code with VS 2013. Therefore, to run my code snippet, I needed the newer version of advapi32.dll which is included with later Visual Studio versions (or Windows 8).

UPDATE

According to 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

So, most likely advapi32.dll on Windows 7 will be updated to include EventSetInformation some time in the future.



来源:https://stackoverflow.com/questions/29908048/excel-interop-creating-instance-with-task-run-results-in-exception-system-entry

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