从 .NET Windows 服务调用 Shell32.dll

Calling Shell32.dll from .NET Windows Service(从 .NET Windows 服务调用 Shell32.dll)

本文介绍了从 .NET Windows 服务调用 Shell32.dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET 4.0 库,它使用 Shell32 和 Folder.GetDetailsOf() 从 WTV 文件中获取元数据.我已经成功地将它与控制台和 Windows 窗体应用程序一起使用,没有问题.但由于某种原因,当从 .NET 4.0 Windows 服务调用组件时,调用启动 Shell 类会导致 COM 错误.

I have a .NET 4.0 library that uses Shell32 and Folder.GetDetailsOf() to get metadata from WTV files. I have used it successfully with Console and Windows Forms apps without issue. But for some reason, when calling the component from a .NET 4.0 Windows Service, the call to initiate the Shell class causes a COM error.

库内失败的代码:

Shell32.Shell shell = new Shell();

Shell32.Shell shell = new Shell();

错误:

无法将System.__ComObject"类型的 COM 对象转换为接口类型Shell32.Shell".此操作失败,因为 IID 为{286E6F1B-7113-4355-9562-96B7E9D64C54}"的接口的 COM 组件上的 QueryInterface 调用失败,原因是以下错误:不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE)).

Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

我阅读了我的 Apartment Threading、COM Interops、Dynamic、PIA's 等,等等 :) 但我找到的解决方案组合都没有解决这个问题.它必须是来自另一个看不到互操作的线程的调用.请帮忙:)

I read my fill of Apartment Threading, COM Interops, Dynamic, PIA's, etc, etc, etc :) But no combination of solutions I've found has solved the problem. It must be a calling from another thread that can't see the Interop. Help, please :)

推荐答案

我最近在使用命令行应用程序(控制台)时遇到了同样的问题.事实证明,需要使用 [STAThread] 属性来注释程序的 Main() 方法.还需要注意的是,如果入口点使用 [MTAThread] 进行注释,它会以完全相同的方式失败.希望对你有帮助.

I had the same problem just recently with a command line application (console). Turns out, it was required to annotate the program's Main() method with the [STAThread] attribute. It has also been noted that it fails miserably in the exact same way if the entry point is annotated with [MTAThread] instead. I hope it helps.

这篇关于从 .NET Windows 服务调用 Shell32.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 .NET Windows 服务调用 Shell32.dll

基础教程推荐